如何测试所编写的C语言程序的执行时间

fcdean 2012-03-15 10:53:33
如题 谢谢各位高手!
...全文
495 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
fcdean 2012-03-17
  • 打赏
  • 举报
回复
按照NorthCan的程序运行出现如下错误,请赐教~~
C:\Documents and Settings\Administrator\桌面\test\fc.c(9) : error C2275: 'clock_t' : illegal use of this type as an expression
c:\program files\microsoft visual studio\vc98\include\time.h(84) : see declaration of 'clock_t'
C:\Documents and Settings\Administrator\桌面\test\fc.c(9) : error C2146: syntax error : missing ';' before identifier 'cEnd'
C:\Documents and Settings\Administrator\桌面\test\fc.c(9) : error C2065: 'cEnd' : undeclared identifier
[Quote=引用 6 楼 northcan 的回复:]
C/C++ code

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

int main()
{
clock_t cBegin = clock();
// do something...
clock_t cEnd = clock();

printf("execution time……
[/Quote]
fcdean 2012-03-17
  • 打赏
  • 举报
回复
按照NorthCan的程序运行出现如下错误,请赐教~~
C:\Documents and Settings\Administrator\桌面\test\fc.c(9) : error C2275: 'clock_t' : illegal use of this type as an expression
c:\program files\microsoft visual studio\vc98\include\time.h(84) : see declaration of 'clock_t'
C:\Documents and Settings\Administrator\桌面\test\fc.c(9) : error C2146: syntax error : missing ';' before identifier 'cEnd'
C:\Documents and Settings\Administrator\桌面\test\fc.c(9) : error C2065: 'cEnd' : undeclared identifier
cdaniel 2012-03-17
  • 打赏
  • 举报
回复
如果在linux 下,最简单的方法,在运行程序前加个time ,例如 time ./a.out
cs$ time ./a.out 
real 0m0.003s
user 0m0.000s
sys 0m0.000s
沭水河畔 2012-03-17
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 fcdean 的回复:]

按照NorthCan的程序运行出现如下错误,请赐教~~
C:\Documents and Settings\Administrator\桌面\test\fc.c(9) : error C2275: 'clock_t' : illegal use of this type as an expression
c:\program files\microsoft visual studio\v……
[/Quote]
樓主標題里寫到要測試程序運行的時間,可這是測試函數運行的時間啊。
使用clock這種寫法是Linux支持的,Windows可能不支持。如果對時間精度要求不高,可以用C庫提供的time函數。
代碼如下:

#include <stdio.h>
#include <time.h> /* for time() */
#include <Windows.h> /* for Sleep() */

void test(int sec)
{
Sleep(1000 * sec);
}

int main(int argc,char **argv)
{
time_t t1, t2, run_time;

time(&t1);
test(6);
time(&t2);

run_time = t2 - t1;

printf("%d\n", (int)run_time);
return 0;
}

如果對時間精度有一定要求,那麼GetSystemTime,如果再不夠用,那麼用QueryPerformanceCounter。
具體用法查MSDN。
fcdean 2012-03-15
  • 打赏
  • 举报
回复
谢谢各位大虾! 非常感谢~
zengguang_2003 2012-03-15
  • 打赏
  • 举报
回复
楼上回答的很正确,在函数之前和之后取得系统的时间,然后相减就是函数执行时间,不过在取得系统时间的时候,最小单位是微秒
northcan 2012-03-15
  • 打赏
  • 举报
回复

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

int main()
{
clock_t cBegin = clock();
// do something...
clock_t cEnd = clock();

printf("execution time: %d s", (cEnd - cBegin) / CLOCKS_PER_SEC);

return 0;
}
lanmeng521131485 2012-03-15
  • 打赏
  • 举报
回复
学习中,以前我是用的time();不过那个精确度不高啊!!!学习!!!
luciferisnotsatan 2012-03-15
  • 打赏
  • 举报
回复
看钟
代码里记下执行前,执行后的时间。
获取进程句柄,查看用户,内核时间等等。
使用专门的测试库
naoshi 2012-03-15
  • 打赏
  • 举报
回复

#include "stdafx.h"
#include "omp.h"
#include <stdlib.h>
int main (int argc, char *argv[])
{
double starttime, stoptime;
starttime = omp_get_wtime();
/*实际程序*/
stoptime = omp_get_wtime();
printf("Time for this routine: %3.2f\n", stoptime-starttime);
}
沭水河畔 2012-03-15
  • 打赏
  • 举报
回复
Linux/Unix下比較簡單,用time ./your_program或者更精確的/usr/bin/time ./your_program
Windows下可以寫這樣一個程序,通過system調用你的程序來顯示時間:
#include <stdio.h>

int main(void)
{
/* 獲取開始時間 */
system("your_program");
/* 獲取結束時間 */
/* 運行時間 = 結束時間 - 開始時間 */
}
northcan 2012-03-15
  • 打赏
  • 举报
回复
在某段程序之前和之后,分别调用c语言的库函数获取时间,或者调用系统的获取时间的API接口函数,将两次时间相减就是你程序的运行时间。

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧