为什么 Golang 比 ANSI C 快,我该如何优化我的解决方案?

Adm*_*uth 1 c optimization performance pointers go

我编写了基准测试来检查 if 语句可以由GolangANSI C分别处理的速度。我试图保持相同的架构整体解决方案。

ANSI C 中的解决方案如下;

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

void bench(void (*f)(int));
void if_func_1(int i);
void if_func_2(int i);
void if_func_3(int i);

int main() {
        bench(&if_func_1);
        bench(&if_func_2);
        bench(&if_func_3);

        return 0;
}

void bench(void (*f)(int)) {
        int i;
        struct timespec start, end;
        float delta_us;

        clock_gettime(CLOCK_MONOTONIC_RAW, &start);

        for (i = 2147483647; -2147483648 != i; i--) {
            (*f)(i);
        }

        clock_gettime(CLOCK_MONOTONIC_RAW, &end);
        delta_us = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_nsec - start.tv_nsec) * 0.001;
        printf("%.3fms\n", delta_us * 0.001);
}

void if_func_1(int i) {
    if (0 == i) {
        return;
    }

    if (1 == i) {
        return;
    }

    if (2 == i) {
        return;
    }

    if (3 == i) {
        return;
    }

    return;
}

void if_func_2(int i) {
    if (0 == i) {
        return;
    } else if (1 == i) {
        return;
    } else if (2 == i) {
        return;
    } else if (3 == i) {
        return;
    }

    return;
}

void if_func_3(int i) {
    if (0 == i || 1 == i || 2 == i || 3 == i) {
        return;
    }

    return;
}
Run Code Online (Sandbox Code Playgroud)

结果如下:

~ time ./app.bin
20875.278ms
28766.584ms
16371.974ms
./app.bin  65.59s user 0.09s system 99% cpu 1:06.02 total
Run Code Online (Sandbox Code Playgroud)

正如我所料if_func_3,它是最快的,因为它实现了不同的逻辑。

Golang我的解决方案如下:

package main

import (
    "fmt"
    "time"
)

func main() {
    bench(if_func_1)
    bench(if_func_2)
    bench(if_func_3)
}

func bench(f func(int)) {
    var i int = 0

    start := time.Now();
    for i = 2147483647; -2147483648 != i; i-- {
        f(i)
    }

    elapsed := time.Since(start)
    fmt.Println(elapsed)

}

func if_func_1(i int) {
    if 0 == i {
        return
    }

    if 1 == i {
        return
    }

    if 2 == i {
        return
    }

    if 3 == i {
        return
    }

    return
}

func if_func_2(i int) {
    if 0 == i {
        return
    } else if 1 == i {
        return
    } else if 2 == i {
        return
    } else if 3 == i {
        return
    }

    return
}

func if_func_3(i int) {
    if 0 == i || 1 == i || 2 == i || 3 == i {
        return
    }

    return
}
Run Code Online (Sandbox Code Playgroud)

我可以在这里使用指针,因为它们在 Golang 中不存在。

结果相当混乱。

~> time go run app.go
11.595459054s
13.062146816s
14.504122183s
go run app.go  39.33s user 0.34s system 92% cpu 42.746 total
Run Code Online (Sandbox Code Playgroud)

是什么导致了这两种解决方案的这种差异?如何优化ANSI C解决方案以提高性能?

环境规格

系统MacOS

gcc 版本 10.0.0

转到版本 1.10.3

-ansi --pedantic -Wall标志编译。

概括

添加-O和更改琐碎return后打印一些文本。总执行时间已更改。

对于 ANSI C

From: System 99% cpu 1:06.02 total 
To: System 99% cpu 8.552 total
Run Code Online (Sandbox Code Playgroud)

对于 Golang

From: system 98% cpu 43.634 total 
To: system 92% cpu 42.746 total
Run Code Online (Sandbox Code Playgroud)

Arn*_*gel 5

所有经过测试的函数都非常等效于void no_op(int) {}. 大的时间差异是可能的,因为您在编译时没有进行优化,这使得您的基准测试结果充其量是可疑的。

Proper benchmarking requires turning on optimizations (i.e. -O or higher for GCC and Clang), and taking care that the relevant parts are, however, not optimized out. It can appear to be a simple problem, but is often surprisingly hard in practice. I recommend using a benchmarking library such as google benchmark to make the problem a bit more manageable.

I see that you updated your question with compiler version and settings, which is a good thing. Performance-related questions tend to have either somewhat or highly implementation-dependent answers, so this information should always be included in this kind of question (for that matter, it never hurts for any question involving a test program). You should also add the version of and switches for Golang that you are using.

  • `gcc ./app.c -O --ansi --pedantic -Wall` 在使用 `time ./a.out` 运行后给出 `./a.out 0.00s user 0.00s system 53% cpu 0.012 total`。 (2认同)