On Tue, Feb 18, 2020 at 11:10 AM Vincent Blanchon
<blanchon.vinc...@gmail.com> wrote:

> @Jan Thank you for the feedback. I will try with a higher number of 
> constants. By the way, my benchmark tries all the path:
>
> func BenchmarkStringerWithSwitch(b *testing.B) {
>    for i := 0; i < b.N ; i++  {
>       p := Pill(i%20)
>       _ = p.String()
>    }
> }

That's IMO a very good approach. However, there's one potential trap.
The `_ = f()` thing may, or may not, get optimized away completely,
depending on the particular compiler and its version, if the compiler
can prove the called function is side effects free. The usual
workaround is, for example, to actually assign the returned value to a
package level variable _and_ read it afterwards. Like in

        foo = p.String()
        ..

and later, just before returning

        if foo == "" { // Assuming none of the string represenations are ""
                b.Fatal()
        }

-- 
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/CAA40n-XYxgR_D8XAtFX6JEd4%2BLAiTQumYxPZxdzH3vcVd1wi%3DQ%40mail.gmail.com.

Reply via email to