duanpie4763 2018-09-27 09:17

# 为什么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 为什么apriori的运行时间会比fp growth的运行时间短呢
• ¥15 帮我解决一下膳食平衡的线性规划模型的数据实例
• ¥40 万年历缺少农历，需要和阳历同时显示
• ¥250 雷电模拟器内存穿透、寻基址和特征码的教学
• ¥200 比特币ord程序wallet_constructor.rs文件支持一次性铸造1000个代币，并将它们分配到40个UTXO上（每个UTXO上分配25个代币），并设置找零地址
• ¥15 关于Java的学习问题
• ¥15 如何使用chatgpt完成文本分类任务？
• ¥15 已知速度v关于位置s的等式，怎么转化为已知位置求速度v的等式
• ¥15 我有个餐饮系统,用wampserver把环境配置好了,但是后端的网页却进去,是为什么,能不能帮远程一下？
• ¥15 R运行没有名称为"species"的插槽对于此对象类"SDMmodelCV"