Live data from Hacker News

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

jott.live

61–70 of 119 posts

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

#61

I've seen these types of comparisons, and I understand there a loads of factors playing a part in them. However, it does make me wonder, why has python become to standard for data science? Is it Library support or purely community based?

> why has python become to standard for data science?

Because it's glue, so its speed doesn't matter overly much.

> Is it Library support or purely community based?

That's a dichotomy which doesn't really make sense. Python has cultivated and attracted attention from scientific communities from the start: the matrix-sig (a special interest group focusing on array computing packages) was created back in '95 and a number of their suggestions were added as language-level conveniences (that continues to this day, `@` was recently added as the "matrix multiplication" operator).

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

#62
post #4

Earlier quoted context omitted.

Indeed. Furthermore this basically just benches function call overhead by using the worst possible implementation of fib(). Function call is a well-known weak point of cpython, even amongst all its other weak points performance-wise. It's hard to express how utterly uninteresting and useless TFA is, and if its author is surprised by the result… really the only component this tells us about is the author. > Would be m…

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 effort to create and then run something in production.

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

#63

I've seen these types of comparisons, and I understand there a loads of factors playing a part in them. However, it does make me wonder, why has python become to standard for data science? Is it Library support or purely community based?

Most Python data science offloads the data crunching to Numpy, an optimized array based processing library.

This is feasible to reduce interpreter overhead considerably by using array programming:

c = a + b #This is valid Python code.

Where a and b are two same sized numpy arrays. Numpy typically handles the add in an optimized SIMD function.

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

#64

Python feels noticeably slower than Node.js, particularly when running scripts with large amounts of imports. I now pretty much always use JavaScript for, well, scripting. Not sure why Python and Ruby are considered go-tos there.

Python gives you better escape hatches for typical compute-heavy work (lots of libraries use C under the hood), and Python has a better multi-threading story than Node.js. But I guess the biggest differentiators are really the ecosystems and standard libraries. Node.js is certainly catching up, but Python has generally a better selection of high-quality libraries for tasks beyond the web.

I believe libraries like v8pp are helping to shrink the gap with C library integration.

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

#65

Earlier quoted context omitted.

> An interpreter with a JIT is obviously faster than one without. And why doesn't python's default runtime environment obviously come with a JIT then? I think they should absolutely go for it, given the huge user base of python. Reasons like "but named functions can dynamically change" are not applicable, since JS has those same properties and can do it. They could start with optimizing the case where you call the sa…

> And why doesn't python's default runtime environment come with JIT? I think they should absolutely go for it, ensure the default python you get when you run python has a JIT, given the huge user base of python. 1. because CPython aims to be relatively simple and straightforward by choice 2. because the "huge user base" comes in large parts from the deep and extensive C API, which is absolute hell on a JIT 3. becaus…

> because CPython aims to be relatively simple and straightforward by choice

Sacrificing performance for core interpreter developer convenience may have been the right choice when Python was getting started; it's no longer the right choice today. Today it's short-sighted.

> because the "huge user base" comes in large parts from the deep and extensive C API, which is absolute hell on a JIT

We can have both a JIT and a "deep and extensive" (or more importantly, stable) native API, as demonstrated by node.

> because most of the userbase would not give a shit anyway

Actually a lot of the userbase don't use libraries with native extensions, are painfully aware of Python's performance issues, and are intensely interested in addressing them.

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

#67

I've seen these types of comparisons, and I understand there a loads of factors playing a part in them. However, it does make me wonder, why has python become to standard for data science? Is it Library support or purely community based?

It's because to a reasonably approximation of "None" - none of the actual data science runs in Python, it's all hyper customized libraries which do run (close to) metal fast once the data has been loaded into the appropriate data structures. Pandas is a shim on top of Numpy, which heavily leverages the Fortran77 BLAS/LAPACK libraries.

Python is used at the top of the stack because it's an easy language to learn, you can get started fast, and, for places where performance is important - nothing is running in Python anyways.

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

#68
1. Python doesn't have a JIT. This shouldn't be surprising or headline worthy.

2. Why benchmark an O(2^n) fibonacci? It's basically benchmarking call frame creation.

3. A single order of magnitude is honestly not that impressive a speedup for JIT vs. no JIT. Might have a lot to do with the inefficiency of the recursive fibonacci func.

Also, to quote another poster:

    @numba.njit
    def fib(n):
      if n == 1 or n == 0:
        return 1
      return fib(n - 1) + fib(n - 2)
After JIT warms up:

~86 ms

Which is...effectively identical to Node?

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

#70

Doesn't the Node.js version use double precision floating point vs python using infinite precision integers ? That would explain part of the difference in performance, and make the python version exact, but js version inexact.

I assumed since this seems to be testing function call overhead, rather than the math, that an equivalent function with BigIntegers would be about the same for JS. But I tried it just for fun:

    const { performance } = require('perf_hooks');

    function fib(n) {
       if (n == 0 || n == 1) { return 1; }
       return fib(n - 1) + fib(n - 2);
    }

    function fibn(n) {
       if (n == 0n || n == 1n) { return 1n; }
       return fibn(n - 1n) + fibn(n - 2n);
    }

    var t0 = performance.now(); fib(35); console.log("fib:", performance.now() - t0);
    var t0 = performance.now(); fibn(35n); console.log("fibn:", performance.now() - t0);
results:

    fib: 127.30008998513222
    fibn: 2134.1405459940434

yikes, I think you're right. (nodejs v15.6.0)

(An equivalent version with Python on my machine: ~2657.3ms)

Post reply on HN