Skip to content

Commit 85154b5

Browse files
aQuaaQua
aQua
authored and
aQua
committed
71 finish
1 parent 47cb38f commit 85154b5

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

Algorithms/0071.simplify-path/simplify-path.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,19 @@ func simplifyPath(path string) string {
99

1010
for i := 0; i < lp; i++ {
1111
// 使用前,清空 dir
12+
// 这个方法比 dir = []byte{} 快了近 8 倍
1213
dir = dir[:0]
13-
// 截取 dir
14+
// 获取 dir
1415
for i < lp && path[i] != '/' {
1516
dir = append(dir, path[i])
1617
i++
1718
}
1819

1920
s := string(dir)
21+
2022
switch s {
2123
case ".", "":
22-
continue
24+
// do nothing
2325
case "..":
2426
if len(stack) > 0 {
2527
// pop

Algorithms/0071.simplify-path/simplify-path_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,18 @@ func Test_Problem0071(t *testing.T) {
9090
ast.Equal(a.one, simplifyPath(p.path), "输入:%v", p)
9191
}
9292
}
93+
94+
func Benchmark_NewSlice(b *testing.B) {
95+
dir := make([]byte, 0, 3)
96+
dir = dir[:0]
97+
for i := 0; i < b.N; i++ {
98+
dir = []byte{}
99+
}
100+
}
101+
102+
func Benchmark_CutSlice(b *testing.B) {
103+
dir := make([]byte, 0, 3)
104+
for i := 0; i < b.N; i++ {
105+
dir = dir[:0]
106+
}
107+
}

0 commit comments

Comments
 (0)