Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 90bb2db

Browse files
3.2 指定切片容量
1 parent 215cd24 commit 90bb2db

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package datastructure
2+
3+
import (
4+
"testing"
5+
)
6+
7+
const size = 1000000
8+
9+
func BenchmarkSliceWithoutCapacity(b *testing.B) {
10+
// Bad
11+
for n := 0; n < b.N; n++ {
12+
data := make([]int, 0)
13+
for k := 0; k < size; k++ {
14+
data = append(data, k)
15+
}
16+
}
17+
}
18+
19+
func BenchmarkSliceWithCapacity(b *testing.B) {
20+
// Good
21+
for n := 0; n < b.N; n++ {
22+
data := make([]int, 0, size)
23+
for k := 0; k < size; k++ {
24+
data = append(data, k)
25+
}
26+
}
27+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
![carbon](https://raw.githubusercontent.com/boatrainlsz/my-image-hosting/main/202203181109956.svg)

‎performance_rule/readme.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,11 @@ cpu: AMD Ryzen 5 4600U with Radeon Graphics
4848
| ------------------------------------------------------------ | ------------------------------------------------------------ |
4949
| [map_capacity_hint_test.go](datastructure/map_capacity_hint_test.go) | [map_capacity_hint_test.md](datastructure/map_capacity_hint_test.md) |
5050

51+
#### 3.2 指定切片容量
52+
53+
| 代码 | 验证结果 |
54+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
55+
| [slice_capacity_test.go](datastructure/slice_capacity_test.go) | [slice_capacity_test.md](datastructure/slice_capacity_test.md) |
56+
57+
58+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /