Is time.h clock() broken on my hardware?


I try to measure the clock cyles needed to execute a piece of code on the TMS32064x+ DSP that comes with the OMAP ZOOM 3430 MDK. I look at the "Programmer's Guide" of the DSP chip and it says that the DSP supports the clock() function.

What I do is really simple, I just do

start = clock();
for (i=0;i<100;i++){
    /* do something here */
stop = clock();
total = stop - start;

and then put the values of "start","stop" and "total" to a previously allocated shared memory with the ARM processor. Then I simply print it to the screen at the ARM side.

The problem is, in my first executes, I always get the same "total" value, and then in my next runs I always get 0! The "start" and "stop" values go along with the "total" value.

The strangest thing is that they seem to follow a bit pattern! I put the output below:

# ./sampleapp
Total = 63744
Start clock() value = 0x000000f9
Stop  clock() value = 0x0000f9f9
# ./sampleapp 
Total = 4177526784
Start clock() value = 0x00f9f9f9
Stop  clock() value = 0xf9f9f9f9
# ./sampleapp
Total clock cyles = 0
Start clock() value = 0xf9f9f9f9
Stop  clock() value = 0xf9f9f9f9

Apparantly, clock() is not functioning well, but I'm not sure if this is because of something I do wrong or because this type of thing is not supported with the hardware I have. Any ideas why this might be happening?

asked on Stack Overflow Jul 3, 2009 by Can Bal • edited Aug 22, 2011 by Stephen

6 Answers


From reading the questions so far, I'd say the Original Poster has substantially more knowledge of this matter than the contributors so far, and that the suspicion that the clock() is broken (or not supported, and returns an undefined result) on the DSP seems quite likely.

answered on Stack Overflow Jul 14, 2009 by Will

Curiously, Why do you require a previously allocated shared memory. Why don't you try with a normal stack variable? Is there anything that I am missing?

answered on Stack Overflow Jul 8, 2009 by AIB

Maybe you need to initialize the clock first.

answered on Stack Overflow Jul 8, 2009 by swegi

How are you printing it out? maybe the issue is actually with displaying the result?

on most platforms clock_t is a long long. If you're using printf with %d you might get variable results which is what you're seeing.

answered on Stack Overflow Jul 9, 2009 by hookenz

Assuming the start and end variable are of type 'clock_t', and your shared memory assumes the same on the other end's interpretation of the numbers passed, then your problem is not with the call to clock, and your handleing of the difference between the start end end times.

I believe your problem is in the shared memory between the two. Can you please post code to show how you're sharing memory between two separate processors?

answered on Stack Overflow Jul 10, 2009 by Kieveli

Perhaps you could use some inline assembly to access the CPU's counter registers directly.

The TMS320C64x+ has a 64-bit timestamp register in TSCL, TSCH. The counter is not enabled on reset, you must first write to the register to start the counter (maybe this is the problem with clock?). Reading from the register is not quite trivial as each half must be read with a separate instruction (and you can get interrupts...).

answered on Stack Overflow Aug 22, 2011 by Dietrich Epp

User contributions licensed under CC BY-SA 3.0