Member Avatar for I_m_rude

hi.. can anybody please tell me that how can i print the execution time for my code ? Actually, what exaclty i want is that my program should also print the time which is elapsed while executing it or for maupulating any i/p. any pre-defined or user-defind function which can help, then please tell me. thanks. any help will be apprecited. thanks.

You could use the clock() function in the time.h header. The 0 value has nothing to do with real world time (it can be based on the time the program started, but is implementation specific). It gives you the number of clock cycles (ticks) that have ellapsed, so you have to divide by CLOCKS_PER_SEC to get it in seconds.

Basically, get the return value at the beginning of your program and at the end, and use the difference to show execution time.

Member Avatar for I_m_rude

hey, Can you please give me a code snippet here just to show the hint of exaclty what you are trying to say ? thanks in advance to you.

Oh, why not:

int i;
clock_t ticks;

ticks = clock();
for(i = 0; i < 100000000; i++);
ticks = clock() - ticks;

printf("Number of ticks:   %ld\n", ticks);
printf("Clocks per second: %ld\n", CLOCKS_PER_SEC);
printf("Time ellapsed:     %ld ms\n", ticks * 1000 / CLOCKS_PER_SEC));
getchar();

Note, I did this with a C++ compiler, but it should work with a C compiler (don't have a C compiler to test it with). Also note that the order of operations in the last printf statement are very important. Reversing the 1000 and CLOCKS_PER_SEC would give me a result of 0ms instead of 235ms (in my case, where there were ticks=235 and CLOCK_PER_SEC=1000), since this is integer arithmetic.

Member Avatar for I_m_rude

but if there is any i/p then it will wait for the i/p and that time will be included here also. so is there any way so as to delete tha time for which it has waited to get i/p ?

I'm not sure what you mean by i/p... but if you want to not include a certain amount of time while something else is happening you could try this:

clock_t ticks;
clock_t ticks_temp;

ticks = clock();

...

ticks_temp = clock();
...
/* don't time this */
...
ticks -= (clock() - ticks_temp);

...

ticks = clock() - ticks;
/* print the time */
Member Avatar for I_m_rude

i/p means input and o/p means output here. also , can i use float data type instead of clock_t ? why have u used long int instead of float as time will be better represtned in float. thanks

Oh ok, I've only ever heard the term I/O where I've worked. In either case, yes you could cast it to a floating point number. Personally, I try not to use them unless I need them, but there's no problem with using them.

Member Avatar for I_m_rude

what is problem ? can you specify ?

what is problem ? can you specify ?

I didn't say there was a problem.

Member Avatar for I_m_rude

thank you for his precious help.

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.