Code Order and Performance

I wanted to find which is faster: struct vs array. So I wrote GO code in which I write 4 int values ​​(1,2,3 and 4) to the members of the structure, and then an array of length 4. I tried to find the time it takes to write.

Case 1: First, I write the values ​​to a structure and then to an array. Here I found an array faster than a structure.

package main import ( "fmt" "time" ) type abc struct { a, b, c, d int } func main() { var obj abc t1 := time.Now() obj.a = 1 obj.b = 2 obj.c = 3 obj.d = 4 t2 := time.Since(t1) fmt.Println("Struct access time: : ", t2) a := make([]int, 4) t3 := time.Now() a[0] = 1 a[1] = 2 a[2] = 3 a[3] = 4 t4 := time.Since(t3) fmt.Println("Array access time: : ", t4) } 

Case 2: Secondly, I write the values ​​to an array and then to a structure. Here I found a structure faster than an array.

 package main import ( "fmt" "time" ) type abc struct { a, b, c, d int } func main() { var obj abc a := make([]int, 4) t3 := time.Now() a[0] = 1 a[1] = 2 a[2] = 3 a[3] = 4 t4 := time.Since(t3) fmt.Println("Array access time: : ", t4) t1 := time.Now() obj.a = 1 obj.b = 2 obj.c = 3 obj.d = 4 t2 := time.Since(t1) fmt.Println("Struct access time: : ", t2) } 

Why does performance depend on what I write first? The one I write first seems slower. Why is this so?

+1
source share
2 answers

Another answer explained the time difference, let's get into struct vs. slice.

If the compiler can determine at compile time that the slice is large enough, access to the slice and structure elements will generate identical code. Of course, in fact, often the compiler does not know how big a slice is, and completely different optimizations will be applied depending on whether you work with a structure or a slice, so to measure performance you need to look at the whole program and its behavior, and not only one specific operation.

+1
source

Running any code for the first time may have some (significant) overhead, for example. related code can be loaded, many things can be delayed until they are needed (for example, internal buffers). Starting the same again can take significantly less time, the difference can even be several orders of magnitude.

Whenever you want to measure the execution time, you have to run it many times, measure the execution time of several runs and calculate the average time. It is also a good idea to exclude the first (some) mileage from the calculation for the above reasons.

Go is the best and easiest way to use test files and control functions. See the doc testing document for more details.

Your case can be compared as follows:

 package main import "testing" type abc struct { a, b, c, d int } func BenchmarkSlice(b *testing.B) { a := make([]int, 4) for i := 0; i < bN; i++ { a[0] = 1 a[1] = 2 a[2] = 3 a[3] = 4 } } func BenchmarkStruct(b *testing.B) { a := abc{} for i := 0; i < bN; i++ { aa = 1 ab = 2 ac = 3 ad = 4 } } 

Save it in a file like something_test.go , run it with go test -bench . . Output:

 BenchmarkSlice-4 2000000000 1.24 ns/op BenchmarkStruct-4 2000000000 0.31 ns/op 

You can see that using the structure is about 4 times faster. You will get similar (very close) results if you reorder the reference functions.

+9
source

Source: https://habr.com/ru/post/1265567/


All Articles