Core.async - Daisy chain benchmark

core.async

Comparing daisy chain benchmark for ClojureScript core.async and go-lang

12 Sept 2013 by swannodette
swannodette/6542719
daisy.cljs
(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
daisy.go
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