Skip to content

Instantly share code, notes, and snippets.

@deadcheat
Last active August 25, 2017 08:50
Show Gist options
  • Save deadcheat/7aa43d113027e39ec33c8c721f3f4006 to your computer and use it in GitHub Desktop.
Save deadcheat/7aa43d113027e39ec33c8c721f3f4006 to your computer and use it in GitHub Desktop.
Bool判定 VS ErrNil判定
package main
import (
"errors"
"fmt"
"time"
)
func main() {
boolWin := 0
errWin := 0
for j := 0; j < 10000; j++ {
start1 := time.Now()
for i := 0; i < 1000000; i++ {
ok, err := test()
if !ok {
_ = err
_ = ok
}
}
time1 := time.Since(start1)
start2 := time.Now()
for i := 0; i < 1000000; i++ {
ok, err := test()
if err != nil {
_ = err
_ = ok
}
}
time2 := time.Since(start2)
if time1 == time2 {
continue
} else if time1 < time2 {
boolWin++
} else {
errWin++
}
}
fmt.Println(boolWin, errWin)
}
var (
ErrorTest = errors.New("test")
)
func test() (ok bool, err error) {
return false, ErrorTest
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment