Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/compile: Iterating a slice backwards is a tiny bit faster than forwards #35140

Closed
mlh758 opened this issue Oct 24, 2019 · 2 comments
Closed
Labels
FrozenDueToAge Performance WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Milestone

Comments

@mlh758
Copy link

mlh758 commented Oct 24, 2019

What version of Go are you using (go version)?

go version go1.12 darwin/amd64

Does this issue reproduce with the latest release?

Need to try.

What operating system and processor architecture are you using (go env)?

go env Output
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/hl/8pj040l94nlfzdfz8yzxjmr82d2t40/T/go-build481657979=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

package main

import "testing"

func BenchmarkCap(b *testing.B) {
	for i := 0; i < b.N; i++ {
		x := make([]string, 0, 500)
		for s := 0; s < 500; s++ {
			x = append(x, "taco")
		}
	}
}

func BenchmarkLen(b *testing.B) {
	for i := 0; i < b.N; i++ {
		x := make([]string, 500)
		for s := 0; s < 500; s++ {
			x[s] = "taco"
		}
	}
}

func BenchmarkLenReverse(b *testing.B) {
	for i := 0; i < b.N; i++ {
		x := make([]string, 500)
		for s := 499; s >= 0; s-- {
			x[s] = "taco"
		}
	}
}

What did you expect to see?

I figured the second and third benchmarks would be about the same

What did you see instead?

Going through the array in reverse is consistently a tiny bit faster. Is this just a quirk of my CPU architecture? I tried switching it around a little to use 1 instead of 0 for the comparison and that narrowed the gap slightly but in reverse was still consistently faster.

@dmitshur dmitshur added this to the Backlog milestone Oct 24, 2019
@dmitshur
Copy link
Contributor

Thanks for the report.

a tiny bit faster

Can you provide the results?

I suggest using benchstat to get a better sense of whether the difference is statistically significant.

@dmitshur dmitshur added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Oct 24, 2019
@dmitshur dmitshur changed the title Iterating a slice backwards is a tiny bit faster than forwards cmd/compile: Iterating a slice backwards is a tiny bit faster than forwards Oct 24, 2019
@mlh758
Copy link
Author

mlh758 commented Oct 24, 2019

When I run benchstat (20 runs) it seems to consistently reverse:

Len-8         146ns ± 1%
LenReverse-8  159ns ± 1%

It's probably just a fluke with the numbers being so small.

Edit: Bumping it up to 5000 items gives me

Len-8         16.3µs ± 3%
LenReverse-8  16.3µs ± 1%

I'm definitely looking at nothing here.

@mlh758 mlh758 closed this as completed Oct 24, 2019
@golang golang locked and limited conversation to collaborators Oct 23, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge Performance WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

3 participants