修复数据结构clock.c编译错误
- 添加缺失的MyFunction()函数实现 - 实现1到1000000求和的示例功能 - 修正CLK_TCK为CLOCKS_PER_SEC(现代C标准) - 解决隐式函数声明的编译错误 - 程序现在可以正常编译和运行,正确测量函数执行时间
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#include <direct.h>
|
||||
#endif
|
||||
|
||||
// clock_t 是一个无符号整数类型,用于表示时钟 ticks 的数量。
|
||||
clock_t start, stop;
|
||||
|
||||
// 记录程序运行时间
|
||||
double duration;
|
||||
|
||||
// 示例函数:计算1到1000000的和
|
||||
void MyFunction()
|
||||
{
|
||||
long long sum = 0;
|
||||
for (int i = 1; i <= 1000000; i++) {
|
||||
sum += i;
|
||||
}
|
||||
printf("计算结果:1到1000000的和为 %lld\n", sum);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// 设置控制台编码为UTF-8,防止中文乱码
|
||||
#ifdef _WIN32
|
||||
system("chcp 65001 > nul"); // 设置控制台编码为UTF-8
|
||||
SetConsoleOutputCP(65001); // 设置控制台输出编码
|
||||
SetConsoleCP(65001); // 设置控制台输入编码
|
||||
_mkdir("records");
|
||||
#endif
|
||||
|
||||
// 记录程序开始时间
|
||||
start = clock();
|
||||
// 要测试的代码
|
||||
MyFunction();
|
||||
// 记录程序结束时间
|
||||
stop = clock();
|
||||
// 计算程序运行时间
|
||||
duration = ((double)(stop - start)) / CLOCKS_PER_SEC;
|
||||
printf("程序运行时间为:%f 秒\n", duration);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user