Last active
December 19, 2015 12:59
-
-
Save deathbob/5958480 to your computer and use it in GitHub Desktop.
go vs clojure go routines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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