snapshot of https://github.com/uber-go/guide.git (master, 2026-09-15; blobs > 131072B omitted)
This commit is contained in:
39
src/strconv.md
Normal file
39
src/strconv.md
Normal file
@@ -0,0 +1,39 @@
|
||||
# Prefer strconv over fmt
|
||||
|
||||
When converting primitives to/from strings, `strconv` is faster than
|
||||
`fmt`.
|
||||
|
||||
<table>
|
||||
<thead><tr><th>Bad</th><th>Good</th></tr></thead>
|
||||
<tbody>
|
||||
<tr><td>
|
||||
|
||||
```go
|
||||
for i := 0; i < b.N; i++ {
|
||||
s := fmt.Sprint(rand.Int())
|
||||
}
|
||||
```
|
||||
|
||||
</td><td>
|
||||
|
||||
```go
|
||||
for i := 0; i < b.N; i++ {
|
||||
s := strconv.Itoa(rand.Int())
|
||||
}
|
||||
```
|
||||
|
||||
</td></tr>
|
||||
<tr><td>
|
||||
|
||||
```plain
|
||||
BenchmarkFmtSprint-4 143 ns/op 2 allocs/op
|
||||
```
|
||||
|
||||
</td><td>
|
||||
|
||||
```plain
|
||||
BenchmarkStrconv-4 64.2 ns/op 1 allocs/op
|
||||
```
|
||||
|
||||
</td></tr>
|
||||
</tbody></table>
|
||||
Reference in New Issue
Block a user