Skip to content

Instantly share code, notes, and snippets.

@annashipman
Last active September 20, 2021 21:22
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 annashipman/d3b0533ce26df1e4dd84fbc3001e98dc to your computer and use it in GitHub Desktop.
Save annashipman/d3b0533ce26df1e4dd84fbc3001e98dc to your computer and use it in GitHub Desktop.
Debugging Clojure/Vagrant for 7 concurrency models in 7 weeks
I am trying to replicate an example from this book: https://uk.bookshop.org/books/seven-concurrency-models-in-seven-weeks-when-threads-unravel/9781937785659
Running the below commands in a lein repl.
In the book, the second `parallel-sum` function executes 2.5x as fast as `sum` but on my Vagrant box (which has 4 cores, and demonstrates other parallelised code having a speed up effect) the two functions take pretty much the same time every time. My Vagrantfile is here https://github.com/annashipman/7weeks-concurrency/blob/main/Vagrantfile
This works in a non-Vagrant Clojure environment, i.e parallel-sum runs much faster.
So, what should I do differently in Vagrant to get it to work?
(ns sum.core
(:require [clojure.core.reducers :as r]))
(defn sum [numbers]
(reduce + numbers))
(defn parallel-sum [numbers]
(r/fold + numbers))
(def numbers (into [] (range 0 10000000)))
(time (sum numbers))
(time (sum numbers))
(time (parallel-sum numbers))
(time (parallel-sum numbers))
@gildegoma
Copy link

⚠️ The above examples are only ran 1 time each, I don't pretend to present any concurrency benchmarking, but demonstrate that GC impact is tremendous and can prevent to compare parallelism gains.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment