clock时钟周期

clock时钟周期

cpp 复制代码
#include <iostream>
#include <ctime>

int main() {
    clock_t start = clock();  // 获取开始时间的时钟周期数

    // 模拟一些工作
    for (long long i = 0; i < 10000000000; ++i);

    clock_t end = clock();  // 获取结束时间的时钟周期数
    std::cout << "start: " << static_cast<double>(start) / CLOCKS_PER_SEC << " seconds" << std::endl;
    std::cout << "end: " << static_cast<double>(end) / CLOCKS_PER_SEC << " seconds" << std::endl;

    // 计算所用时间(秒)
    double elapsed_time = static_cast<double>(end - start) / CLOCKS_PER_SEC;
    std::cout << "Elapsed time: " << elapsed_time << " seconds" << std::endl;

    return 0;
}
/*
start: 0.51 seconds
end: 3.087 seconds
Elapsed time: 2.577 seconds
*/
相关推荐
LeoLei806031 分钟前
新特性之C++17
开发语言·c++
jllws11 小时前
C++基础:STL概述
开发语言·c++
FightingLod1 小时前
C++中list容器使用详解
开发语言·c++·list
yyqzjw1 小时前
【C++】单例模式
c++·单例模式
每天的积累1 小时前
C++学习笔记二
c++·笔记·学习
zengy51 小时前
代码随想录打卡第十三天
数据结构·c++·算法·leetcode
Danica~2 小时前
RpcChannel的调用过程
网络·c++·rpc
C_player_0012 小时前
C++ list的模拟实现
c++
逸群不凡3 小时前
C++11|lambda语法与使用
开发语言·c++
DieSnowK3 小时前
[C++][CMake][CMake基础]详细讲解
开发语言·c++·makefile·make·cmake·新手向·详细讲解