cmd: use errrors.New instead of empty fmt.Errorf#27329
Conversation
Signed-off-by: jsvisa <delweng@gmail.com>
|
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 |
holiman
left a comment
There was a problem hiding this comment.
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 |
Signed-off-by: jsvisa <delweng@gmail.com>
)" This reverts commit 13b2b9f.
)" This reverts commit 13b2b9f.
WIP: use errrors.New instead of empty fmt.Errorf (ethereum#27329)
Signed-off-by: jsvisa <delweng@gmail.com>
Seems
errors.Newis 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: