运行C程序的经过时间

yCa*_*ran 8 c windows time elapsed

我想知道要添加到程序中的C代码行,以便它告诉我程序运行的总时间.我猜应该在main的开头附近进行计数器初始化,在main函数结束后进行初始化.是正确的标题clock.h

非常感谢...

更新我有一台Win Xp机器.它只是clock()在程序开头添加还是在clock()程序结束时添加另一个?然后我可以估计时差.是的,你是对的time.h.

这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <share.h>
#include <time.h>


void f(long double fb[], long double fA, long double fB);

int main() {

clock_t start, end;
start = clock();


const int ARRAY_SIZE = 11;

long double* z = (long double*) malloc(sizeof (long double) * ARRAY_SIZE);

int i;
long double A, B;

if (z == NULL) {
    printf("Out of memory\n");
    exit(-1);
}

A = 0.5;
B = 2;


for (i = 0; i < ARRAY_SIZE; i++) {
    z[i] = 0;
}

z[1] = 5;

f(z, A, B);

for (i = 0; i < ARRAY_SIZE; i++)
    printf("z is %.16Le\n", z[i]);



free(z);
z = NULL;

end = clock();
printf("Took %ld ticks\n", end-start);
printf("Took %f seconds\n", (double)(end-start)/CLOCKS_PER_SEC);



return 0;  
}  

void f(long double fb[], long double fA, long double fB) {
    fb[0] = fb[1]* fA;
    fb[1] = fb[1] - 1;
    return;
 }  
Run Code Online (Sandbox Code Playgroud)

MVS2008的一些错误:

testim.c(16) : error C2143: syntax error : missing ';' before 'const'  
testim.c(18) :error C2143: syntax error : missing ';' before 'type'  
testim.c(20) :error C2143: syntax error : missing ';' before 'type'   
testim.c(21) :error C2143: syntax error : missing ';' before 'type'    
testim.c(23) :error C2065: 'z' : undeclared identifier   
testim.c(23) :warning C4047: '==' : 'int' differs in levels of indirection from 'void *'  
testim.c(28) : error C2065: 'A' : undeclared identifier
testim.c(28) : warning C4244: '=' : conversion from 'double' to 'int', possible loss of data   

它会导致28个错误.请注意,没有您的时钟代码我没有任何错误/警告.

最新消息:遗憾的是我在这里没有得到很好的答复.但在Google上搜索后,代码正常运行.这里是:

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


void f(long double fb[], long double fA);

int main() {

clock_t start = clock();


const int ARRAY_SIZE = 11;

long double* z = (long double*) malloc(sizeof (long double) * ARRAY_SIZE);

int i;
long double A;

if (z == NULL) {
printf("Out of memory\n");
exit(-1);
}

A = 0.5;


for (i = 0; i < ARRAY_SIZE; i++) {
z[i] = 0;
}

z[1] = 5;

f(z, A);

for (i = 0; i < ARRAY_SIZE; i++)
printf("z is %.16Le\n", z[i]);



free(z);
z = NULL;

printf("Took %f seconds\n", ((double)clock()-start)/CLOCKS_PER_SEC);



return 0;
}

void f(long double fb[], long double fA) {
fb[0] = fb[1]* fA;
fb[1] = fb[1] - 1;
return;
}
Run Code Online (Sandbox Code Playgroud)

干杯

4月10日更新:由于"JustJeff",这是一个更好的解决方案

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

void f(long double fb[], long double fA);

const int ARRAY_SIZE = 11;

int main(void)
{

   long double* z = (long double*) malloc(sizeof (long double) * ARRAY_SIZE);
   int i;
   long double A;

   LARGE_INTEGER freq;
   LARGE_INTEGER t0, tF, tDiff;
   double elapsedTime;
   double resolution;

   if (z == NULL) {
   printf("Out of memory\n");
   exit(-1);
   }
   QueryPerformanceFrequency(&freq);
   QueryPerformanceCounter(&t0);
   // code to be timed goes HERE
   {
    A = 0.5;


    for (i = 0; i < ARRAY_SIZE; i++) {
    z[i] = 0;
    }

    z[1] = 5;
    f(z, A);


    for (i = 0; i < ARRAY_SIZE; i++)
    printf("z is %.16Le\n", z[i]);

    free(z);
    z = NULL;

   }
QueryPerformanceCounter(&tF);
tDiff.QuadPart = tF.QuadPart - t0.QuadPart;
elapsedTime = tDiff.QuadPart / (double) freq.QuadPart;
resolution = 1.0 / (double) freq.QuadPart;
printf("Your performance counter ticks %I64u times per second\n", freq.QuadPart);
printf("Resolution is %lf nanoseconds\n", resolution*1e9);
printf("Code under test took %lf sec\n", elapsedTime);
return 0;
}


void f(long double fb[], long double fA) {
fb[0] = fb[1]* fA;
fb[1] = fb[1] - 1;
return;
}
Run Code Online (Sandbox Code Playgroud)

它适用于MVS2008和2003年的Borland C++ builderX.

Jav*_*ier 6

在Unix(我认为)系统上,time带有程序名称作为命令行参数的命令将告诉您程序运行所需的时间.请注意,这测量整个程序的执行时间.如果您只需测试一个部件,请包含time.h并使用时钟功能,或多或少如下:

#include <time.h>

int main() {
    clock_t start;
    clock_t end;
    int function_time;
    start = clock();
    function_you_want_to_time();
    end = clock();
    /* Get time in milliseconds */
    function_time = (double)(end - start) / (CLOCKS_PER_SEC / 1000.0);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这将给你时间,以毫秒为单位(注意/ 1000.0部分).如果你想要秒,请删除/ 1000.0.如果你想简单的时钟周期,这将是更准确,使function_time一个clock_t并更换function_time = ...符合:

function_time = end - start;
Run Code Online (Sandbox Code Playgroud)

为了给整个程序计时,我建议创建一个名为_main()或者之类的函数,将所有与程序相关的代码main()(不是时序代码!)移动到该函数,并从中调用它main().这样,更清楚的是什么是时序代码以及程序的其余部分.


Jus*_*eff 1

如果您使用的是 Windows 并且想要以微秒为单位测量数据,请研究 QueryPerformanceCounter() 和 QueryPerformanceFrequency()。在许多系统上,这些可以解析完整的处理器时钟周期,即三分之一纳秒的数据,而且我认为我从未见过比 3.5795MHz 更粗糙的数据,仍然远低于一微秒。

您可以调用 QueryPerformanceFrequency() 来确定计数器每秒计数的数量。然后在测试代码之前调用 QueryPerformanceCounter(),然后再次调用。将 QPC 的两个读数相除,然后除以 QPF 的周期,即可得到两次 QPC 调用之间经过的时间。就像这样...

LARGE_INTEGER freq;
LARGE_INTEGER t0, tF, tDiff;
double elapsedTime;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&t0);
// code to be timed goes HERE
QueryPerformanceCounter(&tF);
tDiff.QuadPart = tF.QuadPart - t0.QuadPart;
elapsedTime = tDiff.QuadPart / (double) freq.QuadPart;
// elapsedTime now has your measurement, w/resolution given by freq
Run Code Online (Sandbox Code Playgroud)

显然,这些访问与主板上某些系统振荡器相关的硬件计数设备,在这种情况下,它们不应受到软件负载的抖动。您获得的分辨率取决于您的系统。

跟进

这是一个非常简单的完整程序,演示了该界面:

#include <windows.h>
int main(void)
{
    LARGE_INTEGER freq;
    LARGE_INTEGER t0, tF, tDiff;
    double elapsedTime;
    double resolution;
    QueryPerformanceFrequency(&freq);
    QueryPerformanceCounter(&t0);
    // code to be timed goes HERE
    {
        Sleep(10);
    }
    QueryPerformanceCounter(&tF);
    tDiff.QuadPart = tF.QuadPart - t0.QuadPart;
    elapsedTime = tDiff.QuadPart / (double) freq.QuadPart;
    resolution = 1.0 / (double) freq.QuadPart;
    printf("Your performance counter ticks %I64u times per second\n", freq.QuadPart);
    printf("Resolution is %lf nanoseconds\n", resolution*1e9);
    printf("Code under test took %lf sec\n", elapsedTime);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

对于像这样简单的事情,跳过 IDE 会更快,只需将其保存在 foo.c 文件中并(假设 MS VS 2008)使用命令行

cl foo.c
Run Code Online (Sandbox Code Playgroud)

来建造它。这是我的系统上的输出:

Your performance counter ticks 3579545 times per second
Resolution is 279.365115 nanoseconds
Code under test took 0.012519 sec
Run Code Online (Sandbox Code Playgroud)