Reputation: 41
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\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;
}
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.
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
Upvotes: 1
Views: 537
Reputation: 6666
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.
Upvotes: 5