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 2f3da40

Browse files
5.2 []struct{}
1 parent 8962095 commit 2f3da40

File tree

4 files changed

+46
-5
lines changed

4 files changed

+46
-5
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package datastructure
2+
3+
import "testing"
4+
5+
type Item struct {
6+
id int
7+
val [1024]byte
8+
}
9+
10+
// genItems 生成指定长度 []*Item 切片
11+
func genItems(n int) []*Item {
12+
items := make([]*Item, 0, n)
13+
for i := 0; i < n; i++ {
14+
items = append(items, &Item{id: i})
15+
}
16+
return items
17+
}
18+
19+
func BenchmarkIndexPointer(b *testing.B) {
20+
items := genItems(1024)
21+
for i := 0; i < b.N; i++ {
22+
var tmp int
23+
for k := 0; k < len(items); k++ {
24+
tmp = items[k].id
25+
}
26+
_ = tmp
27+
}
28+
}
29+
30+
func BenchmarkRangePointer(b *testing.B) {
31+
items := genItems(1024)
32+
for i := 0; i < b.N; i++ {
33+
var tmp int
34+
for _, item := range items {
35+
tmp = item.id
36+
}
37+
_ = tmp
38+
}
39+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
![carbon (2)](https://raw.githubusercontent.com/boatrainlsz/my-image-hosting/main/202203192151611.svg)

‎performance_rule/datastructure/iterate_struct_test.go‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@ package datastructure
22

33
import "testing"
44

5-
type Item struct {
6-
id int
7-
val [1024]byte
8-
}
9-
105
func BenchmarkIndexStructSlice(b *testing.B) {
116
var items [1024]Item
127
for i := 0; i < b.N; i++ {

‎performance_rule/readme.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,9 @@ cpu: AMD Ryzen 5 4600U with Radeon Graphics
8282
| ------------------------------------------------------------ | ------------------------------------------------------------ |
8383
| [iterate_struct_test.go](datastructure/iterate_struct_test.go) | [iterate_struct_test.md](datastructure/iterate_struct_test.md) |
8484

85+
#### 5.3 []*struct,因为有了指针,index与range基本无差别
86+
87+
| 代码 | 验证结果 |
88+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
89+
| [iterate_struct_pointer_test.go](datastructure/iterate_struct_pointer_test.go) | [iterate_struct_pointer_test.md](datastructure/iterate_struct_pointer_test.md) |
90+

0 commit comments

Comments
(0)

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