1

I have to get the execution time of a Python script with 100 different input values, so I wrote the following C program

#include <stdlib.h>
#include <stdio.h>

int main(void) {

    int i;
    char command [200];

    for (i = 1; i <= 100; ++i) {
        sprintf(command, "time python program.py %d", i);
        system(command);
    };

    return 0;
};

With this program I can see the execution time of each execution, but I'd like to be able to catch it in a variable. Is there a way to do that?

Adrian
  • 829
  • 5
  • 14
  • 21
  • 1
    Perhaps what you're *really* looking for is [`timeit`](http://docs.python.org/2/library/timeit.html). See also [How to use timeit correctly](http://stackoverflow.com/questions/8220801/how-to-use-timeit-correctly). – Jonathon Reinhart Feb 13 '14 at 03:26

1 Answers1

1

gettimeofday() from <sys/time.h> can be used in your case.

 double elapsedTime[100];
 for (i = 1; i <= 100; ++i) {    
        sprintf(command, "python program.py %d", i);
        gettimeofday(&t1, NULL);
        system(command);
        gettimeofday(&t2, NULL);

        // compute and print the elapsed time in millisec
        elapsedTime[i] = (t2.tv_sec - t1.tv_sec) * 1000.0;      // sec to ms
        elapsedTime[i] += (t2.tv_usec - t1.tv_usec) / 1000.0;   // us to ms
    };

If possible, you can use some profiling tool which supports python.

Jeyaram
  • 9,158
  • 7
  • 41
  • 63