我必须获得具有100个不同输入值的Python脚本的执行时间,因此我编写了以下C程序
#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;
};
使用这个程序,我可以看到每次执行的执行时间,但我希望能够在变量中捕获它。有没有办法做到这一点?
答案 0 :(得分:1)
gettimeofday()
的{p> <sys/time.h>
可用于您的情况。
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
};
如果可能,您可以使用一些支持python的分析工具。