-
Notifications
You must be signed in to change notification settings - Fork 20.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cmd: use errrors.New instead of empty fmt.Errorf #27329
Conversation
Signed-off-by: jsvisa <[email protected]>
I played around a bit with your example code. It's not really that package main
import (
"errors"
"fmt"
"time"
)
func testErrors(txt string) error {
return errors.New(txt)
}
func testFmt(txt string) error {
return fmt.Errorf(txt)
}
func main() {
repeat := 100000000
start := time.Now()
var a, b error
var c string
for i := 0; i < repeat; i++ {
a = testErrors("test")
}
duration := time.Since(start)
fmt.Println("errors.New", duration)
start = time.Now()
for i := 0; i < repeat; i++ {
b = fmt.Errorf("test")
}
duration = time.Since(start)
fmt.Println("fmt.Errorf", duration)
//
start = time.Now()
for i := 0; i < repeat; i++ {
c = fmt.Sprintf("test")
}
duration = time.Since(start)
fmt.Println("fmt.Sprintf", duration)
fmt.Printf(c)
fmt.Printf(a.Error())
fmt.Printf(b.Error())
} Output:
So ~5s is added to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, it's the right thing to do, even if the article is a bit flawed and the results are is a bit misleading
Thanks for this tip. It seems that the return value of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Signed-off-by: jsvisa <[email protected]>
)" This reverts commit 13b2b9f.
)" This reverts commit 13b2b9f.
WIP: use errrors.New instead of empty fmt.Errorf (ethereum#27329)
Seems
errors.New
is super fast then the emptyfmt.Errorf
, here is an benchmark test case from https://blog.imberkay.com/errorsnew-vs-fmterrorfIn my macbook air m1, the result as below: