Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Yeah, Thread/sleep isn't quite what you want here:

(time (let [c (chan), n 1000] (dotimes [i n] (go (Thread/sleep 50) (>! c i))) (dotimes [i n] (<!! c))))

"Elapsed time: 8412.25733 msecs"

(defmacro gosleep [millis] `(<! (timeout ~millis)))

(time (let [c (chan), n 1000] (dotimes [i n] (go (gosleep 50) (>! c i))) (dotimes [i n] (<!! c))))

"Elapsed time: 91.278469 msecs"

ETA: for comparison, here's what happens if you actually make 1000 system threads:

(time (let [c (chan) n 1000] (dotimes [i n] (thread (Thread/sleep 50) (>!! c i))) (dotimes [i n] (<!! c))))

"Elapsed time: 4183.669835 msecs"



Thanks (and thanks to pron). I've updated the post to include a warning against using Thread/sleep in go blocks for "real" code.


I feel silly though that this is the one thing I've commented on, so:

This is an awesome post, thanks for it ^_^




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: