hi,

Can any one help me about clock problem..i need to calculate the exact execution time of algorithm using the processor clock speed not the wall clock time..i am getting zero by using the clock_t function in time.h.also how can i calculate the exact processor speed execution of program..reply me as soon as possible.

for example
#include<stdio.h>
#include<time.h>
int main()
{
clock_t start,end;
start=clock();
bubblesort(array,arraysize);
end=clock();
printf("the clock speed: %f",start-end);
getch();

}

Dani AI

Generated

asked for "processor" execution time and tried the simple difference — both lines of thought are fine, but a few practical points are missing.

The C standard timing call returns CPU-time in implementation-defined ticks, not seconds, and small measurements can appear as zero when the timer resolution is coarser than the measured run or when the optimizer removes work. Convert tick differences to seconds (use the standard tick rate macro) and make the measured work observable (or disable optimization) or run it many times and average the result. See the C reference for the standard timer: clock() reference.

For finer, reliable CPU-time on POSIX systems use a clock API that gives nanosecond resolution for the process, for example the process-CPU clock. Typical pattern:

struct timespec t0, t1;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t0);
/* do the real work that must not be optimized away */
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t1);
double seconds = (t1.tv_sec - t0.tv_sec) + (t1.tv_nsec - t0.tv_nsec) / 1e9;

See the POSIX man page: clock_gettime.

On Windows, prefer GetProcessTimes for CPU-time or QueryPerformanceCounter for high-resolution wall-clock timing; both are documented by Microsoft. Cycle counters (RDTSC/rdtscp) can give cycle counts but require careful serialization and consideration of frequency scaling and multi-core issues, so they are best used only when those hardware behaviors are understood.

Practical checklist: ensure measured work is large enough, avoid compiler optimizations that remove it, run many iterations and average, and pick the timer API that matches whether you need CPU time or wall time.

your code: " printf("the clock speed: %f",start-end); "

Hey dude, change to: end - start to obtain time in miliseconds
or (end - start) / 1000.0 for time in seconds.miliseconds

(and don't forget to declare double start, end; ) ;)

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.