Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在C/C+中获取函数的执行时间+;_C++_C_Time - Fatal编程技术网

C++ 如何在C/C+中获取函数的执行时间+;

C++ 如何在C/C+中获取函数的执行时间+;,c++,c,time,C++,C,Time,我有一个函数,可以生成10000个随机数并将它们写入一个文件 void generator(char filename[]) { int i; int n; FILE* fp; if((fp=fopen(filename,"w+"))==NULL) { printf("Fail creating file!"); } srand((unsigned)time(NULL)); for(i=0

我有一个函数,可以生成10000个随机数并将它们写入一个文件

void generator(char filename[])              
{
    int i;
    int n;
    FILE* fp;
    if((fp=fopen(filename,"w+"))==NULL)
    {
        printf("Fail creating file!");
    }
    srand((unsigned)time(NULL));
    for(i=0;i<10000;i++)
    {
        n=rand()%10000;
        fprintf(fp,"%d ",n);
    }
    fclose(fp);
}
void生成器(字符文件名[])
{
int i;
int n;
文件*fp;
如果((fp=fopen(文件名,“w+”)==NULL)
{
printf(“创建文件失败!”);
}
srand((无符号)时间(NULL));
对于(i=0;i
void生成器(字符文件名)
{
时钟启动=时钟();
/*你的代码在这里*/
printf(“所用时间:%.2fs\n”,(双精度)(clock()-tStart)/CLOCKS\u/SEC);
}
加上#包括
#包括
.....
时钟启动=时钟();
…//要获取其执行时间的代码
双倍运行时间=静态播放(时钟()-开始)/每秒时钟数;

std::cout暂时将限额设为10000000


用秒表计时。将时间除以1000,你可以使用计时库

#include <chrono>

//*****//

auto start = std::chrono::steady_clock::now();
generator("file.txt")
auto end = std::chrono::steady_clock::now();

std::cout << "genarator() took "
              << std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() << "us.\n";
#包括
//*****//
自动启动=标准::时钟::稳定时钟::现在();
生成器(“file.txt”)
自动结束=标准::时钟::稳定时钟::现在();

std::cout代码评测不是一项特别容易的任务(或者,正如我们在编程中经常说的,它是“非常重要的”。)问题在于,以秒为单位度量的“执行时间”并不特别准确或有用

您想要做的是测量CPU周期数。这可以使用外部工具,如
callgrind
(Valgrind的工具之一)来完成。99%的可能性就是您想要的

<>如果你真的想用代码来完成这项任务,你就要承担一项相当艰巨的任务。我知道第一手——我在C++中编写了一个比较基准库,用于即时性能测试。
<>如果你真的想沿着这条路走下去,你可以研究英特尔处理器上的基准测试(大部分都是AMD),或者你使用的任何处理器。但是,正如我所说,这个主题是大的和深入的,而且远远超出了堆栈溢出的答案。

< p>你已经有一些很好的C++语言了。 这里使用C++的解决方案,使用<强> < /强>:

auto tbegin = std::chrono::high_resolution_clock::now(); 
...
auto tend = std::chrono::high_resolution_clock::now();
auto tduration = std::chrono::duration_cast<std::chrono::microseconds>(tend - tbegin).count();
auto-tbegin=std::chrono::high_-resolution_-clock::now();
...
自动趋势=标准::时钟::高分辨率时钟::现在();
auto-tduration=std::chrono::duration_cast(倾向-tbegin).count();
优点是您可以非常轻松地从切换到或任何其他时间测量单位

请注意,您可能对时钟精度有操作系统限制(在windows环境中通常为15毫秒),因此只有当您确实超过此限制时,才可能产生有意义的结果。

尝试此方法

    #include <sys/time.h> 

    struct timeval tpstart,tpend; 

    double timeuse; 

    //get time before generator starts
    gettimeofday(&tpstart,NULL); 

    //call generator function
    generator(filename);

    //get time after generator ends
    gettimeofday(&tpend,NULL); 

    //calculate the used time
    timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec; 
    timeuse/=1000000; 

    printf("Used Time:%fsec\n",timeuse);
#包括
结构timeval tpstart,tpend;
双重时间使用;
//在发电机启动前获取时间
gettimeofday(&tpstart,NULL);
//调用生成器函数
生成器(文件名);
//在生成器结束后获取时间
gettimeofday(&tpend,NULL);
//计算使用的时间
timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec;
timeuse/=1000000;
printf(“使用时间:%fsec\n”,timeuse);

<代码> C或C++?它们是两种不同的语言。你在测量数字的生成还是写入文件?有比你所发布的更快的文件写入方法。你需要解释你的函数被中断。注意任何运行的任务也使用硬驱动器。执行完整的F所花费的时间。此外,当处理器是Intel芯片时,它可能以其turbo boost频率运行,这使得周期数对于计时也毫无用处,因为它的时钟频率未知。这是一个系统特定的东西,不能在机器之间使用。你可以使用基准程序比较机器的性能,但这就是你所能做的。即使你还没有解决我的问题,我还是喜欢你的答案。@CarefulNow,我的基准测试库在比较两个“测试”的性能方面做得很好(当然,这是为了进行适当的比较而精心编写的)在同一台机器上。到目前为止,它可以在所有Intel/AMD Linux系统上运行,只要代码是g++编译的。我不必为该库中的其他编译器或体系结构操心。它不会给出完美的结果,但它仍然有非常严格和一致的大概估计,与Callgrind告诉你的相差不远。@Français,我很高兴这很有帮助。如果这是您要选择的答案,请单击旁边的复选标记“接受”答案。否则,如果您选择其他人的答案,请将他们的答案标记为“接受”。您应该接受最能引导您前进的答案。这假设函数的运行时间线性地取决于此限制,这显然不是打开和关闭文件的情况。这并不说明程序被调出或中断。此外,秒表也不是非常准确(人类按下开始和停止按钮既不准确也不一致)。有趣的是,[gettimeofday()])依赖于操作系统(即POSIX,而不是标准的C/C++)。OSI的时间设置也可能会出现不连续性。我不知道如何避免不连续性。
auto tbegin = std::chrono::high_resolution_clock::now(); 
...
auto tend = std::chrono::high_resolution_clock::now();
auto tduration = std::chrono::duration_cast<std::chrono::microseconds>(tend - tbegin).count();
    #include <sys/time.h> 

    struct timeval tpstart,tpend; 

    double timeuse; 

    //get time before generator starts
    gettimeofday(&tpstart,NULL); 

    //call generator function
    generator(filename);

    //get time after generator ends
    gettimeofday(&tpend,NULL); 

    //calculate the used time
    timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec; 
    timeuse/=1000000; 

    printf("Used Time:%fsec\n",timeuse);