gpt4 book ai didi

c++ - C/C++ 中的算法速度测试器

转载 作者:太空狗 更新时间:2023-10-29 19:44:14 25 4
gpt4 key购买 nike

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

最佳答案

你可以使用 clock()来自 <time.h> 的函数
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;

关于c++ - C/C++ 中的算法速度测试器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11609026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com