Are your test results different  when rowSize = colSize    > 1000 say ??



On Sunday, September 29, 2019 at 10:18:15 AM UTC-4, zct wrote:
>
> The test code is below:
> package main
>
> import (
> "testing"
> )
>
> const rowSize = 1000000
> const colSize = 100
>
> var array [rowSize][colSize]int
>
> func BenchmarkRow(b *testing.B) {
> b.ResetTimer()
> for i := 0; i < b.N; i++ {
> sum := 0
> for r := 0; r < rowSize; r++ {
> for c := 0; c < colSize; c++ {
> sum += array[r][c]
> }
> }
> }
> }
>
> func BenchmarkColumn(b *testing.B) {
> b.ResetTimer()
> for i := 0; i < b.N; i++ {
> sum := 0
> for c := 0; c < colSize; c++ {
> for r := 0; r < rowSize; r++ {
> sum += array[r][c]
> }
> }
> }
> }
>
>
> As we known, there is a cpu cache in computer, so the row-wise should 
> perform better than the column-wise. But the test result is :
>
> go test -bench=. -count=5
> goos: darwin
> goarch: amd64
> BenchmarkRow-4                30          42926367 ns/op
> BenchmarkRow-4                30          50048505 ns/op
> BenchmarkRow-4                32          38466153 ns/op
> BenchmarkRow-4                28          40887279 ns/op
> BenchmarkRow-4                30          36325967 ns/op
> BenchmarkColumn-4             34          30991838 ns/op
> BenchmarkColumn-4             36          30965998 ns/op
> BenchmarkColumn-4             39          31575142 ns/op
> BenchmarkColumn-4             33          35048352 ns/op
> BenchmarkColumn-4             38          32584167 ns/op
> PASS
>
> It show that the column-wise traverse is quicker than the row-wise. I test 
> it in my macbook and ubuntu server, the result is the same
>
>

-- 
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/7424c9d6-51d0-4f59-81a4-b0635fc40530%40googlegroups.com.

Reply via email to