【问题标题】:Pre vs post increment operator profiling results前后增量运算符分析结果
【发布时间】:2023-03-17 10:10:02
【问题描述】:

我正在分析 C 中的 pre vs post 递增运算符(出于好奇,不是出于微优化目的!),我得到了一些令人惊讶的结果。我预计后增量运算符会更慢,但我所有的测试都表明它(非平凡)更快。我什至查看了在 gcc 中使用 -S 标志生成的汇编代码,并且 post 有一条额外的指令(如预期的那样)。谁能解释一下?我在 Arch Linux 上使用 gcc 4.8.1。

这是我的代码。

编辑:我的代码中有一个整数溢出错误。它不会影响问题,但您应该注意实际的迭代次数与传入的参数不同。

Post.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);
}

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

不久前我还写了一个timing script。运行结果相同:

$ 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 中,因为我不想在这篇文章中添加一些不必要的东西。

很抱歉再次引发这场争论,但这些数字对我来说似乎很奇怪。

【问题讨论】:

  • 使y volatile 并再次运行它只是为了好玩。之后,完全抛弃x,从两个for循环中删除增量步骤,只使用y = ++i;(显然分别使用i++)。我知道它在我的装备上做什么,对你的很好奇。 similar test 只是为了兴趣,虽然它是 C++ 代码。
  • @WhozCraig。很酷。时间几乎相同。
  • @WhozCraig 移除 volatile 变化不大。
  • 好吧,100000000000 不适合普通的int。在atoi() 后面加上printf("%d", iterations);,你就会明白我的意思了。
  • @Emmet,啊,这是一个愚蠢的错误。我应该从输出中意识到。不要认为它与问题有关,但无论如何我都会解决它。

标签: c performance profiling post-increment pre-increment


【解决方案1】:

有趣的问题。我是作为评论开始的,但它有点长。

所以,这就是我所做的,我像这样运行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

简而言之,我发现 Ivy Bridge CPU 和 gcc 4.6.3 在 Ubuntu 12.04 amd64 上的差异要小得多。

反汇编只显示了对指令的轻微重新排序,没有额外的指令(我想知道你为什么期望它)。

我的最佳猜测是,这是一个极其微妙的 CPU 问题,例如,可能是 µop 在预留站中多花一个周期等待操作数转发。这在我的设置中并不是很明显,这是肯定的。

【讨论】:

    猜你喜欢
    • 2016-02-02
    • 2011-03-12
    • 2012-10-17
    • 2015-08-20
    • 2015-10-16
    • 1970-01-01
    • 2017-06-30
    • 1970-01-01
    相关资源
    最近更新 更多