fivemack: (Default)
Tom Womack ([personal profile] fivemack) wrote2006-09-12 11:15 am
Entry tags:

Two processors, two clocks

At work, I've the good fortune of having a two-CPU workstation on my desk.

Unfortunately, whilst the two CPUs are within a centimetre of one another on the same piece of silicon, they appear to maintain independent clocks running with a noticeable offset; I can't tell if the speeds are also different.

In any case,

a = clock()
multi_threaded_operation()
b = clock()


can leave b reading out as several seconds before a, if the main thread was initially on the core with the later clock and got rescheduled onto the other one after the operation. This is not helpful for seeing which things are actually faster than which others.

Is there a standard C library routine, or at least something in <sys/*.h>, guaranteed to read a clock of a kind such that I can be reasonably confident that the computer's got only one?
ext_8103: (Default)

[identity profile] ewx.livejournal.com 2006-09-12 10:26 am (UTC)(link)
clock() returns "an approximation of processor time used by the program". If you wanted something that was actually a time then try gettimeofday().

[identity profile] randwolf.livejournal.com 2006-09-12 10:44 am (UTC)(link)
I think you want time(3) or gettimeofday(2); clock() is processor time, which differs for different threads. Also, beware of reentrance--you may be seeing a problem because you need to use an _r function somewhere. Better yet, use processes with shared memory regions, rather than threads.
sparrowsion: photo of male house sparrow (string-handling kitten)

[personal profile] sparrowsion 2006-09-12 11:17 am (UTC)(link)
This reminded me of this recent thread on comp.lang.python—I'm not sure if the underlying problems are relevant to your case or not.