算法速度测试程序在C / C ++ [英] algorithm speed tester in C/C++

查看:181
本文介绍了算法速度测试程序在C / C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须计算我的算法的速度,以毫秒为单位。在C ++ / C,我怎么能这样做?我需要写入smth之前输入和输出后,但究竟是什么?

I have to calculate speed of my algorithm in base of milliseconds. In C++/C , how can i do this? I need to write smth before input and after output but what exactly??

推荐答案

您可以使用 clock() c>< time.h>

clock()显示自您的程序启动以来, CLOCKS_PER_SEC 包含每秒钟的滴答数,因此您可以真正得到时间。

You could use clock() function from <time.h>
clock() shows how many ticks have passed since your program started. The macro CLOCKS_PER_SEC contains the number of ticks per second, so you can actually get time.

//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;

这篇关于算法速度测试程序在C / C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆