C++ 非常小的运行时间下的程序执行时间

C++ 非常小的运行时间下的程序执行时间,c++,C++,我查阅了一大堆关于如何对代码计时的其他示例,但是使用chrono或time的示例似乎都不起作用(它们返回0)。然而,什么起作用了。使用它的唯一缺点是,在我阅读时,它仅适用于Windows。我的教练使用Mac电脑,所以我不能把代码交给他。这就是我使用QueryPerformanceCounter时代码的样子 #include <iostream> #include "heapsort.h" #include "quicksort.h" #include "insertionsort.h

我查阅了一大堆关于如何对代码计时的其他示例,但是使用chrono或time的示例似乎都不起作用(它们返回0)。然而,什么起作用了。使用它的唯一缺点是,在我阅读时,它仅适用于Windows。我的教练使用Mac电脑,所以我不能把代码交给他。这就是我使用QueryPerformanceCounter时代码的样子

#include <iostream>
#include "heapsort.h"
#include "quicksort.h"
#include "insertionsort.h"


using namespace std;


#include <windows.h>
//THE FOLLOWING CODE RETURNS RUNNING TIME IN MICROSECONDS. 
//https://stackoverflow.com/questions/1739259/how-to-use-queryperformancecounter
double PCFreq = 0.0;
__int64 CounterStart = 0;

void StartCounter()
{
    LARGE_INTEGER li;
    if (!QueryPerformanceFrequency(&li))
        cout << "QueryPerformanceFrequency failed!\n";

    PCFreq = double(li.QuadPart) / 1000000.0;

    QueryPerformanceCounter(&li);
    CounterStart = li.QuadPart;
}
double GetCounter()
{
    LARGE_INTEGER li;
    QueryPerformanceCounter(&li);
    return double(li.QuadPart - CounterStart) / PCFreq;
}


int main(){
    static const size_t SIZE = 150;
    int arr[] = { 685, 119, 938, 836, 721, 801, 738, 334, 739, 89, 917, 277, 708, 905, 978, 84, 620, 948, 409, 891, 447, 957, 673, 627, 546, 137, 456, 594, 878, 972, 722, 934, 383, 628, 103, 604, 132, 2, 428, 893, 212, 629, 646, 382, 348, 49, 306, 707, 156, 373, 733, 419, 323, 825, 112, 930, 432, 862, 830, 69, 994, 600, 226, 570, 759, 988, 289, 75, 232, 167, 292, 644, 10, 679, 607, 522, 967, 341, 989, 130, 326, 816, 503, 794, 303, 108, 915, 148, 258, 73, 206, 701, 897, 350, 713, 940, 764, 471, 936, 93, 163, 824, 950, 796, 98, 823, 465, 37, 102, 342, 243, 696, 687, 935, 459, 50, 553, 225, 562, 181, 453, 665, 525, 175, 768, 251, 996, 954, 925, 531, 962, 585, 250, 829, 777, 928, 76, 704, 565, 20, 422, 51, 125, 197, 588, 267, 850, 494, 699, 173 };


    StartCounter();
    heapSort<int> heap(arr, SIZE);
    cout << GetCounter() << endl;
    StartCounter();
    quickSort<int> quick(arr, 0, SIZE-1);
    cout << GetCounter() << endl;
    StartCounter();
    insertionSort<int> insertion(arr);
    cout << GetCounter() << endl;
    return 0;
}
#包括
#包括“heapsort.h”
#包括“quicksort.h”
#包括“insertionsort.h”
使用名称空间std;
#包括
//以下代码以微秒为单位返回运行时间。
//https://stackoverflow.com/questions/1739259/how-to-use-queryperformancecounter
双PCFreq=0.0;
__int64 countstart=0;
void StartCounter()
{
大整数李;
if(!QueryPerformanceFrequency(&li))

cout如果所有平台都运行Intel,您可以考虑使用时间戳计数器(RDTSC)并围绕此编写自己的性能计数器。但它不是非常便携,并且存在各种棘手的位(内核之间的漂移、除非禁用该功能,否则时钟频率可变,等等).一般来说,我会选择低技术的方法,即大量完成手头的任务,然后通过低技术计时器获得平均时间(甚至是
time a.out
at shell).

您是否要求性能评测代码注入?例如,可通过
gprof
获得。您强制使用哪种工具链?@πάνταῥεῖ 我不知道这意味着什么。谷歌也没有返回任何定义。我需要知道每个排序算法需要多长时间。这需要微秒,但我仍然需要它来比较这三种排序算法。顺便说一下,我在Windows上。我不认为我必须使用任何工具,但它只需要在讲师运行时工作在Windows上,您可能无法为任何操作获得正确的微秒分辨率。(十分之一)毫秒是最佳选择。@πάνταῥεῖ 这就是我在研究如何做到这一点时所看到的。这就是为什么我问这个问题,尽管有这么多相同的问题(没有一个似乎足够,除非我遗漏了什么)。你的时钟源分辨率低并不重要。重复代码100万次,将测量值除以100万。谢谢。我使用了低技术计时器方法,这样它就可以在别人的电脑上运行。汉斯·帕桑也提出了这种方法。你们俩都很荣幸。