Skip to content

Instantly share code, notes, and snippets.

@deathbob
Last active December 19, 2015 12:59
Show Gist options
  • Save deathbob/5958480 to your computer and use it in GitHub Desktop.
Save deathbob/5958480 to your computer and use it in GitHub Desktop.
go vs clojure go routines
(defn foo[]
(let [c (chan)
n 100]
(doseq [i (range n)]
(go
(Thread/sleep 1000)
(>! c i)))
(doseq [i (range n)]
(println (<!! c)))))
(time (foo))
;; "Elapsed time: 10026.963 msecs"
package main
import (
"fmt"
"time"
)
func main() {
c := make(chan int)
n := 100
for i := 0; i < n; i++ {
go func(i int) {
time.Sleep(time.Duration(1) * time.Second)
c <- i
}(i)
}
for i := 0; i < n; i++ {
fmt.Println(<-c)
}
}
// time ./hello
// 0.00s user 0.00s system 0% cpu 1.011 total
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment