Created
March 11, 2019 12:19
-
-
Save qushot/842565a25db4fce560c23541f650b03c to your computer and use it in GitHub Desktop.
Increment vs Decrement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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-- | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
❯❯❯ 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