Skip to content

Instantly share code, notes, and snippets.

@c1982
Created October 28, 2019 10:30
Show Gist options
  • Save c1982/940a0c035ca341a14c619f618bcb0181 to your computer and use it in GitHub Desktop.
Save c1982/940a0c035ca341a14c619f618bcb0181 to your computer and use it in GitHub Desktop.
package main
import "testing"
var testmk11 = []string{"scorpion", "sub-zero", "raiden", "kano", "kitana"}
func ifloop() {
for i := 0; i < len(testmk11); i++ {
if testmk11[i] == "scorpion" {
continue
}
if testmk11[i] == "sub-zero" {
continue
}
if testmk11[i] == "raiden" {
continue
}
if testmk11[i] == "kitana" {
continue
}
}
}
func iflooprange() {
for _, p := range testmk11 {
if p == "scorpion" {
continue
}
if p == "sub-zero" {
continue
}
if p == "raiden" {
continue
}
if p == "kitana" {
continue
}
}
}
func switchloop() {
for i := 0; i < len(testmk11); i++ {
switch testmk11[i] {
case "scorpion":
continue
case "sub-zero":
continue
case "raiden":
continue
case "kitana":
continue
}
}
}
func switchlooprange() {
for _, p := range testmk11 {
switch p {
case "scorpion":
continue
case "sub-zero":
continue
case "raiden":
continue
case "kitana":
continue
}
}
}
func BenchmarkIfLoop(b *testing.B) {
for i := 0; i < b.N; i++ {
ifloop()
}
}
func BenchmarkIfLoopRange(b *testing.B) {
for i := 0; i < b.N; i++ {
iflooprange()
}
}
func BenchmarkSwitchLoop(b *testing.B) {
for i := 0; i < b.N; i++ {
switchloop()
}
}
func BenchmarkSwitchLoopRange(b *testing.B) {
for i := 0; i < b.N; i++ {
switchlooprange()
}
}
goarch: amd64
pkg: console
BenchmarkIfLoop-16             	100000000	        13.5 ns/op	       0 B/op	       0 allocs/op
BenchmarkIfLoopRange-16        	100000000	        12.1 ns/op	       0 B/op	       0 allocs/op
BenchmarkSwitchLoop-16         	100000000	        15.5 ns/op	       0 B/op	       0 allocs/op
BenchmarkSwitchLoopRange-16    	100000000	        15.2 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	console	5.806s
Success: Benchmarks passed.```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment