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 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 {
}
}
}
@campoy
campoy / main.go
Created February 12, 2018 17:32
gist 2 for episode 28 of justforfunc
func BenchmarkFoo(b *testing.B) {
for i := 0; i < b.N; i++ {
// perform the operation we're analyzing
}
}
@campoy
campoy / main.go
Created February 12, 2018 17:31
gist 1 for episode 28 of justforfunc
func mergeRec(chans ...<-chan int) <-chan int {
switch len(chans) {
case 0:
c := make(chan int)
close(c)
return c
case 1:
return chans[0]
default:
m := len(chans) / 2
@campoy
campoy / paper.txt
Last active February 5, 2018 23:18
Article
\documentclass[conference,10pt]{IEEEtran}
\usepackage[utf8]{inputenc}
\ifCLASSINFOpdf
\usepackage[pdftex]{graphicx}
\else
\usepackage[dvips]{graphicx}
\fi
\usepackage{algpseudocode}
\usepackage{amsmath}
\usepackage{color}
func merge(cs ...<-chan int) <-chan int {
out := make(chan int)
var wg sync.WaitGroup
wg.Add(len(cs))
for _, c := range cs {
go func(c <-chan int) {
for v := range c {
out <- v
}
wg.Done()
i, v, ok := reflect.Select(cases)
if !ok {
cases = append(cases[:i], cases[i+1:]...)
continue
}
out <- v.Interface().(int)
i, v, ok := reflect.Select(cases)
if !ok {
cases = append(cases[:i], cases[i+1:]...)
continue
}
out <- v.Interface().(int)
var cases []reflect.SelectCase
for _, c := range chans {
cases = append(cases, reflect.SelectCase{
Dir: reflect.SelectRecv,
Chan: reflect.ValueOf(c),
})
}
func merge(cs ...<-chan int) <-chan int {
out := make(chan int)
var wg sync.WaitGroup
wg.Add(len(cs))
for _, c := range cs {
go func(c <-chan int) {
for v := range c {
out <- v
}
wg.Done()