Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 当算法需要0秒/毫秒的时间时,如何获得近似时间?_C++_Algorithm_Time_Time Complexity_Quicksort - Fatal编程技术网

C++ 当算法需要0秒/毫秒的时间时,如何获得近似时间?

C++ 当算法需要0秒/毫秒的时间时,如何获得近似时间?,c++,algorithm,time,time-complexity,quicksort,C++,Algorithm,Time,Time Complexity,Quicksort,我需要时间来学习 一千 五千 一万 15000 20000等数据数量。 通过使用快速排序算法从数据数量与时间图验证时间复杂度。但对于1000个数据和20000个数据,我仍然有零秒的时间。如果我用毫秒或纳秒来测量时间,但时间仍然是零。有没有办法找到不同数据数量的近似或比较时间 我的快速分拣流程如下- #include <bits/stdc++.h> using namespace std; int A[50000], i, j, V, store; int part(int l

我需要时间来学习

  • 一千
  • 五千
  • 一万
  • 15000
  • 20000等数据数量。

    通过使用快速排序算法从数据数量时间图验证时间复杂度。但对于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;    
}
#包括
使用名称空间std;
INTA[50000],i,j,V,store;
整数部分(整数左、整数右、整数P)
{
V=A[P];
掉期(A[P],A[权利]);
存储=左;
for(i=left;imaxValue;

对于(k=0;k只要在迭代次数足够多的for循环中重复要测量的任务,然后将测量的时间除以迭代次数。

只要在迭代次数足够多的for循环中重复要测量的任务,然后将测量的时间除以迭代次数。

可能重复的,我很难从中获得答案,我不知道如何。您可以复制粘贴接受的答案,只需更改函数调用。您的另一个选项是多次运行函数并取平均值,但精度较低。好的,我会尝试。谢谢您的建议。如果您使用可变大小的容器而不是数组然后用随机数据填充数据,你可以在命令行上设置大小,这样可以确保它不会被优化。也就是说,请将你的代码发布到,你的代码将受益匪浅。可能是重复的,我很难从中获得答案,我不知道怎么做。你可以复制粘贴接受的答案,只需更改函数即可调用。你的另一个选项是多次运行函数并取平均值,但精度较低。好的,我会尝试。谢谢你的建议。如果你使用可变大小的容器而不是数组,并用随机数据填充数据,你可以在命令行上设置大小,这样可以确保它不会被优化。也就是说,请将您的代码发布到,您的代码将受益匪浅。可能是一种方式。谢谢可能是一种方式。谢谢
main()
{
    ...
    clock_t t1 = clock();
    qSort(0, nData-1);
    clock_t t2 = clock();
    cout<<"\n\nTime: "<<(double)(t2-t1)/CLOCKS_PER_SEC<<endl;    
}
#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;
}