{
clock_t t1, t2;
double time1 = 0;
t1 = clock();
bubbleSort(data, n);
t2 = clock();
time1 = difftime(t2,t1);
cout<<"Bubble Sort: \n";
for (int y = 0; y<n; y++)
{
cout<<data[y]<<"\t";
}
cout<<endl;
cout<<"Algorithm Runtime is: "<<time1<<" milliseconds.";
}
Run Code Online (Sandbox Code Playgroud)
我试图获取运行时间(以毫秒为单位),但我总是得到0。只有输入1000+个随机数时,我才能获取运行时间。我想要得到的是数字较低的运行时间。谁能帮我。
我尝试了在该论坛中看到的所有其他方式,但对我来说似乎不起作用。也许我做错了方法。
先感谢您。
您可以进行更精确的时间测量。(纳秒)。需要C ++ 11进行编译。
例:
#include <chrono>
using namespace std;
{
auto t1 = chrono::high_resolution_clock::now();
bubbleSort(data, n);
auto t2 = chrono::high_resolution_clock::now();
chrono::duration<int64_t,nano> elapsed = t2 - t1; //if you want milliseconds you should use: std::chrono::duration<double,milli>
cout << "Bubble Sort: \n";
for (int y = 0; y<n; y++) {
cout << data[y] << "\t";
}
cout << endl;
cout << "Algorithm Runtime is: " << elapsed.count() << " nanoseconds.";
}
Run Code Online (Sandbox Code Playgroud)
希望我的回答对您有所帮助。