C++ compute quick sort time cost via std::chrono which its precision can reach nanoseconds

 

 

void Util::arrayAsc61(int len)
{
    stringstream ss;
    int *arr = new int[len];
    getArray23(arr, len);
   
    chrono::time_point<chrono::system_clock> startTime;
    chrono::time_point<chrono::system_clock> endTime;
    startTime=chrono::system_clock::now();
    quickSortAsc61(arr, 0, len - 1);
    endTime=chrono::system_clock::now();
     
    ss<<getTimeNow()<<",len="<<len<<",quick sort time cost  "
    <<chrono::duration_cast<chrono::milliseconds>(endTime-startTime).count()<<" milliseconds,"
    <<chrono::duration_cast<chrono::microseconds>(endTime-startTime).count()<<" microseconds,"
    <<chrono::duration_cast<chrono::nanoseconds>(endTime-startTime).count()<<" nanoseconds"<<endl;

    logCost(ss.str());
    ss=stringstream(); 
    delete[] arr;
    cout<<getTimeNow()<<",len= "<<len << ",finished in quicksortasc!" << endl;
}



int Util::partitionAsc60(int *arr, int low, int high)
{
    int pivot = arr[high];
    int i = low - 1;
    for (int j = low; j < high; j++)
    {
        if (arr[j] < pivot)
        {
            i++;
            swap(&arr[i], &arr[j]);
        }
    }
    swap(&arr[i + 1], &arr[high]);
    return i + 1;
}

void Util::quickSortAsc61(int *arr, int low, int high)
{
    if (low < high)
    {
        int pivot = partitionAsc60(arr, low, high);
        quickSortAsc61(arr, low, pivot - 1);
        quickSortAsc61(arr, pivot + 1, high);
    }
}

#define interval 10000

void array41(int len)
{
    Util ul;
    for(int i=1;i<=len;i++)
    {
        int temp=interval*i;
        ul.arrayAsc61(temp);
    }    
}

 

Log the computed time cost via std::chrono and for loops to 0.1 B via interval 1000000

20220320150242,len=1000000,quick sort time cost 132 milliseconds,132652 microseconds,132652265 nanoseconds

20220320150243,len=2000000,quick sort time cost 281 milliseconds,281030 microseconds,281030367 nanoseconds

20220320150243,len=3000000,quick sort time cost 439 milliseconds,439106 microseconds,439106861 nanoseconds

20220320150244,len=4000000,quick sort time cost 572 milliseconds,572280 microseconds,572280175 nanoseconds

20220320150244,len=5000000,quick sort time cost 733 milliseconds,733061 microseconds,733061133 nanoseconds

20220320150245,len=6000000,quick sort time cost 894 milliseconds,894343 microseconds,894343836 nanoseconds

20220320150247,len=7000000,quick sort time cost 1076 milliseconds,1076861 microseconds,1076861355 nanoseconds

20220320150248,len=8000000,quick sort time cost 1217 milliseconds,1217709 microseconds,1217709597 nanoseconds

20220320150249,len=9000000,quick sort time cost 1364 milliseconds,1364847 microseconds,1364847775 nanoseconds

20220320150251,len=10000000,quick sort time cost 1507 milliseconds,1507015 microseconds,1507015482 nanoseconds

20220320150253,len=11000000,quick sort time cost 1679 milliseconds,1679238 microseconds,1679238520 nanoseconds

20220320150254,len=12000000,quick sort time cost 1823 milliseconds,1823466 microseconds,1823466420 nanoseconds

20220320150257,len=13000000,quick sort time cost 2009 milliseconds,2009727 microseconds,2009727640 nanoseconds

20220320150259,len=14000000,quick sort time cost 2161 milliseconds,2161303 microseconds,2161303766 nanoseconds

20220320150301,len=15000000,quick sort time cost 2333 milliseconds,2333749 microseconds,2333749211 nanoseconds

20220320150304,len=16000000,quick sort time cost 2545 milliseconds,2545475 microseconds,2545475839 nanoseconds

20220320150307,len=17000000,quick sort time cost 2641 milliseconds,2641509 microseconds,2641509826 nanoseconds

20220320150310,len=18000000,quick sort time cost 2840 milliseconds,2840411 microseconds,2840411626 nanoseconds

20220320150313,len=19000000,quick sort time cost 2986 milliseconds,2986846 microseconds,2986846788 nanoseconds

20220320150316,len=20000000,quick sort time cost 3160 milliseconds,3160135 microseconds,3160135339 nanoseconds

20220320150319,len=21000000,quick sort time cost 3404 milliseconds,3404818 microseconds,3404818202 nanoseconds

20220320150323,len=22000000,quick sort time cost 3652 milliseconds,3652556 microseconds,3652556583 nanoseconds

20220320150327,len=23000000,quick sort time cost 3670 milliseconds,3670290 microseconds,3670290607 nanoseconds

20220320150331,len=24000000,quick sort time cost 3854 milliseconds,3854643 microseconds,3854643684 nanoseconds

20220320150335,len=25000000,quick sort time cost 3973 milliseconds,3973167 microseconds,3973167198 nanoseconds

20220320150340,len=26000000,quick sort time cost 4150 milliseconds,4150737 microseconds,4150737154 nanoseconds

20220320150344,len=27000000,quick sort time cost 4380 milliseconds,4380022 microseconds,4380022497 nanoseconds

20220320150349,len=28000000,quick sort time cost 4526 milliseconds,4526395 microseconds,4526395497 nanoseconds

20220320150354,len=29000000,quick sort time cost 4663 milliseconds,4663012 microseconds,4663012557 nanoseconds

20220320150359,len=30000000,quick sort time cost 4866 milliseconds,4866101 microseconds,4866101374 nanoseconds

20220320150404,len=31000000,quick sort time cost 5191 milliseconds,5191899 microseconds,5191899221 nanoseconds

20220320150410,len=32000000,quick sort time cost 5271 milliseconds,5271076 microseconds,5271076776 nanoseconds

20220320150415,len=33000000,quick sort time cost 5337 milliseconds,5337743 microseconds,5337743066 nanoseconds

20220320150421,len=34000000,quick sort time cost 5713 milliseconds,5713630 microseconds,5713630925 nanoseconds

20220320150427,len=35000000,quick sort time cost 5789 milliseconds,5789991 microseconds,5789991122 nanoseconds

20220320150433,len=36000000,quick sort time cost 5907 milliseconds,5907050 microseconds,5907050029 nanoseconds

20220320150439,len=37000000,quick sort time cost 6031 milliseconds,6031685 microseconds,6031685648 nanoseconds

20220320150446,len=38000000,quick sort time cost 6317 milliseconds,6317187 microseconds,6317187688 nanoseconds

20220320150453,len=39000000,quick sort time cost 6440 milliseconds,6440320 microseconds,6440320773 nanoseconds

20220320150500,len=40000000,quick sort time cost 6631 milliseconds,6631095 microseconds,6631095634 nanoseconds

20220320150507,len=41000000,quick sort time cost 6787 milliseconds,6787037 microseconds,6787037133 nanoseconds

20220320150514,len=42000000,quick sort time cost 7028 milliseconds,7028024 microseconds,7028024239 nanoseconds

20220320150521,len=43000000,quick sort time cost 7273 milliseconds,7273294 microseconds,7273294065 nanoseconds

20220320150529,len=44000000,quick sort time cost 7337 milliseconds,7337668 microseconds,7337668036 nanoseconds

20220320150537,len=45000000,quick sort time cost 7598 milliseconds,7598468 microseconds,7598468340 nanoseconds

20220320150545,len=46000000,quick sort time cost 7704 milliseconds,7704384 microseconds,7704384395 nanoseconds

20220320150553,len=47000000,quick sort time cost 7978 milliseconds,7978738 microseconds,7978738261 nanoseconds

20220320150602,len=48000000,quick sort time cost 7991 milliseconds,7991760 microseconds,7991760998 nanoseconds

20220320150610,len=49000000,quick sort time cost 8321 milliseconds,8321373 microseconds,8321373443 nanoseconds

20220320150619,len=50000000,quick sort time cost 8327 milliseconds,8327991 microseconds,8327991261 nanoseconds

20220320150628,len=51000000,quick sort time cost 8650 milliseconds,8650581 microseconds,8650581672 nanoseconds

20220320150637,len=52000000,quick sort time cost 8638 milliseconds,8638851 microseconds,8638851018 nanoseconds

20220320150647,len=53000000,quick sort time cost 9474 milliseconds,9474441 microseconds,9474441032 nanoseconds

20220320150657,len=54000000,quick sort time cost 9741 milliseconds,9741559 microseconds,9741559772 nanoseconds

20220320150707,len=55000000,quick sort time cost 9947 milliseconds,9947016 microseconds,9947016070 nanoseconds

20220320150717,len=56000000,quick sort time cost 9988 milliseconds,9988346 microseconds,9988346157 nanoseconds

20220320150728,len=57000000,quick sort time cost 10383 milliseconds,10383135 microseconds,10383135138 nanoseconds

20220320150739,len=58000000,quick sort time cost 10500 milliseconds,10500201 microseconds,10500201401 nanoseconds

20220320150751,len=59000000,quick sort time cost 11106 milliseconds,11106484 microseconds,11106484936 nanoseconds

20220320150802,len=60000000,quick sort time cost 11183 milliseconds,11183753 microseconds,11183753454 nanoseconds

20220320150813,len=61000000,quick sort time cost 10335 milliseconds,10335506 microseconds,10335506886 nanoseconds

20220320150824,len=62000000,quick sort time cost 10671 milliseconds,10671625 microseconds,10671625903 nanoseconds

20220320150835,len=63000000,quick sort time cost 10749 milliseconds,10749037 microseconds,10749037718 nanoseconds

20220320150846,len=64000000,quick sort time cost 10827 milliseconds,10827354 microseconds,10827354147 nanoseconds

20220320150858,len=65000000,quick sort time cost 11065 milliseconds,11065510 microseconds,11065510761 nanoseconds

20220320150909,len=66000000,quick sort time cost 11139 milliseconds,11139806 microseconds,11139806281 nanoseconds

20220320150921,len=67000000,quick sort time cost 11565 milliseconds,11565759 microseconds,11565759417 nanoseconds

20220320150933,len=68000000,quick sort time cost 11631 milliseconds,11631571 microseconds,11631571298 nanoseconds

20220320150945,len=69000000,quick sort time cost 11641 milliseconds,11641353 microseconds,11641353709 nanoseconds

20220320150958,len=70000000,quick sort time cost 12045 milliseconds,12045582 microseconds,12045582773 nanoseconds

20220320151010,len=71000000,quick sort time cost 12107 milliseconds,12107102 microseconds,12107102186 nanoseconds

20220320151023,len=72000000,quick sort time cost 12331 milliseconds,12331676 microseconds,12331676476 nanoseconds

20220320151036,len=73000000,quick sort time cost 12433 milliseconds,12433887 microseconds,12433887608 nanoseconds

20220320151049,len=74000000,quick sort time cost 12641 milliseconds,12641244 microseconds,12641244122 nanoseconds

20220320151103,len=75000000,quick sort time cost 12833 milliseconds,12833388 microseconds,12833388327 nanoseconds

20220320151116,len=76000000,quick sort time cost 12896 milliseconds,12896952 microseconds,12896952058 nanoseconds

20220320151130,len=77000000,quick sort time cost 13715 milliseconds,13715013 microseconds,13715013672 nanoseconds

20220320151144,len=78000000,quick sort time cost 13277 milliseconds,13277596 microseconds,13277596234 nanoseconds

20220320151158,len=79000000,quick sort time cost 13503 milliseconds,13503335 microseconds,13503335836 nanoseconds

20220320151213,len=80000000,quick sort time cost 14570 milliseconds,14570663 microseconds,14570663078 nanoseconds

20220320151228,len=81000000,quick sort time cost 14792 milliseconds,14792340 microseconds,14792340874 nanoseconds

20220320151243,len=82000000,quick sort time cost 14712 milliseconds,14712903 microseconds,14712903631 nanoseconds

20220320151300,len=83000000,quick sort time cost 15736 milliseconds,15736077 microseconds,15736077747 nanoseconds

20220320151316,len=84000000,quick sort time cost 15915 milliseconds,15915605 microseconds,15915605640 nanoseconds

20220320151332,len=85000000,quick sort time cost 15648 milliseconds,15648457 microseconds,15648457619 nanoseconds

20220320151348,len=86000000,quick sort time cost 14856 milliseconds,14856935 microseconds,14856935254 nanoseconds

20220320151404,len=87000000,quick sort time cost 15124 milliseconds,15124742 microseconds,15124742106 nanoseconds

20220320151419,len=88000000,quick sort time cost 15230 milliseconds,15230924 microseconds,15230924369 nanoseconds

20220320151435,len=89000000,quick sort time cost 15403 milliseconds,15403497 microseconds,15403497395 nanoseconds

20220320151451,len=90000000,quick sort time cost 15548 milliseconds,15548871 microseconds,15548871399 nanoseconds

20220320151508,len=91000000,quick sort time cost 15910 milliseconds,15910616 microseconds,15910616701 nanoseconds

20220320151524,len=92000000,quick sort time cost 15966 milliseconds,15966494 microseconds,15966494127 nanoseconds

20220320151541,len=93000000,quick sort time cost 16049 milliseconds,16049839 microseconds,16049839818 nanoseconds

20220320151558,len=94000000,quick sort time cost 16470 milliseconds,16470821 microseconds,16470821206 nanoseconds

20220320151615,len=95000000,quick sort time cost 16594 milliseconds,16594037 microseconds,16594037072 nanoseconds

20220320151633,len=96000000,quick sort time cost 16724 milliseconds,16724537 microseconds,16724537137 nanoseconds

20220320151650,len=97000000,quick sort time cost 16633 milliseconds,16633384 microseconds,16633384341 nanoseconds

20220320151707,len=98000000,quick sort time cost 17010 milliseconds,17010934 microseconds,17010934218 nanoseconds

20220320151726,len=99000000,quick sort time cost 17609 milliseconds,17609849 microseconds,17609849094 nanoseconds

20220320151745,len=100000000,quick sort time cost 18415 milliseconds,18415007 microseconds,18415007737 nanoseconds

 

Interval 10000000

20220320151922,len=10000000,quick sort time cost 1534 milliseconds,1534054 microseconds,1534054506 nanoseconds

20220320151926,len=20000000,quick sort time cost 3975 milliseconds,3975644 microseconds,3975644901 nanoseconds

20220320151932,len=30000000,quick sort time cost 5115 milliseconds,5115940 microseconds,5115940785 nanoseconds

20220320151939,len=40000000,quick sort time cost 6980 milliseconds,6980709 microseconds,6980709448 nanoseconds

20220320151948,len=50000000,quick sort time cost 8676 milliseconds,8676228 microseconds,8676228540 nanoseconds

20220320151959,len=60000000,quick sort time cost 10545 milliseconds,10545304 microseconds,10545304695 nanoseconds

20220320152012,len=70000000,quick sort time cost 12121 milliseconds,12121006 microseconds,12121006862 nanoseconds

20220320152026,len=80000000,quick sort time cost 14444 milliseconds,14444869 microseconds,14444869104 nanoseconds

20220320152043,len=90000000,quick sort time cost 16239 milliseconds,16239391 microseconds,16239391001 nanoseconds

20220320152102,len=100000000,quick sort time cost 17970 milliseconds,17970804 microseconds,17970804992 nanoseconds

 

posted @ 2022-03-20 14:44  FredGrit  阅读(81)  评论(0编辑  收藏  举报