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

qsort 與 std::sort 的性能?

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

問題描述

根據(jù) Scott Meyers 的說法,在他的 Effective STL 書中 - 第 46 項(xiàng).他聲稱std::sortstd::qsort 由于內(nèi)聯(lián)的事實(shí).我測(cè)試了自己,發(fā)現(xiàn) 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 << "
";
}

這是我的結(jié)果:

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 條:將函數(shù)對(duì)象而不是函數(shù)視為算法參數(shù).

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

推薦答案

std::clock() 不是可行的計(jì)時(shí)時(shí)鐘.您應(yīng)該使用特定于平臺(tái)的更高分辨率計(jì)時(shí)器,例如 Windows 高性能計(jì)時(shí)器.更重要的是,您調(diào)用 clock() 的方式是首先將文本輸出到控制臺(tái),該控制臺(tái)包含在時(shí)間中.這肯定會(huì)使測(cè)試無效.此外,請(qǐng)確保您使用所有優(yōu)化進(jìn)行編譯.

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.

最后,我復(fù)制并粘貼了您的代碼,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.

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

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

相關(guān)文檔推薦

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++ 中旋轉(zhuǎn)圖像而不使用 OpenCV 函數(shù))
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 無法設(shè)置 SVM 參數(shù))
Convert a single color with cvtColor(使用 cvtColor 轉(zhuǎn)換單一顏色)
主站蜘蛛池模板: 中文字幕乱码一区二区三区 | 国产一区二区影院 | 91精品国产乱码久久蜜臀 | 91精品久久久久久综合五月天 | 国产精品a免费一区久久电影 | 精品国产一区二区三区成人影院 | 国产男女视频网站 | 黄色永久免费 | 国产成人麻豆免费观看 | 国产精品99视频 | 污污免费网站 | 亚洲精品在线免费 | 日本久草 | 欧美中文视频 | www.久久精品视频 | 性做久久久久久免费观看欧美 | 妹子干综合 | 国产精品一区二区三区在线播放 | 色综合视频 | 红色av社区| 亚洲a视频 | 精品国产乱码久久久久久闺蜜 | 看av网 | 亚洲精品乱码久久久久久蜜桃91 | 久久精品国产亚洲一区二区三区 | 国产精品96久久久久久 | 国产精品久久久久久久久久免费看 | 日韩欧美大片在线观看 | 国产精品18久久久久久白浆动漫 | 国内久久 | 亚洲 精品 综合 精品 自拍 | 国产毛片久久久久久久久春天 | av在线视| 91成人免费观看 | 久久最新网址 | 久久精品电影 | 久久久www | 日本a网站 | 日本精品裸体写真集在线观看 | 日韩精品一区二区三区在线播放 | 亚洲精品一区久久久久久 |