Android NDK 开发在测试接口性能 使用gettimeofday,发现获取系统时间的函数输出的是一个负数。

long getCurrentMillisecondCount()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}
LOGE("millisecond:%ld\n",getCurrentMillisecondCount()); //毫秒

感觉可能是溢出 tv.tv_sec 是long 型*1000

修正:

long long getCurrentMillisecondCount()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (long long)tv.tv_sec * 1000 + tv.tv_usec / 1000;
}
LOGE("millisecond:%ld\n",getCurrentMillisecondCount()); //毫秒

ok 解决。