Live data from Hacker News

Node.js 14 is over 20x faster than Python3.8 for fib(n)

jott.live

81–90 of 119 posts

Re: Node.js 14 is over 20x faster than Python3.8 for fib(n)

#81

This is a pretty terrible implementation of fib(n).

It's a common approach to measuring function spawn cost.

I think GP meant how the fib function was written (and not why it was chosen for the measurement). The `if n == 1 or n == 0` thing hurts my eyes too.

Re: Node.js 14 is over 20x faster than Python3.8 for fib(n)

#82

FYI: pypy3 is 10x faster than CPython on this benchmark. ~> python fib.py 4825.7598876953125 ms ~> pypy3 fib.py 514.7459506988525 ms

Numba version: @numba.jit def fib(n): if n == 1 or n == 0: return 1 return fib(n - 1) + fib(n - 2) first run: 498.46601486206055 ms. second run: 89.19310569763184 ms. For completeness, with njit: @numba.njit def fib(n): if n == 1 or n == 0: return 1 return fib(n - 1) + fib(n - 2) first run: 152.62889862060547 ms second run: 86.35592460632324 ms

Weird. On Mac I get 282 ms from pypy3, but 233 ms from numba.

    from numba import jit
    @jit
    def fib(n: int) -> int:

Re: Node.js 14 is over 20x faster than Python3.8 for fib(n)

#83
post #57

Ignoring the bad implementation of fib(n) and use of a Python interpreter without JIT... why does this matter? Python and JavaScript are scripting languages. Their advantages are being highly portable and relatively easy to develop and maintain. Performance has always been a weakness of both languages when compared to their pre-compiled siblings. That limitation is often mitigated by "gluing" together functionality i…

I think it matters because a lot of people are doing math in python.

And if good optimizations can help run a small simulation in 1 minute instead of 2, multiplied by the number of python users, it is a lot of time gained.

And I suspect, the fib(n) situation is actually quite common. I mean, not everyone who writes python is a "real" developer, there are a lot of scientists who just want the computer to run their formulas, and they write them it the most straightforward way possible. They won't bother with high performance libraries and optimizing their algorithms just to save a few minutes, but they would appreciate if the language could make things a little faster.

And it doesn't matter in the way of "hey look, Python slow, use JS". But it is good information for Python developers and advanced users, the people who non-specialists rely on.

Re: Node.js 14 is over 20x faster than Python3.8 for fib(n)

#84
post #72
post #57

Ignoring the bad implementation of fib(n) and use of a Python interpreter without JIT... why does this matter? Python and JavaScript are scripting languages. Their advantages are being highly portable and relatively easy to develop and maintain. Performance has always been a weakness of both languages when compared to their pre-compiled siblings. That limitation is often mitigated by "gluing" together functionality i…

I think it's notable that the gap between dominant scripting languages for simple function-call bound code has become quite large.

I do not consider the difference in performance for that particular implementation of that particular function on those particular interpreters to be notable. I don't think it's a fair match-up and I don't think there's much value in comparing the effectiveness of tools for a job they aren't made for.

For additional comparison, I quickly I rewrote the function in C (output and code below). The code was comparable in length and complexity (at least the fib(n) implementation was), but the relative performance is enough to make JavaScript blush. If you really wanted to do something as trivial as this, why would you even use pure JavaScript or Python to begin with? And if performance was a concern, why would you choose a reference interpreter like CPython?

Output:

    $ ./fib 35
    14930352
    68 ms
Code:

    #include 
    #include 
    #include 
    
    int fib(int n) {
        if (n == 1 || n == 0) return 1;
        return fib(n - 1) + fib(n - 2);
    }
    
    int main(int argc, char *argv[]) {
        if (argc 

Re: Node.js 14 is over 20x faster than Python3.8 for fib(n)

#85
post #83
post #57

Ignoring the bad implementation of fib(n) and use of a Python interpreter without JIT... why does this matter? Python and JavaScript are scripting languages. Their advantages are being highly portable and relatively easy to develop and maintain. Performance has always been a weakness of both languages when compared to their pre-compiled siblings. That limitation is often mitigated by "gluing" together functionality i…

I think it matters because a lot of people are doing math in python. And if good optimizations can help run a small simulation in 1 minute instead of 2, multiplied by the number of python users, it is a lot of time gained. And I suspect, the fib(n) situation is actually quite common. I mean, not everyone who writes python is a "real" developer, there are a lot of scientists who just want the computer to run their for…

Yeah, but it's like comparing the effectiveness of a shovel vs a spade when it comes to hammering nails. You should obviously use a hammer. If it's not worthwhile to get a hammer, then does it really matter whether you use the shovel or the spade?

Re: Node.js 14 is over 20x faster than Python3.8 for fib(n)

#86

Earlier quoted context omitted.

Numba version: @numba.jit def fib(n): if n == 1 or n == 0: return 1 return fib(n - 1) + fib(n - 2) first run: 498.46601486206055 ms. second run: 89.19310569763184 ms. For completeness, with njit: @numba.njit def fib(n): if n == 1 or n == 0: return 1 return fib(n - 1) + fib(n - 2) first run: 152.62889862060547 ms second run: 86.35592460632324 ms

Weird. On Mac I get 282 ms from pypy3, but 233 ms from numba. from numba import jit @jit def fib(n: int) -> int:

Even on subsequent runs?

Re: Node.js 14 is over 20x faster than Python3.8 for fib(n)

#87

Earlier quoted context omitted.

Seems like an unduly harsh take. Is everyone just supposed to inherently know this fact about cpython or find it unsurprising when they run across it? "Obviously this isn't the most comprehensive benchmark, but the results are surprising to me." I fully agree with this and I learned something new about cpython's weakpoints today!

Oh, I think it is a quite reasonable comment. It isn't interesting work being done in the benchmark and the benchmark itself is short. If you want to get ultimate performance from Python then write a C function... If you want to inform me about runtime performance then show me how the language runtimes are spending cycles. If you wish to convince me about a language being great then tell me about the engineering effo…

It's a 2 sentence post, the latter of which seems to be trying to dispel the idea that it's about what you're implying it is. Uninteresting to experts in the area that consider this common knowledge maybe, not a failure of the author to do something worthwhile though.

Re: Node.js 14 is over 20x faster than Python3.8 for fib(n)

#88

Earlier quoted context omitted.

Weird. On Mac I get 282 ms from pypy3, but 233 ms from numba. from numba import jit @jit def fib(n: int) -> int:

Even on subsequent runs?

Yeah, that's the weird part.

Re: Node.js 14 is over 20x faster than Python3.8 for fib(n)

#89

Earlier quoted context omitted.

I asked, run it twice. The first time is for compilation. The second time it reaches 90ms on my machine.

I would expect numba to win, but I also do not think it is a fair comparison.

To be fair, python itself isn't jitting anything, while node is.

Re: Node.js 14 is over 20x faster than Python3.8 for fib(n)

#90
post #78

Compare languages on performance is weird. Every language is made with another philosophy. Python isnt made for performance, but for good code readability. Write clear, logical code for small and large-scale projects. It is also made by Guido to build applications in less time. Guido knows his language isnt the fastest, but thas wasnt the goal with Python. Node.js is also made for other applications than Python. It's…

Syntactic differences aside, the capabilities of JavaScript and Python do not differ significantly. They are both highly popular highly dynamic object-oriented-ish languages.

Knowing that the most prevalent implementation of one is significantly faster than the most prevalent implementation of the other is useful information for someone deciding between the two for a project where performance is important.

Comparing alternative implementations of the languages, which are geared toward performance, would also be valuable. But simply knowing that, if your choice to use CPython is more-or-less arbitrary, you're leaving performance on the table, is valuable.

(Of course, as discussed in the top-level thread by @SuchAnonMuchWow, this particular benchmark seems to be comparing apples to oranges, and thus may not be of much value.)

Post reply on HN