C语言中如何计算执行时间?
如何计算以下代码中的执行时间:
#include <stdio.h> /* Core input/output operations */
#include <stdlib.h> /* Conversions, random numbers, memory allocation, etc. */
#include <math.h> /* Common mathematical functions */
#include <time.h> /* Converting between various date/time formats */
#include <sys/time.h>
#define PI 3.1415926535 /* Known vaue of PI */
#define NDARTS 128 /* Number of darts thrown */
double pseudo_random(double a, double b) {
double r; /* Random number */
r = ((b - a) * ((double) rand()/(double) RAND_MAX)) + a;
return r;
}
int main (int argc, char *argv[]) {
int n_procs, /* Number of processors */
llimit, /* Lower limit for random numbers */
ulimit, /* Upper limit for random numbers */
n_circle, /* Number of darts that hit the circle */
i; /* Dummy/Running index */
double pi_sum, /* Sum of PI values from each WORKER */
x, /* x coordinate, betwen -1 & +1 */
y, /* y coordinate, betwen -1 & +1 */
z, /* Sum of x^2 and y^2 */
error; /* Error in calculation of PI */
clock_t start_time, /* Wall clock - start time */
end_time; /* Wall clock - end time */
struct timeval stime, starttime1, endtime1;
struct timeval tv1, tv2, diff;
llimit = -1;
ulimit = 1;
n_circle = 0;
printf("\n Monte Carlo method of finding PI\n\n");
printf(" Number of processors : %d\n", n_procs);
printf(" Number of darts : %d\n\n", NDARTS);
gettimeofday(&tv1, NULL);
gettimeofday(&stime, NULL);
srand(stime.tv_usec * stime.tv_usec * stime.tv_usec * stime.tv_usec);
for (i = 1; i <= NDARTS; i++) {
x = pseudo_random(llimit, ulimit);
y = pseudo_random(llimit, ulimit);
z = pow(x, 2) + pow(y, 2);
if (z <= 1.0) {
n_circle++;
}
}
pi_sum = 4.0 * (double)n_circle/(double)NDARTS;
pi_sum = pi_sum / n_procs;
error = fabs((pi_sum - PI)/PI) * 100;
gettimeofday(&tv2, NULL);
double timeval_subtract (result, x, y)
{
result = ((double) x - (double) y ) / (double)CLOCKS_PER_SEC;
}
double result1 = timeval_subtract(&diff, &tv1, &tv2);
printf(" Known value of PI : %11.10f\n", PI);
printf(" Average value of PI : %11.10f\n", pi_sum);
printf(" Percentage Error : %10.8f\n", error);
printf(" Time : \n", clock() );
printf(" Start Time : \n",&tv1);
printf(" End Time :\n", &tv2);
printf(" Time elapsed (sec) : \n", result1 );
return 0;
}
我使用了 timeval_subtract 函数,当我执行代码时,我得到:
Monte Carlo method of finding PI
Number of processors : 16372
Number of darts : 128
Known value of PI : 3.1415926535
Average value of PI : 0.0002004184
Percentage Error : 99.99362048
Time :
Start Time :
End Time :
Time elapsed (sec) :
首先,我找不到查找处理器数量的错误(我必须获得 1 个处理器)。
第二个“这是最重要的一点”,为什么我得到的时间、开始时间、结束时间和经过的时间都是空的?
How can I calculate the execution time in the following code:
#include <stdio.h> /* Core input/output operations */
#include <stdlib.h> /* Conversions, random numbers, memory allocation, etc. */
#include <math.h> /* Common mathematical functions */
#include <time.h> /* Converting between various date/time formats */
#include <sys/time.h>
#define PI 3.1415926535 /* Known vaue of PI */
#define NDARTS 128 /* Number of darts thrown */
double pseudo_random(double a, double b) {
double r; /* Random number */
r = ((b - a) * ((double) rand()/(double) RAND_MAX)) + a;
return r;
}
int main (int argc, char *argv[]) {
int n_procs, /* Number of processors */
llimit, /* Lower limit for random numbers */
ulimit, /* Upper limit for random numbers */
n_circle, /* Number of darts that hit the circle */
i; /* Dummy/Running index */
double pi_sum, /* Sum of PI values from each WORKER */
x, /* x coordinate, betwen -1 & +1 */
y, /* y coordinate, betwen -1 & +1 */
z, /* Sum of x^2 and y^2 */
error; /* Error in calculation of PI */
clock_t start_time, /* Wall clock - start time */
end_time; /* Wall clock - end time */
struct timeval stime, starttime1, endtime1;
struct timeval tv1, tv2, diff;
llimit = -1;
ulimit = 1;
n_circle = 0;
printf("\n Monte Carlo method of finding PI\n\n");
printf(" Number of processors : %d\n", n_procs);
printf(" Number of darts : %d\n\n", NDARTS);
gettimeofday(&tv1, NULL);
gettimeofday(&stime, NULL);
srand(stime.tv_usec * stime.tv_usec * stime.tv_usec * stime.tv_usec);
for (i = 1; i <= NDARTS; i++) {
x = pseudo_random(llimit, ulimit);
y = pseudo_random(llimit, ulimit);
z = pow(x, 2) + pow(y, 2);
if (z <= 1.0) {
n_circle++;
}
}
pi_sum = 4.0 * (double)n_circle/(double)NDARTS;
pi_sum = pi_sum / n_procs;
error = fabs((pi_sum - PI)/PI) * 100;
gettimeofday(&tv2, NULL);
double timeval_subtract (result, x, y)
{
result = ((double) x - (double) y ) / (double)CLOCKS_PER_SEC;
}
double result1 = timeval_subtract(&diff, &tv1, &tv2);
printf(" Known value of PI : %11.10f\n", PI);
printf(" Average value of PI : %11.10f\n", pi_sum);
printf(" Percentage Error : %10.8f\n", error);
printf(" Time : \n", clock() );
printf(" Start Time : \n",&tv1);
printf(" End Time :\n", &tv2);
printf(" Time elapsed (sec) : \n", result1 );
return 0;
}
I used timeval_subtract function and when I execute the code, I got:
Monte Carlo method of finding PI
Number of processors : 16372
Number of darts : 128
Known value of PI : 3.1415926535
Average value of PI : 0.0002004184
Percentage Error : 99.99362048
Time :
Start Time :
End Time :
Time elapsed (sec) :
First, I couldn't find the mistake in finding the number of processors (I must get 1 processor).
Second "which is the most important point", Why do I get the Time, Start Time, End Time and Time elapsed empty?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
因为您没有足够的格式字符串,所以您需要以“%”开头的内容,例如:
Because you don't have adequate format strings for them, you need something starting with a '%', like:
n_procs 从未初始化,打印的 16372 值恰好是堆栈上之前的值。
C 标准库不提供查询处理器计数或高性能计时器的功能,因此您必须考虑其他查询方法。例如,POSIX 和 Windows API 都提供这样的功能。
编辑:请参阅以编程方式查找计算机上的核心数量 了解如何初始化 n_procs。看看您如何使用 gettimeofday,您可能使用的是某种 unix 变体; “n_procs = sysconf(_SC_NPROCESSORS_ONLN);”可能就是你想要的。
n_procs is never initialized, the 16372-value that gets printed just happens to be what was previously on the stack.
The C standard library doesn't provide functionality to query processor count or high-performance timers, so you will have to look at other means of querying this. For instance, both POSIX and Windows API provides functionality like this.
edit: See Programmatically find the number of cores on a machine for how to initialize n_procs. Seeing how you use gettimeofday, you're probably on some unix-variant; "n_procs = sysconf(_SC_NPROCESSORS_ONLN);" is probably what you want.
试试这个:
并且:
使用以下命令返回以微秒为单位的时间差:
根据两个日期
x
和y
的差异,函数的返回值<由于溢出,code>timeval_subtract(不是result
表示的值!)可能是错误的。假设 long 是 32 位宽,这种溢出将在差异大于 4294 秒时发生,对于具有 64 位的 long(应该是 64 位机器的情况),溢出将在很晚之后发生......;-)
Try this:
And for:
use the following to return the time difference in micro seconds:
Depending on the difference of the two dates
x
andy
the return value of the functiontimeval_subtract
(not the value represented byresult
!) might be wrong, due to an overflow.Assuming a long is 32bit wide this overflow will occur with differences larger than 4294s, for a long having 64bit (which should be the case an 64bit machines) the overflow whould occur after much later ... ;-)
我会尝试以下操作:
Start(&s) 一个分配的timer_profiling ,然后通过调用End(&s,buff); 检索字符串中的结果。
I'd try the following :
Start(&s) one with an allocated timer_profiling , then retrieve the result in a string by calling End(&s,buff);