Invocation delay of using a local function in parent vs. forked child process? C

I'm having a tough time producing a scenario that will display a time difference between invocation of a simple local function vs. the same function but within a child process. The particular function I'm testing shows no difference (time = 0) for the difftime(x,y) function for both the local function and the child process. Can someone generalize potential time differences in these two methods of doing the same thing?

As a side note, I tried doing each task 1000 times and then dividing time by 1000, but even then the time returned is 0. For example, I have:

time(&start);    
for(int i = 0; i<1000; i++){
  reply[i] = my_channel.send_request("hello");
}
time(&end);
time_req_1 = difftime(end,start);

And time returned for "time_req_1" = 0. I only put this code segment in just in case I've done something incorrect.

Answers


You have to remember that the resolution for time is in seconds. A common way to time e.g. a function call is to use clock:

clock_t start_time = clock();

some_lengthy_function_call();

clock_t end_time = clock();

printf("Elapsed time: %.02f seconds\n",
       (double) (end_time - start_time) / CLOCKS_PER_SEC);

Edit: If you want even better accuracy and resolution than clock can provide, see the comment by Jonathan Leffler to your question.


On most machines with most OS's, time is only accurate to one second. For only 1000 iterations, you'll need to find a more accurate time function on your architecture.

A relatively portable interface is clock() . The last time I tried it under Windows with MSVC, the accuracy was 18ms.


For the first part of your question, you need to make sure that after the fork call, you have not called exec.

In your second part, well... its running too fast for difftime Try using gettimeofday() it gives you upto microseconds


Need Your Help

JQGrid How do I removed the annoying horizontal scrollbar when autowidth = true? (In IE)

javascript jquery jqgrid

I seem to be getting an annoying horizontal scrollbar when using autowidth=true in IE

Find a given key's value in a nested ordered dict python

python get find key-value ordereddictionary

I am trying to find the value of a given key from a nested OrderedDict.