久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

qsort 與 std::sort 的性能?

Performance of qsort vs std::sort?(qsort 與 std::sort 的性能?)
本文介紹了qsort 與 std::sort 的性能?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

根據 Scott Meyers 的說法,在他的 Effective STL 書中 - 第 46 項.他聲稱std::sortstd::qsort 由于內聯的事實.我測試了自己,發現 qsort 更快 :( !誰能幫我解釋一下這種奇怪的行為?

According to Scott Meyers, in his Effective STL book - item 46. He claimed that std::sort is about 670% faster than std::qsort due to the fact of inline. I tested myself, and I saw that qsort is faster :( ! Could anyone help me to explain this strange behavior?

#include <iostream>
#include <vector>
#include <algorithm>

#include <cstdlib>
#include <ctime>
#include <cstdio>

const size_t LARGE_SIZE = 100000;

struct rnd {
    int operator()() {
        return rand() % LARGE_SIZE;
    }
};

int comp( const void* a, const void* b ) {
    return ( *( int* )a - *( int* )b );
}

int main() {
    int ary[LARGE_SIZE];
    int ary_copy[LARGE_SIZE];
    // generate random data
    std::generate( ary, ary + LARGE_SIZE, rnd() );
    std::copy( ary, ary + LARGE_SIZE, ary_copy );
    // get time
    std::time_t start = std::clock();
    // perform quick sort C using function pointer
    std::qsort( ary, LARGE_SIZE, sizeof( int ), comp );
    std::cout << "C quick-sort time elapsed: " << static_cast<double>( clock() - start ) / CLOCKS_PER_SEC << "
";
    // get time again
    start = std::clock();
    // perform quick sort C++ using function object
    std::sort( ary_copy, ary_copy + LARGE_SIZE );
    std::cout << "C++ quick-sort time elapsed: " << static_cast<double>( clock() - start ) / CLOCKS_PER_SEC << "
";
}

這是我的結果:

C quick-sort time elapsed: 0.061
C++ quick-sort time elapsed: 0.086
Press any key to continue . . .

更新

Effective STL 第三版(2001 年)
第 7 章 STL 編程
第 46 條:將函數對象而不是函數視為算法參數.

Effective STL 3rd Edition ( 2001 )
Chapter 7 Programming with STL
Item 46: Consider function objects instead of functions as algorithm parameters.

推薦答案

std::clock() 不是可行的計時時鐘.您應該使用特定于平臺的更高分辨率計時器,例如 Windows 高性能計時器.更重要的是,您調用 clock() 的方式是首先將文本輸出到控制臺,該控制臺包含在時間中.這肯定會使測試無效.此外,請確保您使用所有優化進行編譯.

std::clock() is not a viable timing clock. You should use a platform-specific higher resolution timer, like the Windows High Performance Timer. More than that, the way that you call clock() is that first, text is output to the console, which is included in the time. This definitely invalidates the test. In addition, make sure that you compiled with all optimizations.

最后,我復制并粘貼了您的代碼,qsort 為 0.016,std::sort 為 0.008.

Finally, I copied and pasted your code, and got 0.016 for qsort and 0.008 for std::sort.

這篇關于qsort 與 std::sort 的性能?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Assertion failed (size.widthgt;0 amp;amp; size.heightgt;0)(斷言失敗(size.width0 amp;amp; size.height0))
Rotate an image in C++ without using OpenCV functions(在 C++ 中旋轉圖像而不使用 OpenCV 函數)
OpenCV: process every frame(OpenCV:處理每一幀)
Why can#39;t I open avi video in openCV?(為什么我不能在 openCV 中打開 avi 視頻?)
OpenCV unable to set up SVM Parameters(OpenCV 無法設置 SVM 參數)
Convert a single color with cvtColor(使用 cvtColor 轉換單一顏色)
主站蜘蛛池模板: 国产欧美日韩精品一区 | 91亚洲国产成人久久精品网站 | 亚洲久久一区 | 国产不卡视频在线 | 99久久精品国产一区二区三区 | 91精品久久久久 | 五月婷婷视频 | 91动漫在线观看 | 国产精品国产a级 | 久久亚洲天堂 | 亚洲欧美综合 | 亚洲国产成人精品女人久久久 | 久草中文在线 | 成人福利视频网站 | 超碰免费在线观看 | av天天看 | 久久av一区二区 | 国产一区h | 国产日韩一区二区三免费 | 亚洲精品99| 操网站 | 四虎午夜剧场 | av影音在线 | 在线观看中文字幕视频 | 欧美99 | 91大神新作在线观看 | 黑人巨大精品欧美一区二区免费 | 国产精品免费一区二区三区 | 一区二区三区四区五区在线视频 | www..99re| 欧美激情综合 | 久久久久国产精品 | 日本一区二区高清视频 | 久久亚洲一区二区三区四区 | 久久高清 | 欧美精品一区二区三区一线天视频 | 请别相信他免费喜剧电影在线观看 | 精品免费视频 | 99国内精品久久久久久久 | 欧美精品久久久 | 在线一区二区三区 |