跳到正文

个人记录

clock( )卡时

_Blue_约 1 分钟
原文
文章信息与版本

文章信息

发布
2021年11月5日
收录
2025年12月12日
更新
2026年11月21日
篇幅
约 1 分钟

版本

  1. v1当前

NiuJuxin的blog

计算程序运行时间

C++
#include <iostream>
#include <ctime>
using namespace std;

int main()
{
	for(int j = 1; j <= 30; j++)
		for (int i = 1; i <= 12000000; i++);
	cout << double(clock()) / double(CLOCKS_PER_SEC) << "s." << endl;
	return 0;
}

计算程序在规定时间内能够完成的循环次数

C++
#include <iostream>
#include <ctime>
using namespace std;

int main()
{
	int x = 0;
	double sec;
	cin >> sec;
	clock_t delay = sec * CLOCKS_PER_SEC;
	clock_t start = clock();
	while (clock() - start < delay)
		x++;
	cout << "After " << sec << " s, x = " << x << ".\n";
	return 0;
}
原文