Unexpected timing results

Roel Schroeven rschroev_nospam_ml at fastmail.fm
Fri Feb 24 05:51:09 EST 2006


Steven D'Aprano schreef:
> On Fri, 24 Feb 2006 10:11:18 +0100, Magnus Lycka wrote:
> 
>> Steven D'Aprano wrote:
>>> It looks like the time function under Linux at least is very slow.
>> Perhaps you should try doing the same thing in C.
>> Then you can see whether the problem is in the
>> wrapper or in the system call.
> 
> Good idea.
> 
> Now, has anyone got a copy of "Learning C for Dummies" I can borrow?
> 
> *chagrined smile*

First, for reference, the Python version on the machine I used for 
testing (Debian 3.1, P4 3GHz):

timer1:  0.36007809639
timer2:  3.18800234795

On Windows the difference is much less.

C (compiled with gcc 3.3.5 without optimizations) (see below for the code):

timer1: 0.000603
timer2: 2.624557

I think it looks like the problem is in the system call.



C code:

#include <stdio.h>
#include <sys/time.h>
#include <time.h>

void func(void)
{
}

double gettime()
{
     struct timeval tv;
     gettimeofday(&tv, NULL);
     return tv.tv_sec + tv.tv_usec/1e6;
}

double timer1(void)
{
     double t0, t1;
     int i;
     t0 = gettime();
     for (i = 0; i < 100000; ++i)
     {
         func();
     }
     t1 = gettime();
     return t1 - t0;
}

double timer2(void)
{
     int i;
     double t = 0.0;
     double t0, t1;
     for (i = 0; i < 1000000; ++i)
     {
         t0 = gettime();
         func();
         t1 = gettime();
         t += t1 - t0;
     }
     return t;
}

int main(void)
{
     printf("timer1: %lf\n", timer1());
     printf("timer2: %lf\n", timer2());
     return 0;
}




-- 
If I have been able to see further, it was only because I stood
on the shoulders of giants.  -- Isaac Newton

Roel Schroeven



More information about the Python-list mailing list