niklas-heer / speed-comparison

A repo which compares the speed of different programming languages.
https://niklas-heer.github.io/speed-comparison
MIT License
475 stars 76 forks source link

Clojure solution like in Java #55

Closed serioga closed 1 year ago

serioga commented 1 year ago

To compare Clojure solution with Java more correctly you can use this translation of Java code:

(defn calc-pi-leibniz [rounds]
  (let [end (+ 2 rounds)]
    (loop [i 2 x 1.0 pi 1.0]
      (if (= i end)
        (* 4 pi)
        (let [x (- x)]
          (recur (inc i) x (+ pi (/ x (dec (* 2 i))))))))))

It should be about 6-7x faster.

niklas-heer commented 1 year ago

@serioga I appreciate the improvement. :+1: I'm very new to Clojure :sweat_smile:

Do you want to create a PR, or should I add it for you? (since the Hacktoberfest is going on)

serioga commented 1 year ago

I've created a pull request https://github.com/niklas-heer/speed-comparison/pull/56