This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

clock()

Other Parts Discussed in Thread: OMAP-L137, TMS320C6416

Hi everybody,

I can't understand why I can't get information from clock(). Everithing the value =0.

Here is an example:

-------------------------------------------------------------

#include <stdio.h>

#include <time.h> /* Need time.h in order to call clock() */

int main()

{

clock_t start, stop, overhead;

start = clock(); /* Calculate the overhead of calling clock */

stop = clock(); /* and subtract this amount from the results. */

overhead = stop − start;

start = clock();

/* Function or Code Region to time goes here */

stop = clock();

printf(”cycles: %ld\n”,(long)(stop − start – overhead));

return(0);

}

----------------------------------------------------

What I do wrong? Please help me. Thanks a lot.

  • What is you end goal?  Please note that clock() return processor time (as opposed to wall-clock time) and therefore you will need to do something useful in your code in order to get a value greater than zero.  You may find the following thread useful: http://www.gamedev.net/community/forums/topic.asp?topic_id=397731

  • Mr. Gonzales, thanks a lot for replay.

    Goal - I have some variants of piece of code and want to see which is faster. I think using clock() is the simpliest way.

  • Hello everyone. Still it fails. Guys, maybe someone is successfully using the function clock(). Please share this secret. Thank you.

  • The link I sent previously had a usage example; I am not sure if you saw it, but just wanted to make sure you were aware of it.

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

    int main(int argc, char *argv[])
    {
      int i;
      for (i = 0; i < 10000000; ++i)
        ;
      printf("took: %fs CPU time\n", (double) clock() / CLOCKS_PER_SEC);
      return 0;
    }

  • Just an alternative to using clock (though clock should work), you could try using the CCS profiler, of course this is assuming you are using a C64x+ which I believe to be the case based on your other posts with DM6437. Using the profiler means you just set breakpoints at either end of the code that you want to test and than look at the clock values at either end to see how long it took. You can enable the profiler and view the clock value through the Profile -> Clock -> Enable option and the Profile -> Clock -> View option, with both of these enabled you should see a small clock icon and a number at the bottom right hand corner of CCS, this keeps a count of how many cycles have gone by that you can use to measure code execution time.

  • I was under the impression that the clock() function was not actually implemented in the RTS and that this was a per-user implementation. I'll have to look into this and double-check...

  • TimHarron said:

    I was under the impression that the clock() function was not actually implemented in the RTS and that this was a per-user implementation. I'll have to look into this and double-check...

    Pulled from the Compiler Guide (spru187n):

    The clock function works with the stand-alone simulator (load6x). Used in the load6x environment, clock() returns a cycle accurate count. The clock function returns -1 when used with the HLL debugger.
    The HOSTclock() function called by clock() expects to pull the information from an in-system clock - in the case of the simulator it uses the PC's system clock. This means an implementation on a per-target basis would be necessary.

    Alternatives are to use Bernie's method or, if using BIOS, use the BIOS function CLK_gethtime().

     

  • Dear Mr. Gonzales! Of couse I tested this example and got  -  "took: 0.000000s CPU time". That's why I had to write again. Before using profiler ( I got each time different result) I would like to test my pieces of code and then when I be sure in result use profiler. Unfortunately, now I am not familar with profiler.

  • Igor said:
    Dear Mr. Gonzales! Of couse I tested this example and got  -  "took: 0.000000s CPU time". That's why I had to write again. Before using profiler ( I got each time different result) I would like to test my pieces of code and then when I be sure in result use profiler. Unfortunately, now I am not familar with profiler.

    TimHarron said:
    Alternatives are to use Bernie's method or, if using BIOS, use the BIOS function CLK_gethtime().
    Igor,

    If you are having trouble getting consistent results with the Profiler and you are not using DSP/BIOS there is another alternative I wanted to mention. You can manually utilize the on-chip timer's and use the same basic code shell you presented in your first post. The algorithm would look something like this:

    Initialize Timer configuration register, set period register (0xFFFFFFFF?)
    Reset Count register
    Start timer
    Stop timer
    Measure difference (overhead)

    Start Timer
    Run algorithm
    Stop Timer
    Measure difference, minus overhead
  • Dear friends!

    Many Thanks to all.

    The best of the best of the best regards.

    Igor.

  • Igor,

    Glad to help out. Let us know if you run into any problems with this (and if you get a working code snippet feel free to share with the rest of the community!).

  • Hi there,

    This is good information. However, I've a few questions to ask that I believe will benefit everybody in this forum. Great work TI!!

    I'm using OMAP-L137. From the DSP/BIOS Config, it said that "Board Clock in KHz (Informational Only)" with value 20000 whereas "DSP Speed in MHz (CLKOUT)" is 300.0000.

    The question is, the CLOCKS_PER_SEC should be related to 20000 KHz or 300 MHz ? I know, it is already defined in time.h (in mine) as 200000000 (for 200 MHz) but mine is 300 MHz. Weird. Should I change to 300000000 (for 300 MHz) by myself?

    Thanks in advance.

    Best regards,

    Rizuan

  • Juan Gonzales said:

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

    ....................
      printf("took: %fs CPU time\n", (double) clock() / CLOCKS_PER_SEC);

    Hi,

    I've followed exactly this line for my board, OMAP-L137 (C6747), and I've got 0, even before subtracting the overhead, the value return by (double)clock() is 0.

    I've faced this problem before when I was using TMS320C6416 board to profile my code. Hope someone can give a solution for others as well.

    Best regards,

    Rizuan

  • RIZUAN said:

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

    ....................
      printf("took: %fs CPU time\n", (double) clock() / CLOCKS_PER_SEC);

    Hi,

    I've followed exactly this line for my board, OMAP-L137 (C6747), and I've got 0, even before subtracting the overhead, the value return by (double)clock() is 0.[/quote]As I mentioned in a post somewhere below Juan's suggestion you quoted the clock() function is not implemented for any target hardware. This function will not work if used outside of a simulator environment. The C clock() function requires some sort of RTC which it reads and returns to the calling function. Because most (all?) of the DSP devices do not have a battery-powered RTC internal to the device this function cannot work without external hardware.

    As an alternative, DSP/BIOS provides the CLK_getltime() function which returns a relative "time" value based on clock cycles instead of the time of day. These clock cycles are typically a little more precise in any case. Please go back over the trail in this thread and let us know if you have any trouble implementing this scenario.