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 8f23900

Browse files
5.2 []struct{}
1 parent 7f9658b commit 8f23900

File tree

3 files changed

+49
-0
lines changed

3 files changed

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

‎performance_rule/readme.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,10 @@ cpu: AMD Ryzen 5 4600U with Radeon Graphics
7575
| 代码 | 验证结果 |
7676
| -------------------------------------------------------- | -------------------------------------------------------- |
7777
| [iterate_int_test.go](datastructure/iterate_int_test.go) | [iterate_int_test.md](datastructure/iterate_int_test.md) |
78+
79+
#### 5.2 []struct{},for性能更优
80+
81+
| 代码 | 验证结果 |
82+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
83+
| [iterate_struct_test.go](datastructure/iterate_struct_test.go) | [iterate_struct_test.md](datastructure/iterate_struct_test.md) |
84+

0 commit comments

Comments
(0)

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