Created
March 20, 2014 20:37
-
-
Save yifan-gu/9673297 to your computer and use it in GitHub Desktop.
slice vs list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package slice_vs_list | |
import ( | |
"container/list" | |
) | |
func runSliceAppend() { | |
s := make([]int, 0) | |
for i := 0; i < 1024; i++ { | |
s = append(s, i) | |
} | |
} | |
func runListPush() { | |
l := list.New() | |
for i := 0; i < 1024; i++ { | |
l.PushBack(i) | |
} | |
} | |
func runSliceAppendAgain() { | |
s := make([]int, 0) | |
for i := 0; i < 1024; i++ { | |
s = append(s, i) | |
} | |
s = s[:0] | |
for i := 0; i < 1024; i++ { | |
s = append(s, i) | |
} | |
} | |
func runListPushAgain() { | |
l := list.New() | |
for i := 0; i < 1024; i++ { | |
l.PushBack(i) | |
} | |
l.Init() | |
for i := 0; i < 1024; i++ { | |
l.PushBack(i) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package slice_vs_list | |
import ( | |
"testing" | |
) | |
func BenchmarkRunSliceAppend(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
runSliceAppend() | |
} | |
} | |
func BenchmarkRunList(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
runListPush() | |
} | |
} | |
func BenchmarkRunSliceAppendAgain(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
runSliceAppendAgain() | |
} | |
} | |
func BenchmarkRunListAgain(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
runListPushAgain() | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
result: | |
BenchmarkRunSliceAppend 200000 13563 ns/op | |
BenchmarkRunList 10000 101649 ns/op | |
BenchmarkRunSliceAppendAgain 100000 20668 ns/op | |
BenchmarkRunListAgain 10000 200949 ns/op | |
ok github.com/yifan-gu/slice_vs_list 8.167s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment