I am wondering if there is an equivalent to the PHP function microtime() in C and C++. I looked around but couldn't find a definitive answer.
我想知道在C和c++中是否有一个等价于PHP函数microtime()的函数。我环顾四周,却找不到一个明确的答案。
Thanks!
谢谢!
5 个解决方案
#1
5
On Linux, you can use gettimeofday, which should give the same information. In fact, I believe that is the function that PHP uses under the covers.
在Linux上,您可以使用gettimeofday,它应该提供相同的信息。事实上,我相信这是PHP在幕后使用的函数。
#2
8
There is no exact equivalent to PHP's microtime(), but you could a function with a similar functionality based on the following code:
与PHP的microtime()没有完全相同的功能,但是您可以使用基于以下代码的类似功能的函数:
Mac OS X and probably also Linux/Unix
#include <sys/time.h>
struct timeval time;
gettimeofday(&time, NULL); #This actually returns a struct that has microsecond precision.
long microsec = ((unsigned long long)time.tv_sec * 1000000) + time.tv_usec;
(based on: http://brian.pontarelli.com/2009/01/05/getting-the-current-system-time-in-milliseconds-with-c/)
(基于:http://brian.pontarelli.com/2009/01/05/getting-the-current-system-time-in-milliseconds-with-c/)
Windows:
unsigned __int64 freq;
QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
double timerFrequency = (1.0/freq);
unsigned __int64 startTime;
QueryPerformanceCounter((LARGE_INTEGER *)&startTime);
//do something...
unsigned __int64 endTime;
QueryPerformanceCounter((LARGE_INTEGER *)&endTime);
double timeDifferenceInMilliseconds = ((endTime-startTime) * timerFrequency);
(answer by Darcara, from: https://*.com/a/4568649/330067)
(达卡拉回答:https://*.com/a/4568649/330067)
#3
3
C++11 added some standard timekeeping functions (see section 20.11 "Time utilities") with good accuracy, but most compilers don't support those yet.
c++ 11增加了一些标准的计时功能(参见20.11节“时间实用程序”),非常准确,但是大多数编译器还不支持这些功能。
Mostly you need to use your OS API, such as gettimeofday
for POSIX.
大多数情况下,您需要使用OS API,比如POSIX的gettimeofday。
#4
1
For timing sections of code, try std::clock, which returns ticks, then divide by CLOCKS_PER_SEC
.
对于代码的计时段,请尝试std::clock,它返回节拍,然后除以CLOCKS_PER_SEC。
#5
1
libUTP (uTorrent Transport Protocol library) has a good example on getting the microtime on different platforms.
libUTP (uTorrent传输协议库)是在不同平台上获得微时间的一个很好的例子。
#1
5
On Linux, you can use gettimeofday, which should give the same information. In fact, I believe that is the function that PHP uses under the covers.
在Linux上,您可以使用gettimeofday,它应该提供相同的信息。事实上,我相信这是PHP在幕后使用的函数。
#2
8
There is no exact equivalent to PHP's microtime(), but you could a function with a similar functionality based on the following code:
与PHP的microtime()没有完全相同的功能,但是您可以使用基于以下代码的类似功能的函数:
Mac OS X and probably also Linux/Unix
#include <sys/time.h>
struct timeval time;
gettimeofday(&time, NULL); #This actually returns a struct that has microsecond precision.
long microsec = ((unsigned long long)time.tv_sec * 1000000) + time.tv_usec;
(based on: http://brian.pontarelli.com/2009/01/05/getting-the-current-system-time-in-milliseconds-with-c/)
(基于:http://brian.pontarelli.com/2009/01/05/getting-the-current-system-time-in-milliseconds-with-c/)
Windows:
unsigned __int64 freq;
QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
double timerFrequency = (1.0/freq);
unsigned __int64 startTime;
QueryPerformanceCounter((LARGE_INTEGER *)&startTime);
//do something...
unsigned __int64 endTime;
QueryPerformanceCounter((LARGE_INTEGER *)&endTime);
double timeDifferenceInMilliseconds = ((endTime-startTime) * timerFrequency);
(answer by Darcara, from: https://*.com/a/4568649/330067)
(达卡拉回答:https://*.com/a/4568649/330067)
#3
3
C++11 added some standard timekeeping functions (see section 20.11 "Time utilities") with good accuracy, but most compilers don't support those yet.
c++ 11增加了一些标准的计时功能(参见20.11节“时间实用程序”),非常准确,但是大多数编译器还不支持这些功能。
Mostly you need to use your OS API, such as gettimeofday
for POSIX.
大多数情况下,您需要使用OS API,比如POSIX的gettimeofday。
#4
1
For timing sections of code, try std::clock, which returns ticks, then divide by CLOCKS_PER_SEC
.
对于代码的计时段,请尝试std::clock,它返回节拍,然后除以CLOCKS_PER_SEC。
#5
1
libUTP (uTorrent Transport Protocol library) has a good example on getting the microtime on different platforms.
libUTP (uTorrent传输协议库)是在不同平台上获得微时间的一个很好的例子。