Archive for January, 2009

Timing Programs in C (Update)

So I’ve decided to try and do at least one useful post a week to hopefully help a random searcher out. Here is the the updated version of how to get millisecond precise wall clock time in C.

#include <sys\time.h>

gettimeofday(&tstart, NULL);

double t1=tstart.tv_sec+(tstart.tv_usec/1000000.0);

Timing Programs in C (Why I hate clock())

So in working on a parallel programming assignment for a grad class I needed to time two algorithms, a sequential approach and a parallel approach. In doing so I did something like this:

 

clock_t start, finish;

start = clock();

// do something finish = clock();

printf("Time Elapsed: %f", ((double) finish - start) / CLOCKS_PER_SEC);

However this code will give you the wrong time if you are trying to compare the runtime between these two types of programs.

On a sequential algorithm it should produce a good approximation of the wall-clock time, or the amount of time that would expire if you timed it using a clock on the wall (or stopwatch). But in a parallel algorithm we find that it actually gives you a larger time difference.

This is because in a multi-threaded program you are engaging multiple processors (if you have them). Since the clock() gets the number of clock ticks for the program, we get back almost 2x as many (on a two-core machine).

Unfortunately I have yet to find a decent way of getting the milliseconds for a C program accurately. You can use regular old time() to get the seconds, or if you are running on UNIX you can use the time command.

New Theme

Let’s hope there are no major issues. I’ve had some problems with iFrames before and because I’m too lazy to fix it myself I just look for a new one. So far though, this one seems pretty spiffy.

New Caching, Dedicated Memory

I have just enabled page caching on here and soon hope to have dedicated memory from dreamhost, so I should be able to speed up page loads significantly without decimating my wallet.

Return top