当算法需要零 second/milli-second 次时,如何获得近似时间?
How to get approximate time, when an algorithm takes zero second/milli-second times?
我需要
的时间
- 1000
- 5000
- 10000
- 15000
- 20000 等等没有。的数据。
通过使用快速排序算法验证时间复杂度来自不。 Data 与 Times 图表。但是对于 1000 数据和 20000 数据,我仍然有 零秒 时间。如果我以 毫秒或纳秒 为单位测量时间,但时间仍然为零。有什么办法可以找到近似或比较时间不同否。数据的?
我的快速排序过程在这里 -
#include <bits/stdc++.h>
using namespace std;
int A[50000], i, j, V, store;
int part(int left, int right, int P)
{
V = A[P];
swap(A[P], A[right]);
store = left;
for(i = left; i < right; i++)
{
if(A[i] <= V)
{
swap(A[i], A[store]);
store++;
}
}
swap(A[store], A[right]);
return store;
}
void qSort(int left, int right)
{
if(left < right)
{
j = part(left, right, left);
qSort(left, j-1);
qSort(j+1, right);
}
}
main()
{
int nData, k, minValue, maxValue;
cout<<"No. of Data: ";
cin>>nData;
cout<<"\nRange (min, max): ";
cin>>minValue>>maxValue;
for(k=0; k<nData; k++)
{
A[k] = minValue + (rand()%(int) (maxValue - minValue + 1));
}
clock_t t1 = clock();
qSort(0, nData-1);
clock_t t2 = clock();
cout<<"\n\nTime: "<<(double)(t2-t1)/CLOCKS_PER_SEC<<endl;
}
[N.B: 我的操作系统是 Windows]
只需在循环次数足够多的for循环中重复要测量的任务,然后用你测量的时间除以迭代次数即可。
main()
{
...
clock_t t1 = clock();
qSort(0, nData-1);
clock_t t2 = clock();
cout<<"\n\nTime: "<<(double)(t2-t1)/CLOCKS_PER_SEC<<endl;
}
这里的问题是编译器对于这种简单的测试来说太聪明了。编译器看到对程序没有影响的代码,它通过删除不必要的代码来优化程序。您必须禁用优化(运行 调试模式下的程序可能会这样做)或修改程序,以便在某些方面使用排序操作的结果。
此外,clock()
在 Windows 和 POSIX 系统上的准确性不同。使用 std::chrono
更简单。例如
#include <iostream>
#include <chrono>
int main()
{
std::chrono::time_point<std::chrono::system_clock> start, end;
start = std::chrono::system_clock::now();
qSort(0, nData-1);
end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
std::cout << "count:" << elapsed_seconds.count() << "\n";
return 0;
}
我需要
的时间- 1000
- 5000
- 10000
- 15000
- 20000 等等没有。的数据。
通过使用快速排序算法验证时间复杂度来自不。 Data 与 Times 图表。但是对于 1000 数据和 20000 数据,我仍然有 零秒 时间。如果我以 毫秒或纳秒 为单位测量时间,但时间仍然为零。有什么办法可以找到近似或比较时间不同否。数据的?
我的快速排序过程在这里 -
#include <bits/stdc++.h>
using namespace std;
int A[50000], i, j, V, store;
int part(int left, int right, int P)
{
V = A[P];
swap(A[P], A[right]);
store = left;
for(i = left; i < right; i++)
{
if(A[i] <= V)
{
swap(A[i], A[store]);
store++;
}
}
swap(A[store], A[right]);
return store;
}
void qSort(int left, int right)
{
if(left < right)
{
j = part(left, right, left);
qSort(left, j-1);
qSort(j+1, right);
}
}
main()
{
int nData, k, minValue, maxValue;
cout<<"No. of Data: ";
cin>>nData;
cout<<"\nRange (min, max): ";
cin>>minValue>>maxValue;
for(k=0; k<nData; k++)
{
A[k] = minValue + (rand()%(int) (maxValue - minValue + 1));
}
clock_t t1 = clock();
qSort(0, nData-1);
clock_t t2 = clock();
cout<<"\n\nTime: "<<(double)(t2-t1)/CLOCKS_PER_SEC<<endl;
}
[N.B: 我的操作系统是 Windows]
只需在循环次数足够多的for循环中重复要测量的任务,然后用你测量的时间除以迭代次数即可。
main()
{
...
clock_t t1 = clock();
qSort(0, nData-1);
clock_t t2 = clock();
cout<<"\n\nTime: "<<(double)(t2-t1)/CLOCKS_PER_SEC<<endl;
}
这里的问题是编译器对于这种简单的测试来说太聪明了。编译器看到对程序没有影响的代码,它通过删除不必要的代码来优化程序。您必须禁用优化(运行 调试模式下的程序可能会这样做)或修改程序,以便在某些方面使用排序操作的结果。
此外,clock()
在 Windows 和 POSIX 系统上的准确性不同。使用 std::chrono
更简单。例如
#include <iostream>
#include <chrono>
int main()
{
std::chrono::time_point<std::chrono::system_clock> start, end;
start = std::chrono::system_clock::now();
qSort(0, nData-1);
end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
std::cout << "count:" << elapsed_seconds.count() << "\n";
return 0;
}