Sebastien is right! Thanks a lot! I forgot that the optimizer does eliminate some code at times. (and forgot this https://dave.cheney.net/2013/06/30/how-to-write-benchmarks-in-go)
This is the C code now (compiled with gcc -O2 run_std_pow.c -o run_std_pow -lm): #include "stdio.h" #include "math.h" int main() { double res = 0.0; double x = 2.5; int Nmax = 10000000; for (int N=0; N<Nmax; N++) { for (int i=0; i<20; i++) { res += pow(x, i); } } printf("res = %g\n", res); } and this is the Go code: package main import ( "fmt" "math" ) func main() { res := 0.0 x := 2.5 Nmax := 10000000 for N := 0; N < Nmax; N++ { for i := 0; i < 20; i++ { res += math.Pow(x, float64(i)) } } fmt.Printf("res = %g\n", res) } The C code runs on 8.262s and the Go code on 5.414s! Cheers. D On Friday, August 4, 2017 at 5:39:07 PM UTC+10, Sebastien Binet wrote: > > Dorival, > > On Fri, Aug 4, 2017 at 8:20 AM, Dorival Pedroso <dorival...@gmail.com > <javascript:>> wrote: > >> I've noticed that this C code: >> >> #include "math.h" >> int main() { >> double x = 2.5; >> int Nmax = 10000000; >> for (int N=0; N<Nmax; N++) { >> for (int i=0; i<20; i++) { >> pow(x, i); >> } >> } >> } >> >> can run up to 50x faster than this Go code: >> >> package main >> >> import "math" >> >> func main() { >> x := 2.5 >> Nmax := 10000000 >> for N := 0; N < Nmax; N++ { >> for i := 0; i < 20; i++ { >> math.Pow(x, float64(i)) >> } >> } >> } >> >> The C code was compiled with: gcc -O2 ccode.c -o ccode -lm >> then run with time ./ccode >> >> The Go code was compiled with: go build gcode.go >> then run with time ./gcode >> >> I've used the time command on Linux (Ubuntu) to get some estimate. >> >> So the question is: how can we make the Go code faster? >> > no, the question is whether you are really doing what you think you are > doing in C. > that's the usual question with benchmarking. > > with your initial C code, I get this on my machine: > > $> time ./c-code > real 0m0.261s > user 0m0.260s > sys 0m0.000s > > and this for the Go code: > > $> time ./go-code > real 0m12.781s > user 0m12.784s > sys 0m0.000s > > then, modifying the C code to make sure the compiler won't elide the pow > computation altogether: > > #include <math.h> > #include <stdio.h> > > int main() { > double sum = 0; > double x = 2.5; > int Nmax = 10000000; > for (int N=0; N<Nmax; N++) { > for (int i=0; i<20; i++) { > sum += pow(x, i); > } > } > printf("sum=%f\n", sum); > } > > I now get: > > $> time ./c-code-sum > sum=606329794183272.375000 > > real 0m18.385s > user 0m18.377s > sys 0m0.000s > > applying the same modifications to the Go code: > > $> time ./go-code-sum > > sum=6.063297941832724e+14 > > real 0m12.685s > user 0m12.673s > sys 0m0.010s > > -s > >> -- >> You received this message because you are subscribed to the Google Groups >> "golang-nuts" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to golang-nuts...@googlegroups.com <javascript:>. >> For more options, visit https://groups.google.com/d/optout. >> > > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.