我希望计算API返回值所花费的时间.这种行动所花费的时间是纳秒秒.由于API是C++类/函数,我使用timer.h来计算相同的:
#include <ctime>
#include <cstdio>
using namespace std;
int main(int argc, char** argv) {
clock_t start;
double diff;
start = clock();
diff = ( std::clock() - start ) / (double)CLOCKS_PER_SEC;
cout<<"printf: "<< diff <<'\n';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码给出了以秒为单位的时间.如何在毫秒秒内获得相同的精度?
我真的不想描述,因为我想要在不同的简单功能上做很多不同的小基准测试.对于我的生活,我找不到一种方法来记录C++中的毫秒数,顺便说一句,我正在使用Linux.
你能否建议以毫秒为单位获取系统时钟的方法(如果我找不到简单的方法,我可以用几秒钟来解决......)以及它们包含在哪个标题中?
在通过TCP测量任何协议中的网络延迟(接收时间ack - 发送时间msg)时,您建议使用什么计时器?为什么?它有什么分辨率?还有什么其他优点/缺点?
可选:它是如何工作的?
可选:你不使用什么计时器,为什么?
我主要关注Windows/C++解决方案,但如果您想对其他系统发表评论,请随意这样做.
(目前我们使用GetTickCount(),但它不是一个非常准确的计时器.)
我必须计算冒泡排序需要多长时间并打印所需的时间.在我的程序中,打印的时间总是0.00秒.谁能告诉我我做错了什么?
int main()
{
srand((unsigned)time(NULL));
int arr[5000], arr2[5000];
int i;
time_t start, end;
double timeDiff;
for(i=0; i < 5000; i++)
{
arr[i] = rand() % 100 + 1;
arr2[i] = arr[i];
}
cout << "Here is the initial array:" << endl;
printArray(arr, 5000);
time(&start);
bubbleSort(arr, 5000);
time(&end);
timeDiff = difftime(end, start);
cout << "\nHere is the array after a bubble sort:" << endl;
printArray(arr, 5000);
cout << fixed << setprecision(2) << "\nIt took " << timeDiff << " seconds to …Run Code Online (Sandbox Code Playgroud) c++ ×4
c ×3
timer ×2
benchmarking ×1
bubble-sort ×1
clock ×1
gettimeofday ×1
latency ×1
linux ×1
sorting ×1
timing ×1
winapi ×1