小编Adm*_*uth的帖子

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

我编写了基准测试来检查 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", …
Run Code Online (Sandbox Code Playgroud)

c optimization performance pointers go

1
推荐指数
1
解决办法
345
查看次数

标签 统计

c ×1

go ×1

optimization ×1

performance ×1

pointers ×1