1

I have the following code:

clock_t tt = clock();
sleep(10);
tt = clock()-tt;
cout<<(float)tt/CLOCKS_PER_SEC<<" "<<CLOCKS_PER_SEC<<endl;

When I run the code, it apparently pauses for 10 seconds and the output is:

0.001074 1000000

This indicates it passed 1074 clock ticks and 1ms, which is apparently false.

Why does this happen?

I am using g++ under linux.

Viktor Haag
  • 3,363
  • 1
  • 18
  • 21
Coderzelf
  • 752
  • 3
  • 10
  • 26
  • 2
    You are using C++ features, is this a C or a C++ question? Why does it have the C tag? – Sergey L. Jun 18 '14 at 18:21
  • You might want to understand how the `clock()` function actually works. See [this answer](http://stackoverflow.com/a/10456013/1911388). – 0xbe5077ed Jun 18 '14 at 18:29
  • Did you read the man page for [clock](http://linux.die.net/man/3/clock), specifically the description. – David Schwartz Jun 18 '14 at 18:30
  • 1
    You might be looking for any of `time()`, `localtime()`, `gmtime()`, `gettimeofday()`, or a few other similar functions that give you wall-clock time, not CPU time used... – twalberg Jun 18 '14 at 18:43
  • @twalberg Thanks, gettimeofday() seems working – Coderzelf Jun 19 '14 at 21:33

2 Answers2

4

The function clocks returns the processor time consumed by the program. While sleeping, your process does not use any amount of processing, so this is expected. The amount of time your program is showing could be from the clock function calling.

Yamaguti
  • 146
  • 4
  • Thank you, but actually I want to find a way to time milliseconds, but the clock() seems work poorly, where I didn't use sleep() at all. – Coderzelf Jun 18 '14 at 18:33
  • If you are looking for accurate ways of measuring time consume of your program, it really depends on your application and operating system. I suggest you to look at this [question](http://stackoverflow.com/questions/16764276/measuring-time-in-millisecond-precision) given your operating system – Yamaguti Jun 18 '14 at 18:37
  • @Coderzelf: You want to time milliseconds -- but milliseconds of what? Real time as measured by a wall clock, or CPU time (which is nearly zero during a `sleep()` call)? – Keith Thompson Jun 18 '14 at 19:43
  • @KeithThompson I just want to know in runtime who many milliseconds were passed, maybe I used sleep() incorrectly, but in real work, I just want to time some real operations – Coderzelf Jun 19 '14 at 01:57
  • @Coderzelf: Do you mean milliseconds of real time? `clock()` measures CPU time. `sleep(10)` will take 10,000 milliseconds of real time and very few milliseconds of CPU time. Do you understand that? – Keith Thompson Jun 19 '14 at 02:02
  • @KeithThompson I just realized from the answer above by Yamaguti – Coderzelf Jun 19 '14 at 14:02
2

clock() doesn't measure elapsed time (what you would measure with a stopwatch), it measures the time spent by your program running on the CPU. But sleep() almost don't use any CPU, it simply makes your process going to sleep. Try to modify sleep(10) by any other value sleep(1)for example, and you will get the same result.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69