Skip to content

Instantly share code, notes, and snippets.

@swannodette
Last active December 22, 2015 22:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swannodette/6542719 to your computer and use it in GitHub Desktop.
Save swannodette/6542719 to your computer and use it in GitHub Desktop.
Comparing daisy chain benchmark for ClojureScript core.async and go-lang
(ns daisy
(:require [cljs.core.async :refer [chan <! >!]])
(:require-macros [cljs.core.async.macros :refer [go]]))
(defn f [left right]
(go (>! left (inc (<! right)))))
(let [leftmost (chan)
rightmost (loop [n 100000 left leftmost]
(if-not (pos? n)
left
(let [right (chan)]
(f left right)
(recur (dec n) right))))]
(go (time (do
(>! rightmost 1)
(.log js/console (<! leftmost))))))
;; 100001
;; "Elapsed time: 139 msecs"
;; real 0m1.214s
;; user 0m1.074s
;; sys 0m0.162s
package main
import (
"fmt"
"time"
)
func f(left, right chan int) {
left <- 1 + <-right
}
func main() {
const n = 100000
leftmost := make(chan int)
right := leftmost
left := leftmost
for i := 0; i < n; i++ {
right = make(chan int)
go f(left, right)
left = right
}
start := time.Now()
go func(c chan int) { c <- 1 }(right)
fmt.Println(<-leftmost)
fmt.Println(time.Now().Sub(start))
}
// 100001
// 65.424608ms
// real 0m0.989s
// user 0m0.326s
// sys 0m0.661s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment