On Wednesday, December 14, 2016 at 10:12:08 PM UTC+8, T L wrote:
>
> I just read this issue thread: https://github.com/golang/go/issues/5373
> and this https://codereview.appspot.com/137880043
> which says:
>
> for i := range a {
> a[i] = [zero val]
> }
>
> will be replaced with memclr.
> I made some benchmarks, but the results are disappointing.
> When the length of slice/array is very large, memclr is slower.
>
> Result
>
> BenchmarkMemclr_100-4 100000000 37.2 ns/op
> BenchmarkLoop_100-4 100000000 70.7 ns/op
> BenchmarkMemclr_1000-4 20000000 351 ns/op
> BenchmarkLoop_1000-4 10000000 464 ns/op
> BenchmarkMemclr_10000-4 1000000 3623 ns/op
> BenchmarkLoop_10000-4 1000000 4940 ns/op
> BenchmarkMemclr_100000-4 100000 49230 ns/op
> BenchmarkLoop_100000-4 100000 58761 ns/op
> BenchmarkMemclr_200000-4 50000 98165 ns/op
> BenchmarkLoop_200000-4 50000 115833 ns/op
> BenchmarkMemclr_300000-4 30000 170617 ns/op
> BenchmarkLoop_300000-4 20000 190193 ns/op
> BenchmarkMemclr_400000-4 20000 275676 ns/op
> BenchmarkLoop_400000-4 20000 288729 ns/op
> BenchmarkMemclr_500000-4 10000 410280 ns/op
> BenchmarkLoop_500000-4 10000 416195 ns/op
> BenchmarkMemclr_1000000-4 5000 1025504 ns/op
> BenchmarkLoop_1000000-4 5000 1012198 ns/op
> BenchmarkMemclr_2000000-4 2000 2071861 ns/op
> BenchmarkLoop_2000000-4 2000 2032703 ns/op
>
> test code:
>
> package main
>
> import "testing"
>
> func memclr(a []int) {
> for i := range a {
> a[i] = 0
> }
> }
>
> func memsetLoop(a []int, v int) {
> for i := range a {
> a[i] = v
> }
> }
>
> var i = 0
>
> func BenchmarkMemclr_100(b *testing.B) {
> var a = make([]int, 100)
> b.ResetTimer()
> for i := 0; i < b.N; i++ {
> memclr(a)
> }
> }
>
> func BenchmarkLoop_100(b *testing.B) {
> var a = make([]int, 100)
> b.ResetTimer()
> for i := 0; i < b.N; i++ {
> memsetLoop(a, i)
> }
> }
>
> func BenchmarkMemclr_1000(b *testing.B) {
> var a = make([]int, 1000)
> b.ResetTimer()
> for i := 0; i < b.N; i++ {
> memclr(a)
> }
> }
>
> func BenchmarkLoop_1000(b *testing.B) {
> var a = make([]int, 1000)
> b.ResetTimer()
> for i := 0; i < b.N; i++ {
> memsetLoop(a, i)
> }
> }
>
> func BenchmarkMemclr_10000(b *testing.B) {
> var a = make([]int, 10000)
> b.ResetTimer()
> for i := 0; i < b.N; i++ {
> memclr(a)
> }
> }
>
> func BenchmarkLoop_10000(b *testing.B) {
> var a = make([]int, 10000)
> b.ResetTimer()
> for i := 0; i < b.N; i++ {
> memsetLoop(a, i)
> }
> }
>
> func BenchmarkMemclr_100000(b *testing.B) {
> var a = make([]int, 100000)
> b.ResetTimer()
> for i := 0; i < b.N; i++ {
> memclr(a)
> }
> }
>
> func BenchmarkLoop_100000(b *testing.B) {
> var a = make([]int, 100000)
> b.ResetTimer()
> for i := 0; i < b.N; i++ {
> memsetLoop(a, i)
> }
> }
>
> func BenchmarkMemclr_200000(b *testing.B) {
> var a = make([]int, 200000)
> b.ResetTimer()
> for i := 0; i < b.N; i++ {
> memclr(a)
> }
> }
>
> func BenchmarkLoop_200000(b *testing.B) {
> var a = make([]int, 200000)
> b.ResetTimer()
> for i := 0; i < b.N; i++ {
> memsetLoop(a, i)
> }
> }
>
> func BenchmarkMemclr_300000(b *testing.B) {
> var a = make([]int, 300000)
> b.ResetTimer()
> for i := 0; i < b.N; i++ {
> memclr(a)
> }
> }
>
> func BenchmarkLoop_300000(b *testing.B) {
> var a = make([]int, 300000)
> b.ResetTimer()
> for i := 0; i < b.N; i++ {
> memsetLoop(a, i)
> }
> }
>
> func BenchmarkMemclr_400000(b *testing.B) {
> var a = make([]int, 400000)
> b.ResetTimer()
> for i := 0; i < b.N; i++ {
> memclr(a)
> }
> }
>
> func BenchmarkLoop_400000(b *testing.B) {
> var a = make([]int, 400000)
> b.ResetTimer()
> for i := 0; i < b.N; i++ {
> memsetLoop(a, i)
> }
> }
>
> func BenchmarkMemclr_500000(b *testing.B) {
> var a = make([]int, 500000)
> b.ResetTimer()
> for i := 0; i < b.N; i++ {
> memclr(a)
> }
> }
>
> func BenchmarkLoop_500000(b *testing.B) {
> var a = make([]int, 500000)
> b.ResetTimer()
> for i := 0; i < b.N; i++ {
> memsetLoop(a, i)
> }
> }
>
> func BenchmarkMemclr_1000000(b *testing.B) {
> var a = make([]int, 1000000)
> b.ResetTimer()
> for i := 0; i < b.N; i++ {
> memclr(a)
> }
> }
>
> func BenchmarkLoop_1000000(b *testing.B) {
> var a = make([]int, 1000000)
> b.ResetTimer()
> for i := 0; i < b.N; i++ {
> memsetLoop(a, i)
> }
> }
>
> func BenchmarkMemclr_2000000(b *testing.B) {
> var a = make([]int, 2000000)
> b.ResetTimer()
> for i := 0; i < b.N; i++ {
> memclr(a)
> }
> }
>
> func BenchmarkLoop_2000000(b *testing.B) {
> var a = make([]int, 2000000)
> b.ResetTimer()
> for i := 0; i < b.N; i++ {
> memsetLoop(a, i)
> }
> }
>
>
>
I'm sorry, there is a mistake in the test code, a fixed version:
package main
import "testing"
type MyInt int32
var initialValue MyInt = 0
func memclr(a []MyInt) {
for i := range a {
a[i] = 0
}
}
func memsetLoop(a []MyInt, v MyInt) {
for i := range a {
a[i] = v
}
}
func BenchmarkMemclr_10(b *testing.B) {
var a = make([]MyInt, 10)
b.ResetTimer()
for i := 0; i < b.N; i++ {
memclr(a)
}
}
func BenchmarkLoop_10(b *testing.B) {
var a = make([]MyInt, 10)
b.ResetTimer()
for i := 0; i < b.N; i++ {
memsetLoop(a, initialValue)
}
}
func BenchmarkMemclr_100(b *testing.B) {
var a = make([]MyInt, 100)
b.ResetTimer()
for i := 0; i < b.N; i++ {
memclr(a)
}
}
func BenchmarkLoop_100(b *testing.B) {
var a = make([]MyInt, 100)
b.ResetTimer()
for i := 0; i < b.N; i++ {
memsetLoop(a, initialValue)
}
}
func BenchmarkMemclr_1000(b *testing.B) {
var a = make([]MyInt, 1000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
memclr(a)
}
}
func BenchmarkLoop_1000(b *testing.B) {
var a = make([]MyInt, 1000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
memsetLoop(a, initialValue)
}
}
func BenchmarkMemclr_10000(b *testing.B) {
var a = make([]MyInt, 10000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
memclr(a)
}
}
func BenchmarkLoop_10000(b *testing.B) {
var a = make([]MyInt, 10000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
memsetLoop(a, initialValue)
}
}
func BenchmarkMemclr_100000(b *testing.B) {
var a = make([]MyInt, 100000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
memclr(a)
}
}
func BenchmarkLoop_100000(b *testing.B) {
var a = make([]MyInt, 100000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
memsetLoop(a, initialValue)
}
}
func BenchmarkMemclr_200000(b *testing.B) {
var a = make([]MyInt, 200000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
memclr(a)
}
}
func BenchmarkLoop_200000(b *testing.B) {
var a = make([]MyInt, 200000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
memsetLoop(a, initialValue)
}
}
func BenchmarkMemclr_300000(b *testing.B) {
var a = make([]MyInt, 300000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
memclr(a)
}
}
func BenchmarkLoop_300000(b *testing.B) {
var a = make([]MyInt, 300000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
memsetLoop(a, initialValue)
}
}
func BenchmarkMemclr_400000(b *testing.B) {
var a = make([]MyInt, 400000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
memclr(a)
}
}
func BenchmarkLoop_400000(b *testing.B) {
var a = make([]MyInt, 400000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
memsetLoop(a, initialValue)
}
}
func BenchmarkMemclr_500000(b *testing.B) {
var a = make([]MyInt, 500000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
memclr(a)
}
}
func BenchmarkLoop_500000(b *testing.B) {
var a = make([]MyInt, 500000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
memsetLoop(a, initialValue)
}
}
func BenchmarkMemclr_1000000(b *testing.B) {
var a = make([]MyInt, 1000000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
memclr(a)
}
}
func BenchmarkLoop_1000000(b *testing.B) {
var a = make([]MyInt, 1000000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
memsetLoop(a, initialValue)
}
}
func BenchmarkMemclr_2000000(b *testing.B) {
var a = make([]MyInt, 2000000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
memclr(a)
}
}
func BenchmarkLoop_2000000(b *testing.B) {
var a = make([]MyInt, 2000000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
memsetLoop(a, initialValue)
}
}
The memclr versions are really better,
but its advantage really becomes smaller for large size slices.
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.