我正在尝试使用time()来衡量我的程序的各个点.
我不明白为什么之前和之后的值是一样的?我知道这不是描述我的程序的最佳方式,我只想看看有多长时间.
printf("**MyProgram::before time= %ld\n", time(NULL));
doSomthing();
doSomthingLong();
printf("**MyProgram::after time= %ld\n", time(NULL));
Run Code Online (Sandbox Code Playgroud)
我试过了:
struct timeval diff, startTV, endTV;
gettimeofday(&startTV, NULL);
doSomething();
doSomethingLong();
gettimeofday(&endTV, NULL);
timersub(&endTV, &startTV, &diff);
printf("**time taken = %ld %ld\n", diff.tv_sec, diff.tv_usec);
Run Code Online (Sandbox Code Playgroud)
我如何阅读结果**time taken = 0 26339?这是否意味着26,339纳秒= 26.3毫秒?
那怎么说**time taken = 4 45025,这意味着4秒和25毫秒?
更新:代码现在正确编译
我想计算两个时间戳之间的时差.分辨率很重要,因此必须以微秒/毫秒为单位.
我尝试了以下但结果没有意义:
boost::posix_time::ptime before = (&input[0])->timestamp;
boost::posix_time::ptime now = boost::posix_time::microsec_clock::local_time();
boost::posix_time::time_period tp (before, now);
std::string str (boost::posix_time::to_simple_string (tp));
cout << str.c_str() << endl;
Run Code Online (Sandbox Code Playgroud)
我得到的结果如下:
[2014-Jun-20 12:26:07.711182/2014-Jun-20 12:26:07.711596]
Run Code Online (Sandbox Code Playgroud)
我怎样才能获得以下内容?
76 ?s
Run Code Online (Sandbox Code Playgroud)