News for hackers. Stuff that matters.
Re:Comparing clojure and node.js for speed by theturtle32 on Thursday April 07, 2011 @06:38PM (#2421507)
This is totally not the right benchmark for comparison. Node's JS execution is single threaded, and it's well known that it's not the appropriate choice of platform for CPU-bound workloads. Its main selling points are high concurrency in terms of number of network connections, event based design, and JavaScript as a programming environment. While it may be fairly good at doing computation because of V8's strong optimizations (it compiles JS functions to x86 assembly - JVM does similar things) it is not good at also sustaining high concurrency in the process. Those CPU bound operations will block everything else until they're done unless you code your functions to yield and cooperatively multitask.
Re:Comparing clojure and node.js for speed by jshen on Thursday April 07, 2011 @06:08PM (#2421409)
This is a poorly implemented benchmark. He's including the startup time of the jvm, he's not warming up the jvm, and he's not type hinting the clojure code.

I don't know node, so I don't know if he's making similar mistakes on that side.

Re:Comparing clojure and node.js for speed by Swizec on Thursday April 07, 2011 @06:14PM (#2421437)
I tried with type hinting the clojure code, it was slower.

Startup time for the jvm seemed fair, because node has to start up as well. This should count into the overall time imho.

Re:Comparing clojure and node.js for speed by jshen on Thursday April 07, 2011 @06:18PM (#2421446)
you should calculate the time within clojure and node and output it. This will take away the startup time. You should also warm up the jvm.

I may play with the type hints tonight if I get time. It shouldn't make it slower.

Re:Comparing clojure and node.js for speed by gfodor on Thursday April 07, 2011 @06:16PM (#2421441)
Uh, if it's slower you should probably look into that.
Re:Comparing clojure and node.js for speed by oskarkv on Thursday April 07, 2011 @06:58PM (#2421575)
How did you add the type hints?
Re:Comparing clojure and node.js for speed by realgt on Thursday April 07, 2011 @06:15PM (#2421440)
node has no jvm, node starts up in milliseconds. node _is_ faster
Re:Comparing clojure and node.js for speed by runevault on Friday April 08, 2011 @12:00AM (#2422381)
For most anything server based (which is how I would use Node if I were to pick it up) startup time should not matter because the server just remains running... making that comparison pointless at best and disingenuous at worst.
Re:Comparing clojure and node.js for speed by UncleOxidant on Thursday April 07, 2011 @06:44PM (#2421530)
...to start up anyway.
Re:Comparing clojure and node.js for speed by jshen on Thursday April 07, 2011 @07:26PM (#2421669)
which is unimportant to a large class of apps.
Re:Comparing clojure and node.js for speed by UncleOxidant on Thursday April 07, 2011 @06:39PM (#2421515)
As soon as he said: "The comparison I chose to go for is hard computation" I knew that node would be shown to be the loser. node isn't going to be great for lots of long computations - even node fans will tell you that.

We are finding node to be quite nice, though, for our real-time web app (which includes chat communication, and game like features). But we don't have any long-running calculations to do. I suspect that if we did we'd have them run in a separate server process... maybe even writing the compute intensive code in C++.

tl;dr: the type of benchmark chosen is stacked against node.

Re:Comparing clojure and node.js for speed by tomazmuraus on Thursday April 07, 2011 @06:39PM (#2421512)
Nothing new here.

Node wasn't ever really meant to be used for CPU intensive tasks - especially the ones like this which you can fairly easy parallelize.

Re:Comparing clojure and node.js for speed by UncleOxidant on Thursday April 07, 2011 @06:42PM (#2421523)
Exactly, If I were using node and needed to calculate primes I'd probably write some C++ code to handle that part and run it as a server in a separate process that can be called asynchronously from node.
Re:Comparing clojure and node.js for speed by calpaterson on Thursday April 07, 2011 @06:54PM (#2421561)
Hopefully you'd try another algorithm before you did that ;). Primality tests are one of the most highly understood and well documented areas of CS - trial division has been obsolete since, what, 1640?
Re:Comparing clojure and node.js for speed by btilly on Thursday April 07, 2011 @07:43PM (#2421717)
... trial division has been obsolete since, what, 1640?

Longer than that. The sieve of Eratosthenes is attributed to Eratosthenes, who lived from 276 BC to 195 BC.

Re:Comparing clojure and node.js for speed by calpaterson on Friday April 08, 2011 @07:31AM (#2423297)
Hah, this exact thing occurred to me lying in bed last night :)
Re:Comparing clojure and node.js for speed by ibdknox on Thursday April 07, 2011 @06:39PM (#2421513)
I commented with a much better version of your clojure code. I'm still not convinced that this is the best way to calculate primes though. Moreover, I could imagine coming up with a better implementation that uses a lazy seq to just calculate the next prime when it is needed. Here's the code again:

    (defn prime? [n]
     (if (even? n) 
      false
      (let [root-plus-one (inc (int (Math/sqrt n)))]
       (every? #(> (mod n %) 0) (range 3 root-plus-one 2)))))
    
    (defn primes [n]
     (conj (filter prime? (range 3 n)) 2))
As far as I can tell this is also faster on my machine, but as others suggest, I don't know that this is a particularly valuable benchmark.
Re:Comparing clojure and node.js for speed by calpaterson on Thursday April 07, 2011 @06:50PM (#2421545)
Trial division (your algorithm) _is_ linear time, there exist better than linear time algorithms for this, and they're pretty easy to understand (look for Solovay-Strassen on wikipedia for a simple one).

Furthermore, this isn't a great test. For one, you're specifically testing a performance profile that the JVM does not bother to optimise for (programs that run for less than about 20 seconds). For two, number speeds aren't a great measure - the issues with autoboxing (that I think Clojure does - I'm not a user) and whether the implementation is simply calling libgmp (maybe V8 is? Seems sensible to expect that it would - but I am also not a user of javascript) kind of predecide the outcome.

Re:Comparing clojure and node.js for speed by wvl on Thursday April 07, 2011 @06:51PM (#2421552)
In addition to the other comments, this is not how you would implement the code in node. Since node is single threaded, and the isPrime function is not asynchronous in any way, there is no point to writing it as if it were. Simplifying it shows it running in 2/3 the time.

https://gist.github.com/908957