查找文件操作的CPU使用情况

问题描述 投票:0回答:1

我在我的C程序中使用fopen,fclose,fseek等文件操作。我想知道我是否消耗了太多的CPU并且基于它想要进行优化。但是,我不确定如何计算每个函数的CPU使用率(fopen,fclose等)。

即使没有可用的标准工具,任何类型的计算建议都是有用的。

该程序既可以在Visual Studio环境中运行,也可以在Linux上运行。

提前致谢!

c linux visual-studio fopen cpu-usage
1个回答
0
投票

您可以使用如下的时钟功能,该功能在time.h中

int main ()
{
 clock_t start_t, end_t, total_t;
 int i;

 start_t = clock();
 printf("Starting of the program, start_t = %ld\n", start_t);

 printf("Going to scan a big loop, start_t = %ld\n", start_t);
 for(i=0; i< 10000000; i++) {
 }
 end_t = clock();
 printf("End of the big loop, end_t = %ld\n", end_t);

 total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;
 printf("Total time taken by CPU: %f\n", total_t  );
 printf("Exiting of the program...\n");

 return(0);
 }
© www.soinside.com 2019 - 2024. All rights reserved.