簡體   English   中英

前后增量運算符概要分析結果

[英]Pre vs post increment operator profiling results

我在C中對前后增量運算符進行了性能分析(出於好奇,不是出於微優化目的!),我得到了一些令人驚訝的結果。 我希望后增量運算符會比較慢,但是我所有的測試都表明它(非平凡)更快。 我什至看過在gcc中使用-S標志生成的匯編代碼,並且post有一條額外的指令(如預期的那樣)。 誰能解釋一下? 我在Arch Linux上使用gcc 4.8.1。

這是我的代碼。

編輯:我的代碼中有一個整數溢出錯誤。 它不會影響該問題,但是您應該注意實際的迭代次數與傳入的參數不同。

郵政:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char** argv) {
    if (argc < 2) {
        printf("Missing required argument\n");
        exit(1);
    }
    int iterations = atoi(argv[1]);

    int x = 1;
    int y;

    int i;
    for (i = 0; i < iterations; i++) {
        y = x++;
    }

    printf("%d\n", y);
}

Pre.c:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char** argv) {
    if (argc < 2) {
        printf("Missing required argument\n");
        exit(1);
    }
    int iterations = atoi(argv[1]);

    int x = 1;
    int y;

    int i;
    for (i = 0; i < iterations; i++) {
        y = ++x;
    }

    printf("%d\n", y);
}

結果如下:

$ gcc post.c -o post
$ gcc pre.c -o pre
$ I=100000000000; time ./post $I; time ./pre $I
1215752192

real    0m2.777s
user    0m2.777s
sys     0m0.000s
1215752193

real    0m3.140s
user    0m3.137s
sys     0m0.003s

不久前,我還編寫了一個計時腳本 運行得到相同的結果:

$ I=100000000000; comptime "./pre $I" "./post $I"
3193 3133 3157 3143 3133 3153 3147 3150 3143 3146 3143
2743 2767 2700 2727 2700 2697 2727 2710 2680 2783 2700
Mean 1: 3149.18
Mean 2: 2721.27
SD 1: 6.1800
SD 2: 21.2700

輸出大部分是自解釋的,但其想法是,兩個程序都運行10次(默認情況下),並以毫秒為單位計算結果的平均值和標准偏差。

我已經將gcc為post.cpre.c生成的匯編代碼包括在pastebin上,因為我不想用不必要的東西弄亂這篇文章。

很抱歉再次引起這場辯論,但是這些數字對我來說似乎很奇怪。

有趣的問題。 我以評論開始,但是時間有點長。

所以,這里是我做什么,我跑prepost是這樣的:

$ for i in $(seq 1 10); do command time -f "%U" ./pre 1000000000 2> pre.times
$ echo 'd=load("pre.times"); min(d), mean(d), max(d), std(d)' | octave -q

我得到了(我用ans代替了它,以使其更具可讀性):

min =  2.2900
avg =  2.3550
max =  2.4000
std =  0.040893

我跑了類似的post並得到:

min =  2.1900
avg =  2.2590
max =  2.3800
std =  0.055668

簡而言之,我發現在Ubuntu 12.04 amd64上使用Ivy Bridge CPU和gcc 4.6.3的差異要小得多。

反匯編僅顯示對指令的輕微重新排序,而沒有額外的指令(我想知道為什么會這樣)。

我最好的猜測是,這是一個非常微妙的CPU問題,例如,一個µop在預留站中花了一個以上的周期來等待操作數轉發。 可以肯定的是,在我的設置中沒有什么超級明顯的。

暫無
暫無

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

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