Skip to content

Instantly share code, notes, and snippets.

View campoy's full-sized avatar

Francesc Campoy campoy

View GitHub Profile
@campoy
campoy / main.go
Created February 23, 2018 00:49
a more idiomatic solution
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"sync"
@campoy
campoy / main.go
Created February 12, 2018 17:39
gist 10 for episode 28 of justforfunc
for i := 0; i < b.N; i++ {
b.StopTimer()
chans := make([]<-chan int, n)
for j := range chans {
chans[j] = asChan(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
}
b.StartTimer()
c := merge.fun(chans...)
for range c {
}
@campoy
campoy / main.py
Created February 12, 2018 17:37
gist 9 for episode 28 of justforfunc
import matplotlib.pyplot as plt
ns = [2**x for x in range(11)];
data = {
'goroutines': [6919, 13212, 25469, 50819, 88566, 162391, 299955, 574043, 1129372, 2251411, 4760560],
'reflection': [10868, 22335, 54882, 148218, 543921, 1694021, 6102920, 22648976, 90204929, 383579039, 1676544681],
'recursion': [2658, 14707, 44520, 114676, 261880, 560284, 1117642, 2242910, 4784719, 10044186, 20599475],
}
for (label, values) in data.items():
plt.plot(ns, values, label=label)
@campoy
campoy / main.go
Created February 12, 2018 17:36
gist 8 for episode 28 of justforfunc
func BenchmarkMerge(b *testing.B) {
merges := []struct {
name string
fun func(...<-chan int) <-chan int
}{
{"goroutines", merge},
{"reflect", mergeReflect},
{"recursion", mergeRec},
}
for _, merge := range merges {
@campoy
campoy / main.go
Created February 12, 2018 17:35
gist 7 for episode 28 of justforfunc
func BenchmarkMerge(b *testing.B) {
merges := []struct {
name string
fun func(...<-chan int) <-chan int
}{
{"goroutines", merge},
{"reflect", mergeReflect},
{"recursion", mergeRec},
}
for _, merge := range merges {
@campoy
campoy / main.go
Created February 12, 2018 17:35
gist 6 for episode 28 of justforfunc
func BenchmarkMerge(b *testing.B) {
merges := []func(...<-chan int) <-chan int{
merge,
mergeReflect,
mergeRec,
}
for _, merge := range merges {
for i := 0; i < b.N; i++ {
c := merge(asChan(0, 1, 2, 3, 4, 5, 6, 7, 8, 9))
for range c {
@campoy
campoy / main.go
Created February 12, 2018 17:33
gist 5 for episode 28 of justforfunc
func BenchmarkMergeRec(b *testing.B) {
for i := 0; i < b.N; i++ {
c := mergeRec(asChan(0, 1, 2, 3, 4, 5, 6, 7, 8, 9))
for range c {
}
}
}
@campoy
campoy / main.go
Created February 12, 2018 17:33
gist 4 for episode 28 of justforfunc
func BenchmarkMergeReflect(b *testing.B) {
for i := 0; i < b.N; i++ {
c := mergeReflect(asChan(0, 1, 2, 3, 4, 5, 6, 7, 8, 9))
for range c {
}
}
}
@campoy
campoy / main.go
Created February 12, 2018 17:33
gist 4 for episode 28 of justforfunc
func BenchmarkMergeReflect(b *testing.B) {
for i := 0; i < b.N; i++ {
c := mergeReflect(asChan(0, 1, 2, 3, 4, 5, 6, 7, 8, 9))
for range c {
}
}
}
@campoy
campoy / main.go
Created February 12, 2018 17:32
gist 3 for episode 28 of justforfunc
func BenchmarkMerge(b *testing.B) {
for i := 0; i < b.N; i++ {
c := merge(asChan(0, 1, 2, 3, 4, 5, 6, 7, 8, 9))
for range c {
}
}
}