Hi

I want to know how we can measure C execution time by seeing the clock cycles generated from the assembly code.

Thanks...

Recommended Answers

All 4 Replies

Hi

I want to know how we can measure C execution time by seeing the clock cycles generated from the assembly code.

Thanks...

I cannot assure you of this method but it had worked for me.The time.h header file has some variables as CLK_TCK and a data type which you can use to store these values into you can try your purpose by calling clock() function at two instances and the difference in time may serve what you expect.

>The time.h header file has some variables as CLK_TCK [...]
Note that CLK_TCK is not standard. CLOCKS_PER_SEC is, and on the implementations I'm aware of where CLK_TCK is defined, the two are equivalent. Thus, you should use CLOCKS_PER_SEC.

>calling clock() function at two instances and the
>difference in time may serve what you expect.
An actual example might help as well:

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

int main ( void )
{
  clock_t start = clock();
  long i;

  for ( i = 0; i < 10000000; i++ )
    rand();

  printf ( "%f\n", ( (double)clock() - start ) / CLOCKS_PER_SEC );

  return 0;
}

Note that this particular technique isn't strictly portable, but the chances you'll see an implementation where it fails are pretty slim in practice.

There are better ways, such as using implementation-specific functions instead of hacking with the standard library, or better yet using a profiler.

CLK_TCK is limited to turbo c compiler and if I am not worng even to gcc compiler.

I would like to add up a question over here pertaining to this topic.

In the above mentioned codes we are using the clock functions to measure the time taken for a part of the source code we write. Is it possible to calculate the time required "from the call of the main()" till the end.

>Is it possible to calculate the time required "from the call of the main()" till the end.
I assume you mean besides extending my example to cover the whole of main's body. The answer is yes, and I already mentioned it: use a profiler.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.