c++中计算运行时间 clock() clock_t 示例:比较递归和非递归数值运算

本文介绍如何使用C++内置的时间测量函数clock()来评估不同算法的执行效率,并通过递归与非递归版本的斐波那契数列计算程序为例,展示了实际应用中的性能差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 我们在写程序时,常常会好奇,到底我们的几个算法那个好呢,这时,当然你可已使用大O技法来分析复杂度,当然,我认为还是使用编程来计算运算时间来的简单,而且,在数据面前,我们可以信服。
 在C++的库函数中,已经给我们提供了这样的方法,clock(),我们可以使用它来计算程序的运行时间,闲话少说了,开门见山:
 
 计算程序运行时间使用的知识点:
 1clock_t
 2clock()
 3CLOCKS_PER_SEC
 
 这些库函数、类型和常量都是定义在ctime库中的。下面就解释一下吧!
 
 1clock_t数据类型,其实,当你打开time.h就知道了,就是个long型,用来记录一段时间内的clocks数,即CPU的运行单元时间。
 
 2clocks()函数,返回类型clock_t,返回的是从程序开始,到你掉用clock()函数这段时间的clocks。
 
 3CLOCK_PER_SRC
 
    我们若想计算程序的运行时间,则只要根据程序的入口点和出口点出都计算clocks,再算差就可以了,都是在main()中进行操作,如下:
   
   
    int main() {
   
  clock_t start, end;
  
  start = clock();
  
  //省略
  
  end = clock();
        cout<<"Run time: "<<(double)(end - start) / CLOCKS_PER_SEC<<"S"<<endl;
  return 0;
    }
   
   
    这样就ok了,我们来试试吧,都说一般情况下,由于递归的堆栈操作性能不好,不建议使用递归进行数值计算,那么,我们跑跑下面两个程序就知道原因了,它们都是求Fibonachi的。
   
    递归:
    #include <iostream>
#include <ctime>

using namespace std;

unsigned int outFibonachi(int i);  //其中i表示其中的第几个数

int main() {
 
 clock_t start, end;

 start = clock();
 
 for(int j = 1; j <= 30; j++) {
  cout<<outFibonachi(j);
  if( j % 5 == 0) {
   cout<<"/n";
  }else {
   cout<<" , ";
  }

 }

 end = clock();

 cout<<"Run time: "<<(double)(end - start) / CLOCKS_PER_SEC<<endl;


 return 0;
}


unsigned int outFibonachi(int i) {
 if(i <= 2) {
  return 1;
 }else {
  return outFibonachi(i - 1) + outFibonachi(i - 2);
 }

}
   
   
    非递归:
   
    #include <iostream>
#include <ctime>

using namespace std;

int main() {
 clock_t start, end;

 start = clock();
 
 int a, b, i;
 a = 1;
 b = 1;

 for(i = 1; i <= 30; i++) {
  if(i <= 2) {
   cout<<1<<" , ";
  }else {
   cout<<(a+b);
   if(i % 5 == 0)
    cout<<"/n";
   else
    cout<<" , ";
  
   a = b;
   b = a + b;
  }

  
 
 }

 end = clock();
 
 cout<<"Run time: "<<(double)(end - start) / CLOCKS_PER_SEC<<endl;
   
 return 0;

}
   
   
   
   
    如果,效果不明显,就将计算规模扩大吧,可以套一层循环,哈哈哈

#include <iostream> #include <filesystem> #include <chrono> #include <vector> #include <algorithm> namespace fs = std::filesystem; // 将文件时间转换为time_t time_t filetime_to_timet(const fs::file_time_type& ft) { using namespace std::chrono; auto sctp = time_point_cast<system_clock::duration>( ft - fs::file_time_type::clock::now() + system_clock::now() ); return system_clock::to_time_t(sctp); } // 递归搜索文件 void search_files(const fs::path& root, const std::string& extension, time_t start_time, time_t end_time, std::vector<fs::path>& results) { try { for (const auto& entry : fs::recursive_directory_iterator(root)) { if (fs::is_regular_file(entry)) { // 检查文件扩展名 std::string ext = entry.path().extension().string(); std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower); // 检查时间范围 auto ftime = entry.last_write_time(); time_t crtime = filetime_to_timet(ftime); if (ext == extension && crtime >= start_time && crtime <= end_time) { results.push_back(entry.path()); } } } } catch (const fs::filesystem_error& e) { std::cerr << "文件系统错误: " << e.what() << std::endl; } } int main() { fs::path search_path = "D:/TEXT"; std::string target_ext = ".txt"; // 目标文件类型 std::vector<fs::path> found_files; // 设置时间范围(示例:2023年1月1日 - 2024年1月1日) struct std::tm start_tm = { 0 }; start_tm.tm_year = 25; // 2100-2000 start_tm.tm_mon = 5; // 6月 start_tm.tm_mday = 1; time_t start_time = std::mktime(&start_tm); struct std::tm end_tm = { 0 }; end_tm.tm_year = 25; // 2024-1900 end_tm.tm_mon = 5; end_tm.tm_mday = 2; time_t end_time = std::mktime(&end_tm); search_files(search_path, target_ext, start_time, end_time, found_files); // 输出结果 std::cout << "找到 " << found_files.size() << " 个匹配文件:\n"; for (const auto& path : found_files) { std::cout << "• " << path.string() << "\n"; } return 0; }
06-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值