簡體   English   中英

為什么在這段代碼中,C ++中的排序比C中的qsort(對於字符串數組)要慢得多?

[英]why in this code the sort in C++ is much slower than qsort in C (for string array)?

《 C ++編程語言》(第4版)一書中, Stroustrup排序qsort快得多,但是在我的實驗中,我得到了不同的結果,有人可以幫助我解釋為什么嗎?

這是我的代碼:

double begin_time=0, end_time=0, elapsed_time=0;

vector<string> vs1 = { "fsfaa", "errer", "weesa", "yuyre", "wedsa", "xcxcx" };
begin_time = clock();

for (int i = 0; i < 1000000; i++)
{
    std::sort(vs1.begin(), vs1.end());
}

end_time = clock();
elapsed_time = double(end_time - begin_time) / CLOCKS_PER_SEC;

printf("\n\nElapsed time: %f\n", elapsed_time);

const char* vs[] = { "fsfaa", "errer", "weesa", "yuyre", "wedsa", "xcxcx" };
begin_time = clock();

for (int i = 0; i < 1000000; i++)
{
    std::qsort(vs, sizeof(vs) / sizeof(*vs), sizeof(*vs), (int(*)(const void*, const void*))compare_function);
}

end_time = clock();
elapsed_time = double(end_time - begin_time) / CLOCKS_PER_SEC;
printf("\n\nElapsed time: %f\n", elapsed_time);

和,

int compare_function(const void * a, const void * b) {
const char *pa = *(const char**)a;
const char *pb = *(const char**)b;

return strcmp(pa, pb);

}

結果如下(Windows / Visual Studio):

Elapsed time: 0.245000
Elapsed time: 0.090000

更新:感謝您的建議,我將代碼更改如下:

int compFunc_2(const void * a, const void * b) {
    const char *pa = *(char**)a;
    const char *pb = *(char**)b;

    return strcmp(pa, pb);
}

bool compFunc(const char *c1, const char *c2)
{
    return strcmp(c1, c2) < 0;
}

string gen_random_str(int len)
{
    string str;
    string w = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

    for (int i = 0; i < len; i++)
    {
        str += w[rand() % sizeof(w)];
    }
    return str;
}

char *convert(const std::string & s)
{
    char *pc = new char[s.size() + 1];
    strcpy(pc, s.c_str());
    return pc;
}


int main()
{

    srand((unsigned)time(0));
    const int str_num = 1000000;
    vector<string> vs;
    vector<char*>  vc;
    vector<char*>  vc2; //
    char**  as = new char*[str_num];

    for (int i = 0; i < str_num; i++)
    {
        string tmp = gen_random_str(10);  // length is 10
        vs.push_back(tmp);
        as[i] = new char(tmp.size() + 1);
        strcpy(as[i], tmp.c_str());
    }
    transform(vs.begin(), vs.end(), std::back_inserter(vc), convert);
    transform(vs.begin(), vs.end(), std::back_inserter(vc2), convert);



    double begin_time = 0, end_time, elapsed_time;

    begin_time = clock();
    std::qsort(&vc[0], str_num, sizeof(vc[0]), (int(*)(const void*, const void*))compFunc_2);
    end_time = clock();
    elapsed_time = double(end_time - begin_time) / CLOCKS_PER_SEC;
    printf("\n[1] vector<char*>(qsort) Elapsed time: %f", elapsed_time);

    begin_time = clock();
    std::sort(vc2.begin(), vc2.end(), compFunc);
    end_time = clock();
    elapsed_time = double(end_time - begin_time) / CLOCKS_PER_SEC;
    printf("\n[2] vector<char*>(sort) Elapsed time: %f", elapsed_time);



    printf("\nOther test:");
    begin_time = clock();
    std::qsort(as, str_num, sizeof(as[0]), (int(*)(const void*, const void*))compFunc_2);
    end_time = clock();
    elapsed_time = double(end_time - begin_time) / CLOCKS_PER_SEC;
    printf("\n[3] char array(qsort)   Elapsed time: %f", elapsed_time); 

    begin_time = clock();
    std::sort(vs.begin(), vs.end());
    end_time = clock();
    elapsed_time = double(end_time - begin_time) / CLOCKS_PER_SEC;
    printf("\n[4] vector<string>(sort) Elapsed time: %f", elapsed_time);

    // clean
    cin.get();
    return 0;
}

而且,我在計算機上得到了結果:

[1] vector<char*>(qsort) Elapsed time: 1.752000
[2] vector<char*>(sort) Elapsed time: 6.004000
Other test:
[3] char array(qsort)   Elapsed time: 1.691000
[4] vector<string>(sort) Elapsed time: 46.663000

有人說,是的,[1]和[3] / [4]之間沒有比較 (由於數據不同),但是我仍然對[1]和[2]感到困惑,為什么排序似乎仍然慢點? (我已經關閉了VS中的Optimize),任何幫助將不勝感激。

基准測試中有許多問題使您無法從中得出任何結論:

  1. 您可以將std::sort std::stringsstd::vectorqsortchar* s數組進行比較。 正確的基准應該使用相同的數據結構。

  2. 設置大小6不能代表基准幾個數量級。

  3. 兩種算法都進行了排序,因此盡管每個算法執行一百萬次迭代,但實際上只有第一個執行排序。 以下999999迭代“排序”已對數據進行排序。

  4. 您對排序后的數據沒有任何意義,因此編譯器甚至可能刪除您的代碼。

發現了問題,正如@ manni66指出的那樣,我犯了一個大錯誤。.我使用Debug模式,當我更改為Release模式時,結果是合理的:

[1] vector<char*>(qsort) Elapsed time: 0.559000
[2] vector<char*>(sort) Elapsed time: 0.530000
Other test:
[3] char array(qsort)   Elapsed time: 0.512000
[3] vector<string>(sort) Elapsed time: 0.620000

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM