`

从1970至今的毫秒数(Linux C)

阅读更多

#include <time.h>
#include <sys/time.h>
#include <iostream>
#include <stdio.h>
using namespace std;

long long GetMillSec();

int main()
{
	long long nBegin = 0;
	long long nEnd = 0;

	nBegin = GetMillSec();

	struct timeval tv; 
	gettimeofday(&tv,NULL);

	cout << "sec: " << tv.tv_sec << endl;		//秒
	cout << "u_sec: " << tv.tv_usec << endl;	//微秒
	cout << "m_sec: " << tv.tv_usec / 1000 << endl;

	/*time_t nSec = time((time_t*)NULL);
	cout << "time1:" << nSec << endl;

	time_t t2 = 0;
	time_t t3 = 0;
	t2 = time(&t3);

	cout << "time2:" << t2 << endl;
	cout << "time3:" << t3 << endl;*/

	for (int i = 0; i < 1000; i++)
	{
		int nSub = 0;
		nSub += i;
		int n = atoi("11111");

		cout << nSub << endl;
	}
	
	nEnd = GetMillSec();

	long long nSub = nEnd - nBegin;

	cout << "begin sec:" << nBegin << endl;
	cout << "end sec:" << nEnd << endl;
	cout << "sub Mill Sec1:" << nSub << endl;
	cout << "sub Mill Sec2:" << nEnd - nBegin << endl;

	return 0;
}

long long GetMillSec()
{
	long long nMillSec = 0;

	struct timeval tv; 
	gettimeofday(&tv,NULL);

	nMillSec = (long long)tv.tv_sec * 1000;
	cout << "nMIllSec1 = " << nMillSec << endl;

	nMillSec += tv.tv_usec / 1000;
	cout << "nMIllSec2 = " << nMillSec << endl;

	return nMillSec;
}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics