Skip to content

Instantly share code, notes, and snippets.

@qushot
Created March 11, 2019 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qushot/842565a25db4fce560c23541f650b03c to your computer and use it in GitHub Desktop.
Save qushot/842565a25db4fce560c23541f650b03c to your computer and use it in GitHub Desktop.
Increment vs Decrement
package increment_decrement
var (
min = 0
max = 10000000
)
func Increment() {
cnt := min
for i := min; i < max; i++{
cnt++
}
}
func Decrement() {
cnt := max
for i := max; i > min; i--{
cnt--
}
}
package increment_decrement
import "testing"
func BenchmarkIncrement(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
Increment()
}
}
func BenchmarkDecrement(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
Decrement()
}
}
❯❯❯ go test -bench . -benchmem
goos: darwin
goarch: amd64
pkg: github.com/qushot/sandbox/increment-decrement
BenchmarkIncrement-8 300 4861370 ns/op 0 B/op 0 allocs/op
BenchmarkDecrement-8 500 2367798 ns/op 0 B/op 0 allocs/op
PASS
ok github.com/qushot/sandbox/increment-decrement 3.404s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment