Order of the code and performance

后端 未结 2 980
独厮守ぢ
独厮守ぢ 2020-11-30 14:57

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

2条回答
  •  时光取名叫无心
    2020-11-30 15:26

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

    Whenever you want to measure execution times, you should run it many times, measure the execution time of the multiple runs, and calculate average time. It's also a good idea to exclude the first (some) runs from the calculation for the above mentioned reasons.

    In Go, best and easiest is to use test files and benchmark functions. Read the package doc of testing for more details and examples.

    Your case can be benchmarked like this:

    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 < b.N; i++ {
            a[0] = 1
            a[1] = 2
            a[2] = 3
            a[3] = 4
        }
    }
    
    func BenchmarkStruct(b *testing.B) {
        a := abc{}
        for i := 0; i < b.N; i++ {
            a.a = 1
            a.b = 2
            a.c = 3
            a.d = 4
        }
    }
    

    Save it to 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 a struct is roughly 4 times faster. You will get similar (very close) results if you reorder the benchmark functions.

提交回复
热议问题