[英]Elapsed time of running a C program
我想知道要添加到程序中的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;
}
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;
}
干杯
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;
}
它适用于MVS2008和2003年的Borland C ++ builderX。
在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;
}
这将给你时间,以毫秒为单位(注意/ 1000.0
部分)。 如果你想要秒,删除/ 1000.0
。 如果你想简单的时钟周期,这将是更准确,使function_time
一个clock_t
,更换function_time = ...
本着:
function_time = end - start;
为了给整个程序计时,我建议创建一个名为_main()
的函数,将所有与程序相关的代码从main()
(不是计时代码!)移动到该函数,并从main()
调用它。 这样,更清楚的是什么是时序代码以及程序的其余部分。
你可以使用clock()
函数(在<time.h>
),如果你想测试一个代码块,或者在* nix上测试time
程序,就像另一个回答者建议的那样。 例如
> time ./foo my args
对于时钟,您需要减去两个检查点之间的差异。 例如
#include <time.h>
void f() {
clock_t start, end;
start = clock();
// some long code.
end = clock();
printf("Took %ld ticks\n", end-start);
// or in (fractional) seconds.
printf("Took %f seconds\n", (double)(end-start)/CLOCKS_PER_SEC);
}
关于您的新错误,您不能在VC中混合代码和声明。 您不能调用任何函数然后继续声明变量。 在顶部声明所有变量,或使用C ++模式进行编译。
如果您的程序需要总计,那么在Linux控制台中:
$ time myProgram
您还可以在代码中使用time.h。
#include <time.h>
int main(){
time_t start, end;
start = time(0);
/* some working code */
end = time(0);
printf("%i seconds", end - start );
}
你可能想要time.h和clock()
函数。
如果您在Windows上并且想要在微秒内测量内容,请调查QueryPerformanceCounter()和QueryPerformanceFrequency()。 在许多系统上,这些可以解决完整的处理器时钟周期,是纳秒级的三分之一,而且我不相信我曾经看到它比3.5795MHz更粗糙,仍然远低于一微秒。
您调用QueryPerformanceFrequency()来确定计数器每秒计数的数量。 然后在测试代码之前调用QueryPerformanceCounter(),然后再调用。 Delta读取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
显然,这些访问硬件计数设备与主板上的某些系统振荡器相连,在这种情况下,它们不应受到软件负载的抖动。 您获得的分辨率取决于您的系统。
跟进
这是一个非常简单的完整程序,演示了界面:
#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;
}
对于像这样简单的事情,跳过IDE更快,只需将其保存在foo.c文件中(假设MS VS 2008)使用命令行
cl foo.c
建立它。 这是我系统上的输出:
Your performance counter ticks 3579545 times per second
Resolution is 279.365115 nanoseconds
Code under test took 0.012519 sec
您也可以尝试GetTickCount 。 时钟也可以正常工作。 但是,我想时钟值会改变,如果某个其他进程或某些进程手动更改系统时间,那么GetTickCount值不受此影响。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.