加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C/C++中的算法速度测试仪

发布时间:2020-12-16 10:22:39 所属栏目:百科 来源:网络整理
导读:我必须以毫秒为单位计算算法的速度.在C/C++中,我该怎么做?我需要在输入之前和输出之后写smth但是究竟是什么? 解决方法 您可以使用 time.h中的clock()函数. clock()显示自程序启动以来已经过了多少个刻度.宏CLOCKS_PER_SEC包含每秒的滴答数,因此您实际上可
我必须以毫秒为单位计算算法的速度.在C/C++中,我该怎么做?我需要在输入之前和输出之后写smth但是究竟是什么?

解决方法

您可以使用< time.h>中的clock()函数.
clock()显示自程序启动以来已经过了多少个刻度.宏CLOCKS_PER_SEC包含每秒的滴答数,因此您实际上可以获得时间.

//We start measuring here. Remember what was the amount of ticks in the 
//beginning of the part of code you want to test:
int start = clock();
//<...>
//Do your stuff here
//<...>
int end = clock();//Now check what amount of ticks we have now. 
//To get the time,just subtract start from end,and divide by CLOCKS_PER_SEC.
std::cout << "it took " << end - start << "ticks,or " << ((float)end - start)/CLOCKS_PER_SEC << "seconds." << std::endl;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读