Last active
August 25, 2017 08:50
-
-
Save deadcheat/7aa43d113027e39ec33c8c721f3f4006 to your computer and use it in GitHub Desktop.
Bool判定 VS ErrNil判定
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 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