A wrong benchmark function

Look at the Go code of the following benchmark function:

func BenchmarkFiboI(b *testing.B) { 
    for i := 0; i < b.N; i++ { 
        _ = fibo1(i) 
    } 
} 

The BenchmarkFibo() function has a valid name and the correct signature. The bad news, however, is that this benchmark function is wrong, and you will not get any output from it after executing the go test command!

The reason for this is that as the b.N value grows according to the way described in a previous section, the run time of the benchmark function will also increase because of the for loop. This fact prevents BenchmarkFiboI() from converging to a stable number, which prevents the function from completing and therefore returning.

For analogous reasons, the next benchmark function is also wrongly implemented:

func BenchmarkfiboII(b *testing.B) { 
    for i := 0; i < b.N; i++ { 
        _ = fibo2(b.N) 
    } 
}

Conversely, there is nothing wrong with the implementation of the following two benchmark functions:

func BenchmarkFiboIV(b *testing.B) { 
    for i := 0; i < b.N; i++ { 
        _ = fibo3(10) 
    } 
}

func BenchmarkFiboIII(b *testing.B) { _ = fibo3(b.N) }
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.23.147