如何计算在C执行时间? [英] How to calculate the execution time in C?

查看:228
本文介绍了如何计算在C执行时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何计算的执行时间在以下code:

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;
} 

我用timeval_subtract功能,当我执行code,我得到了:

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)   :

首先,我找不到错误在发现处理器的数量(我必须得到1个处理器)。

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?

推荐答案

试试这个:

printf("    Time   : %lu\n", clock() );
printf("    Start Time   : %lds %ldus\n", tv1.tv_sec, tv1.tv_usec);
printf("    End Time   : %lds %ldus\n", tv2.tv_sec, tv2.tv_usec);

和为:

double timeval_subtract (result, x, y)

使用以下命令在微秒返回的时间差:

use the following to return the time difference in micro seconds:

long timeval_subtract (struct timeval * result, struct timeval * x, struct timeval * y)
{
   long usec = x->tv_sec * 1000000L + x->tv_usec;
   usec -= (y->tv_sec * 1000000L + y->tv_usec);

   result->tv_sec = usec / 1000000L;
   result->tv_usec = usec % 1000000L;

   return usec;
}

根据两个日期 X 函数的返回值<$ C $的差异C> timeval_subtract (不是结果!)可能是错误的,因为溢出。

Depending on the difference of the two dates x and y the return value of the function timeval_subtract (not the value represented by result!) might be wrong, due to an overflow.

假设一个长为32位广本溢出将与差异比4294s大,长期具有64位(这应该是一个64位机的情况下)后,对子级很久以后发生溢出发生...; - )

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 ... ;-)

这篇关于如何计算在C执行时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆