![]() |
|
Is clock() really not giving the correct time? |
Albin Engström
Member #8,110
December 2006
![]() |
In the thread about multi-threading GullRaDriel said that the clock() function dosen't return the actual time spent since the application started. Let me get this clear: Sorry I'm making a thread about this but I felt it was necessary. Thank you. |
Evert
Member #794
November 2000
![]() |
Quote: 1: Is this clock() function found in clock.h identical to the one in time.h? It's in the standard library; where you get the prototype from (it should be time.h) doesn't matter. Quote:
2: Did he mean that clock returns the number of vibrations the "oscillator" (whatever that crystal thing is actually called) had performed and not "real time". I think that's best answered by a quote from the man page; I leave it up to you to interpret that: Quote:
The clock() function determines the amount of processor time used since
|
Arthur Kalliokoski
Second in Command
February 2005
![]() |
In other words, if you started two windowed "clock displays" on your desktop at the same time, and they used clock(), they'd both run half speed. That's assuming no other processes used any cpu time. If your top(1) or TaskManager said that one of the clocks used 30% cpu time, and you'd started the "clock" at 1:00, then at wall time of 1:10 your "clock" would say 1:03. They all watch too much MSNBC... they get ideas. |
Albin Engström
Member #8,110
December 2006
![]() |
Evert said: It's in the standard library; where you get the prototype from (it should be time.h) doesn't matter. That's oen thing that bothers me, I don't have the clock.h header, is it a part of a linux/mac only? Quote:
The clock() function determines the amount of processor time used since Interesting words.. however, I like to inteprent them as that clock returns the amount of time since the process started, what I initially thought. Arthur Kalliokoski said: In other words, if you started two windowed "clock displays" on your desktop at the same time, and they used clock(), they'd both run half speed. That's assuming no other processes used any cpu time. If your top(1) or TaskManager said that one of the clocks used 30% cpu time, and you'd started the "clock" at 1:00, then at wall time of 1:10 your "clock" would say 1:03. Have you tried doing that? Beacuse when I do it with 5 "timers" they all run fine, I can even use sleep and they'll gladly jump 10000s of "clocks" to get to the correct time.
|
Elias
Member #358
May 2000
|
Sounds like either your clock() or sleep() implementation is broken then. Usually sleep() should not take CPU time. [Edit:]
I get: 1 second sleep: 0.000000 1 billion adds: 2.610000
-- |
Albin Engström
Member #8,110
December 2006
![]() |
Sorry, i was using Sleep(). I don't even have sleep, are you using linux? Anyway. Here's the code:
One thing I noticed about your code is that sleep(1) sleeps for one millisecond and not one second. (if sleep acts like Sleep). When I change it to Sleep(1000), the value beacomes 1 and not 0.0 |
tobing
Member #5,213
November 2004
![]() |
Sleep is Windows, sleep is linux/unix. Time is measured in milliseconds. Remember, sleep (or Sleep) are not guaranteed to be exact, I think it is like 'at least' the given number of milliseconds. The sleep functions measure elapsed time. On linux/unix, clock measures process time. On Windows, there's no difference between process time and elapsed time, so that's a big difference between linux and Windows. Then, last but not least, timing on Windows is quite far from exact... |
Albin Engström
Member #8,110
December 2006
![]() |
I see, that's good to know when I port my games to linux. But how do you get the elapsed time on linux? |
tobing
Member #5,213
November 2004
![]() |
There are functions besides clock(), like _ftime (that's the Windows version of that function), try to look what you have in time.h and look in the man pages or other help of those functions. I don't have linux myself, so I can't tell exactly what the right function names are. |
Arthur Kalliokoski
Second in Command
February 2005
![]() |
Quote: But how do you get the elapsed time on linux? They all watch too much MSNBC... they get ideas. |
Albin Engström
Member #8,110
December 2006
![]() |
Thanks! |
Evert
Member #794
November 2000
![]() |
Quote: That's oen thing that bothers me, I don't have the clock.h header, is it a part of a linux/mac only? No idea, but it seems that the standard header you should be using is time.h, so I don't see why it would matter one way or the other whether you have a "clock.h" or not. EDIT: I do seem to have a "clock.h", which judging from browsing through it very quickly, seems to be a kernel-interface header file. |
Thomas Fjellstrom
Member #476
June 2000
![]() |
Quote: Interesting words.. however, I like to inteprent them as that clock returns the amount of time since the process started, what I initially thought. But a program is not running all the time. What clock returns is actual used cpu time. Programs share the cpu with other processes, so they are not running every ms that it appears they are. clock will not let you keep track of ACTUAL time the program has been running. -- |
Albin Engström
Member #8,110
December 2006
![]() |
Evert said: No idea, but it seems that the standard header you should be using is time.h, so I don't see why it would matter one way or the other whether you have a "clock.h" or not. I just like to know where I'm standing. Thomas Fjellstrom said: But a program is not running all the time. What clock returns is actual used cpu time. Programs share the cpu with other processes, so they are not running every ms that it appears they are. clock will not let you keep track of ACTUAL time the program has been running. I think we've established that it is true on linux but not on windows. And it certenly dosen't seem that way on windows when I use it to time multiple applications(see above for code). |
Thomas Fjellstrom
Member #476
June 2000
![]() |
Quote: I think we've established that it is true on linux but not on windows. Use a different sleep then. One that doesn't actually just while(1); besides, clock isn't particularly reliable, as you can tell. -- |
Speedo
Member #9,783
May 2008
|
Quote: Use a different sleep then. One that doesn't actually just while(1); besides, clock isn't particularly reliable, as you can tell. His Sleep function is fine. As was already stated clock behaves differently on windows and *nix. Run-Time Library Reference |
GullRaDriel
Member #3,861
September 2003
![]() |
Albin, want some timing related topic ? QPC, GTOD, RDTSC is a topic I started a while ago for compiling high performance timing ressources coming from various people. Wall Clock Time explanation. I admit I was not enough precise when telling that clock() isn't returning the right amount of time. In fact, it's returning the "Wall Clock Time" under Windows, and it's returning the processor usage duration under linux && unix likes, plus that under Linux it does not return the cumulated children consumption. Example:
That code output "10 seconds" under windows, and 0 under our "SunOS mercure 5.10 Generic_127127-11 sun4u sparc SUNW,Sun-Fire-280R" "Code is like shit - it only smells if it is not yours" |
Thomas Fjellstrom
Member #476
June 2000
![]() |
Quote: His Sleep function is fine. As was already stated clock behaves differently on windows and *nix. Sleep != sleep, and most likely windows's clock function is broken. Wouldn't be the first one. Quote:
RETURN VALUE CONFORMING TO NOTES Note that the time can wrap around. On a 32-bit system where CLOCKS_PER_SEC equals 1000000 this function will return the same value On several other implementations, the value returned by clock() also includes the times of any children whose status has been col‐ CPU time does not include time spent not running. -- |
Albin Engström
Member #8,110
December 2006
![]() |
GullRaDriel said: Albin, want some timing related topic ? QPC, GTOD, RDTSC is a topic I started a while ago for compiling high performance timing ressources coming from various people. Wall Clock Time [en.wikipedia.org] explanation. I admit I was not enough precise when telling that clock() isn't returning the right amount of time. In fact, it's returning the "Wall Clock Time" under Windows, and it's returning the processor usage duration under linux && unix likes, plus that under Linux it does not return the cumulated children consumption. Thanks for the links, they'll come in handy now that I have to update my own timing code. "cumulated children consumption."? Does this have something to do with multithreading? Thanks |
GullRaDriel
Member #3,861
September 2003
![]() |
Albin said: Does this have something to do with multithreading? The documentation says so. You'll only have the main program clock consumption, and if you want the total you should make yourself a way to clock each child/thread and send the value back to the main program (and sum them). "Code is like shit - it only smells if it is not yours" |
|