程序执行时间极短
Program execution time for extremely small run times
我查找了一堆其他如何对代码进行计时的示例,但使用chrono或time的示例似乎都不起作用(它们返回0)。但是,什么确实有效 QueryPerformanceCounter.使用它的唯一缺点是它仅适用于我阅读的 Windows。我的教练使用Mac,所以我不能把这个代码交给他。这是我使用查询性能计数器时的代码的样子。
#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;
}
如果所有平台都运行英特尔,您可以考虑使用时间戳计数器 (RDTSC),并围绕此编写自己的性能计数器。但它不是很便携,并且有各种棘手的问题(内核之间的漂移、可变时钟频率,除非您禁用了它等)。通常,我选择低技术方法,即大量完成手头的任务,然后通过低技术计时器(甚至在外壳上time a.out
)获得平均时间。
相关文章:
- 在C/C++中执行"_dl_init"(程序初始化)是单线程的吗
- 简单C++"Hello World"程序的执行时间长
- cmd.exe与Powershell中C++程序的不同执行时间
- OpenCV 函数 cv::remap() 的执行时间更长,当程序在两者之间进入睡眠状态时
- 为什么 C++ openMP 程序执行时间更长
- 如何在 ubuntu 上的 php 脚本中获取程序(c,c++,java,python,php)的执行时间和内存使用量?
- 测量执行时间 - 在程序代码或外壳中
- CUDA 程序不测量执行时间:cuda事件记录
- C程序的示例长时间执行时间
- 计算我的c++程序的执行时间和大O时间
- 程序执行时间极短
- 当我从Java代码执行C++程序时,我应该如何测量它的执行时间
- 程序执行时间计数器
- 为什么一个子线程的执行时间多于整个应用程序的执行时间
- 为什么我的程序中的变量占用 60% 的执行时间
- 在linux中,与动态调用相关的时钟周期中程序的意外执行时间
- 为什么每次执行这个程序的CPU时间都不一样
- C++嵌入式系统上的程序执行时间估计
- 程序的执行时间为"5E-006" ?
- 多线程程序比单线程程序执行时间更长