C++中计算程序的运行时间

对于简单的控制台程序,可使用clock(),表示程序开始执行到现在经过的时间,在要计算时间的代码前后添加如下代码即可(需要包含头文件<time.h>):
clock_t start, finish;
 double totaltime;
 start=clock();

//code

finish=clock();
 totaltime=(double)(finish-start)/CLOCKS_PER_SEC; //得到结果单位秒,如果时间太短太短的话可能是0
 std::cout<<totaltime<<"\n";

也可以调用win32API函数GetTickCount(),

 DWORD   dwStart   =   GetTickCount();
     //代码......... 
    DWORD   dwStop   =   GetTickCount();   
    DWORD   dwInterval   =   dwStop   -   dwStart;

posted on 2010-10-23 14:00 oliver 阅读(961) 评论(1)  编辑 收藏 引用

评论

# re: C++中计算程序的运行时间 2011-10-17 19:59 Neoh

Linux系统下的帖子?  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2010年10月>
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456

导航

统计

常用链接

留言簿

随笔档案

文章分类

文章档案

个人专栏

技术网站

搜索

最新评论

阅读排行榜

评论排行榜