C + + 在 Linux 上获取毫秒时间——时钟()似乎不能正常工作

在 Windows 上,clock()以毫秒为单位返回时间,但在我正在研究的 Linux 机器上,它将时间四舍五入到最接近的1000,所以精度只能达到“第二”级别,而不是毫秒级别。

我使用 QTime类找到了一个使用 Qt 的解决方案,实例化一个对象并在其上调用 start(),然后调用 elapsed()以获得所经过的毫秒数。

我有点幸运,因为我是从 Qt 开始工作的,但我想要一个不依赖于第三方库的解决方案,

没有标准的方法吗?

更新

请不要推荐 Boost. 。

如果 Boost 和 Qt 可以做到这一点,那么肯定不是魔术,一定有他们正在使用的标准!

271474 次浏览

clock() doesn't return milliseconds or seconds on linux. Usually clock() returns microseconds on a linux system. The proper way to interpret the value returned by clock() is to divide it by CLOCKS_PER_SEC to figure out how much time has passed.

In the POSIX standard clock has its return value defined in terms of the CLOCKS_PER_SEC symbol and an implementation is free to define this in any convenient fashion. Under Linux, I have had good luck with the times() function.

You could use gettimeofday at the start and end of your method and then difference the two return structs. You'll get a structure like the following:

struct timeval {
time_t tv_sec;
suseconds_t tv_usec;
}

EDIT: As the two comments below suggest, clock_gettime(CLOCK_MONOTONIC) is a much better choice if you have it available, which should be almost everywhere these days.

EDIT: Someone else commented that you can also use modern C++ with std::chrono::high_resolution_clock, but that isn't guaranteed to be monotonic. Use steady_clock instead.

I prefer the Boost Timer library for its simplicity, but if you don't want to use third-parrty libraries, using clock() seems reasonable.

This should work...tested on a mac...

#include <stdio.h>
#include <sys/time.h>


int main() {
struct timeval tv;
struct timezone tz;
struct tm *tm;
gettimeofday(&tv,&tz);
tm=localtime(&tv.tv_sec);
printf("StartTime: %d:%02d:%02d %d \n", tm->tm_hour, tm->tm_min, tm->tm_sec, tv.tv_usec);
}

Yeah...run it twice and subtract...

#include <sys/time.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
struct timeval start, end;


long mtime, seconds, useconds;


gettimeofday(&start, NULL);
usleep(2000);
gettimeofday(&end, NULL);


seconds  = end.tv_sec  - start.tv_sec;
useconds = end.tv_usec - start.tv_usec;


mtime = ((seconds) * 1000 + useconds/1000.0) + 0.5;


printf("Elapsed time: %ld milliseconds\n", mtime);


return 0;
}

I also recommend the tools offered by Boost. Either the mentioned Boost Timer, or hack something out of Boost.DateTime or there is new proposed library in the sandbox - Boost.Chrono: This last one will be a replacement for the Timer and will feature:

  • The C++0x Standard Library's time utilities, including:
    • Class template duration
    • Class template time_point
    • Clocks:
      • system_clock
      • monotonic_clock
      • high_resolution_clock
  • Class template timer, with typedefs:
    • system_timer
    • monotonic_timer
    • high_resolution_timer
  • Process clocks and timers:
    • process_clock, capturing real, user-CPU, and system-CPU times.
    • process_timer, capturing elapsed real, user-CPU, and system-CPU times.
    • run_timer, convenient reporting of |process_timer| results.
  • The C++0x Standard Library's compile-time rational arithmetic.

Here is the source of the feature list

If you don't need the code to be portable to old unices, you can use clock_gettime(), which will give you the time in nanoseconds (if your processor supports that resolution). It's POSIX, but from 2001.

clock() has a often a pretty lousy resolution. If you want to measure time at the millisecond level, one alternative is to use clock_gettime(), as explained in this question.

(Remember that you need to link with -lrt on Linux).

Please note that clock does not measure wall clock time. That means if your program takes 5 seconds, clock will not measure 5 seconds necessarily, but could more (your program could run multiple threads and so could consume more CPU than real time) or less. It measures an approximation of CPU time used. To see the difference consider this code

#include <iostream>
#include <ctime>
#include <unistd.h>


int main() {
std::clock_t a = std::clock();
sleep(5); // sleep 5s
std::clock_t b = std::clock();


std::cout << "difference: " << (b - a) << std::endl;
return 0;
}

It outputs on my system

$ difference: 0

Because all we did was sleeping and not using any CPU time! However, using gettimeofday we get what we want (?)

#include <iostream>
#include <ctime>
#include <unistd.h>
#include <sys/time.h>


int main() {
timeval a;
timeval b;


gettimeofday(&a, 0);
sleep(5); // sleep 5s
gettimeofday(&b, 0);


std::cout << "difference: " << (b.tv_sec - a.tv_sec) << std::endl;
return 0;
}

Outputs on my system

$ difference: 5

If you need more precision but want to get CPU time, then you can consider using the getrusage function.

As an update,appears that on windows clock() measures wall clock time (with CLOCKS_PER_SEC precision)

 http://msdn.microsoft.com/en-us/library/4e2ess30(VS.71).aspx

while on Linux it measures cpu time across cores used by current process

http://www.manpagez.com/man/3/clock

and (it appears, and as noted by the original poster) actually with less precision than CLOCKS_PER_SEC, though maybe this depends on the specific version of Linux.

I've written a Timer class based on CTT's answer. It can be used in the following way:

Timer timer = Timer();
timer.start();
/* perform task */
double duration = timer.stop();
timer.printTime(duration);

Here is its implementation:

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


class Timer {
private:


timeval startTime;


public:


void start(){
gettimeofday(&startTime, NULL);
}


double stop(){
timeval endTime;
long seconds, useconds;
double duration;


gettimeofday(&endTime, NULL);


seconds  = endTime.tv_sec  - startTime.tv_sec;
useconds = endTime.tv_usec - startTime.tv_usec;


duration = seconds + useconds/1000000.0;


return duration;
}


static void printTime(double duration){
printf("%5.6f seconds\n", duration);
}
};

gettimeofday - the problem is that will can have lower values if you change you hardware clock (with NTP for example) Boost - not available for this project clock() - usually returns a 4 bytes integer, wich means that its a low capacity, and after some time it returns negative numbers.

I prefer to create my own class and update each 10 miliseconds, so this way is more flexible, and I can even improve it to have subscribers.

class MyAlarm {
static int64_t tiempo;
static bool running;
public:
static int64_t getTime() {return tiempo;};
static void callback( int sig){
if(running){
tiempo+=10L;
}
}
static void run(){ running = true;}
};


int64_t MyAlarm::tiempo = 0L;
bool MyAlarm::running = false;

to refresh it I use setitimer:

int main(){
struct sigaction sa;
struct itimerval timer;


MyAlarm::run();
memset (&sa, 0, sizeof (sa));
sa.sa_handler = &MyAlarm::callback;


sigaction (SIGALRM, &sa, NULL);




timer.it_value.tv_sec = 0;
timer.it_value.tv_usec = 10000;






timer.it_interval.tv_sec = 0;
timer.it_interval.tv_usec = 10000;




setitimer (ITIMER_REAL, &timer, NULL);
.....

Look at setitimer and the ITIMER_VIRTUAL and ITIMER_REAL.

Don't use the alarm or ualarm functions, you will have low precision when your process get a hard work.

I like the Hola Soy method of not using gettimeofday(). It happened to me on a running server the admin changed the timezone. The clock was updated to show the same (correct) local value. This caused the function time() and gettimeofday() to shift 2 hours and all timestamps in some services got stuck.

I wrote a C++ class using timeb.

#include <sys/timeb.h>
class msTimer
{
public:
msTimer();
void restart();
float elapsedMs();
private:
timeb t_start;
};

Member functions:

msTimer::msTimer()
{
restart();
}


void msTimer::restart()
{
ftime(&t_start);
}


float msTimer::elapsedMs()
{
timeb t_now;
ftime(&t_now);
return (float)(t_now.time - t_start.time) * 1000.0f +
(float)(t_now.millitm - t_start.millitm);
}

Example of use:

#include <cstdlib>
#include <iostream>


using namespace std;


int main(int argc, char** argv)
{
msTimer t;
for (int i = 0; i < 5000000; i++)
;
std::cout << t.elapsedMs() << endl;
return 0;
}

Output on my computer is '19'. Accuracy of the msTimer class is of the order of milliseconds. In the usage example above, the total time of execution taken up by the for-loop is tracked. This time included the operating system switching in and out the execution context of main() due to multitasking.

With C++11 and std::chrono::high_resolution_clock you can do this:

#include <iostream>
#include <chrono>
#include <thread>
typedef std::chrono::high_resolution_clock Clock;


int main()
{
std::chrono::milliseconds three_milliseconds{3};


auto t1 = Clock::now();
std::this_thread::sleep_for(three_milliseconds);
auto t2 = Clock::now();


std::cout << "Delta t2-t1: "
<< std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count()
<< " milliseconds" << std::endl;
}

Output:

Delta t2-t1: 3 milliseconds

Link to demo: http://cpp.sh/2zdtu