It looks CSE doesn't work for the following case.

package main

import (
    "testing"
)

var v1a int
func Benchmark_CSE_Case_1a(b *testing.B) {
    s := make([]Element, N)
    b.ResetTimer()
    for i := 0; i < b.N; i++ {
        var x, y, z = 123, 456, 789
        v1a = (y+z) / x + (y+z) / x + (y+z) / x
    }
}

func Benchmark_CSE_Case_1b(b *testing.B) {
    s := make([]Element, N)
    b.ResetTimer()
    for i := 0; i < b.N; i++ {
        var x, y, z = 123, 456, 789
        t := y+z) / x
        v1a = t + t + t
    }
}

The result:

$ go test -bench=.
goos: linux
goarch: amd64
Benchmark_SumArrayElements_GlobalVar-4           5000        201535 ns/op
Benchmark_SumArrayElements_LocalVar-4           30000         39922 ns/op

-- 
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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to