我想知道 C 中是否有与 PHP 函数 microtime() 等效的函数andC++。我环顾四周,但找不到明确的答案。
Thanks!
没有与 PHP 的 microtime() 完全相同的函数,但您可以基于以下代码创建具有类似功能的函数:
Mac OS X 或许还有 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;
(基于: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);
(达卡拉的回答,来自:https://stackoverflow.com/a/4568649/330067 https://stackoverflow.com/a/4568649/330067)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)