duanpie4763 2018-09-27 09:17
浏览 73
已采纳

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

I wrote benchmarks to check how fast if statements can be handled by Golang and ANSI C respectively. I was trying to keep the same schema overall solutions.

Solution in ANSI C is following;

#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
", 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;
}

The results were folowing:

~ time ./app.bin
20875.278ms
28766.584ms
16371.974ms
./app.bin  65.59s user 0.09s system 99% cpu 1:06.02 total

As I expected if_func_3 was the fastest one because it implements different logic.

In Golang my solutions is following:

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
}

I could use pointers here because they don't exist in Golang.

Resutls are quite confusing.

~> 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

What causes such difference in these two solutions? How can I optimize ANSI C solution to perform better?

Enviroment specification

System MacOS

gcc version 10.0.0

go version 1.10.3

Compiled with -ansi --pedantic -Wall flags.

Summary

After adding -O and changing the trivial return to print some text. Total execution timings have changed.

For ANSI C

From: System 99% cpu 1:06.02 total 
To: System 99% cpu 8.552 total

For Golang

From: system 98% cpu 43.634 total 
To: system 92% cpu 42.746 total
  • 写回答

1条回答 默认 最新

  • dongtuo5262 2018-09-27 09:31
    关注

    All your tested functions are trivially equivalent to void no_op(int) {}. The large timing differences are just possible because you are compiling without optimizations, which makes your benchmark results dubious at best.

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Stata链式中介效应代码修改
  • ¥15 latex投稿显示click download
  • ¥15 请问读取环境变量文件失败是什么原因?
  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错