1. Do not use go run main.go. Never! 2. Do not run your code with the much less problematic go run because go run compiles and executes your code and the time needed to compile it will ruin all benchmarks. 3. Always use the benchmarking infrastructure built into go test and and package testing.
On Saturday, 20 February 2021 at 15:48:32 UTC+1 TiT8 wrote: > Hello everyone, > I'm Lorenzo and I'm not a computer scientist, I have used some "Matlab" > and I am new with Go (surprisingly fast, simple and elegant). > > I'm trying to do some basic operation on matrix with the "Gonum" package > using vectorization. I was curious about the speed of development and the > performance (repeat... surprisingly for me!). So I've tried to do some > measures: > > *package main* > > *import (* > * "fmt"* > * "time"* > > * "gonum.org/v1/gonum/mat <http://gonum.org/v1/gonum/mat>"* > *)* > > *func main() {* > * p := mat.NewDense(3, 3, nil)* > * a := mat.NewDense(3, 3, []float64{* > * 1, 2, 3, 1, 2, 3, 1, 2, 3,* > * })* > > * start := time.Now()* > > * p.Mul(a, a) *// element-wise > * fmt.Printf("%v\n", mat.Formatted(p, mat.Prefix(""), mat.Squeeze()))* > * p.Add(a, a) * > * fmt.Printf("%v\n", mat.Formatted(p, mat.Prefix(""), mat.Squeeze()))* > * p.MulElem(a, a)* > * fmt.Printf("%v\n", mat.Formatted(p, mat.Prefix(""), mat.Squeeze()))* > > * fmt.Println(time.Since(start))* > *}* > > > Well... when I run the "go run mat.go" command the result time is about > 150 microseconds, but when I run "go build" and then execute the binary the > result time is about 4 *milli*seconds. This happen when I use go1.16. > > When I use go1.15.8, on the same code, the exe is faster (about 120 > microseconds) then the "go run mat.go" (about 180 microseconds). One order > of magnitude faster than go1.16 exe (and basic Matlab). > > So: > > - am I in error with the code (sorry, it's all new for me)? If yes, > where is my fault? > - if the "benchmark" was correct, why go1.16 build executable is > slower than go1.15.8 exe and "go run" command? > - Is it my machine's fault or Gonum package or what? > - Do you also have these values? > > > My pc: HP ELITEBOOK 8560w Intel(R) Core(TM) i7-2670QM CPU @ 2.20GHz, 2201 > Mhz, 4 Core(s), 8 Logical Processor(s) x64. > I am on the *Windows Subsystem Linux 2. * > > > *Sorry for my English, I hope you understand my issue, thank you for the > attention* > > > > > > -- 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. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/9805b860-0a79-44ad-97d8-6ab16a181d8en%40googlegroups.com.