Created
January 4, 2014 02:03
-
-
Save freeeve/8250514 to your computer and use it in GitHub Desktop.
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 main | |
import ( | |
"runtime" | |
"strconv" | |
"strings" | |
"sync" | |
"testing" | |
) | |
func MapMake(list []string, op func(string) string) []string { | |
output := make([]string, len(list)) | |
for i, v := range list { | |
output[i] = op(v) | |
} | |
return output | |
} | |
func Map(list []string, op func(string) string) []string { | |
output := make([]string, 0, len(list)) | |
for _, v := range list { | |
output = append(output, op(v)) | |
} | |
return output | |
} | |
func mapPart(list []string, out *[]string, op func(string) string, start int, end int, wg *sync.WaitGroup) { | |
for i := start; i < end; i++ { | |
(*out)[i] = op(list[i]) | |
} | |
wg.Done() | |
} | |
func MapPar(list []string, op func(string) string) []string { | |
runtime.GOMAXPROCS(2) | |
output := make([]string, len(list)) | |
var wg sync.WaitGroup | |
wg.Add(2) | |
half := len(list) / 2 | |
go mapPart(list, &output, op, 0, half, &wg) | |
go mapPart(list, &output, op, half, len(list), &wg) | |
wg.Wait() | |
return output | |
} | |
func makeSlice(n int) []string { | |
out := []string{} | |
for i := 0; i < n; i++ { | |
out = append(out, strconv.Itoa(i)) | |
} | |
return out | |
} | |
func BenchmarkSliceMake10(b *testing.B) { | |
benchmarkSliceMakeN(10, b) | |
} | |
func BenchmarkSliceMake100(b *testing.B) { | |
benchmarkSliceMakeN(100, b) | |
} | |
func BenchmarkSliceMake1000(b *testing.B) { | |
benchmarkSliceMakeN(1000, b) | |
} | |
func BenchmarkSliceMake10000(b *testing.B) { | |
benchmarkSliceMakeN(10000, b) | |
} | |
func benchmarkSliceMakeN(n int, b *testing.B) { | |
in := makeSlice(n) | |
for i := 0; i < b.N; i++ { | |
out := MapMake(in, strings.ToUpper) | |
if len(out) != n { | |
b.Fatal("wrong length") | |
} | |
} | |
} | |
func BenchmarkSliceAppend10(b *testing.B) { | |
benchmarkSliceAppendN(10, b) | |
} | |
func BenchmarkSliceAppend100(b *testing.B) { | |
benchmarkSliceAppendN(100, b) | |
} | |
func BenchmarkSliceAppend1000(b *testing.B) { | |
benchmarkSliceAppendN(1000, b) | |
} | |
func BenchmarkSliceAppend10000(b *testing.B) { | |
benchmarkSliceAppendN(10000, b) | |
} | |
func benchmarkSliceAppendN(n int, b *testing.B) { | |
in := makeSlice(n) | |
for i := 0; i < b.N; i++ { | |
out := Map(in, strings.ToUpper) | |
if len(out) != n { | |
b.Fatal("wrong length") | |
} | |
} | |
} | |
func BenchmarkSlicePar10(b *testing.B) { | |
benchmarkSliceParN(10, b) | |
} | |
func BenchmarkSlicePar100(b *testing.B) { | |
benchmarkSliceParN(100, b) | |
} | |
func BenchmarkSlicePar1000(b *testing.B) { | |
benchmarkSliceParN(1000, b) | |
} | |
func BenchmarkSlicePar10000(b *testing.B) { | |
benchmarkSliceParN(10000, b) | |
} | |
func benchmarkSliceParN(n int, b *testing.B) { | |
in := makeSlice(n) | |
for i := 0; i < b.N; i++ { | |
out := MapPar(in, strings.ToUpper) | |
if len(out) != n { | |
b.Fatal("wrong length") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment