Live data from Hacker News

PyPy.js: Python in the web browser

pypyjs.org

111–120 of 129 posts

Re: PyPy.js: Python in the web browser

#111
post #76
post #75

File " ", line 2 return f'{fname} {surname} is {age}.' ^ SyntaxError: invalid syntax So this is a subset of Python or something like that?

I believe this feature was introduced in Python 3.6. This implementation may be 3.6 Edit: Just checked the site and see it uses PyPy which states -- on their site -- the python version is 3.5.3

Or you know, just import sys and print sys.version instead of reverse-engineering it from the syntactic features introduced over time.

Re: PyPy.js: Python in the web browser

#112
post #52

The speed is surprisingly reasonable. Testing with a simple loop, I get about 1.3-1.4 million loops per second in python3, 2 million loops per second in python2, 14.5 million loops in pypy, and 0.61 million loops in the browser. My code basically calls int(time.time()) until its value changes, doing i+=1 for every time it did not change (n=10 for every platform (python2/3/pypy/browser)). My browser is Firefox 61, ful…

There's some pypy.js vs cpython benchmarks at http://arewepythonyet.com/ - those look very good as well.

The print will slow everything down as well.

Re: PyPy.js: Python in the web browser

#113
post #98

Earlier quoted context omitted.

I wonder if the time.time is the slow part there? Maybe worth doing a busy look for say 5 million iterations, timing that then doing the calculation for loops per second.

I'm sure it is a thousand times slower than i++, but it should be the same code on each platform. But you made me think: indeed, the underlying call could be slower from JS whereas from pypy/python{2,3} it's fast (or vice versa). So I just tested a version where it checks how long ten million iterations take (n=3). Code here: https://hastebin.com/uyuhecabek.py browser/pypy 5.25 seconds python 3.6.6 2.16 seconds pytho…

Tested on Brython (with Firefox) - http://www.brython.info/tests/editor.html?lang=en

    Iteration 0 took -0.9660000801086426s
    Iteration 1 took -0.9519999027252197s
    Iteration 2 took -0.9499998092651367s
It is about 10 times slower with Chrome on my computer.

Re: PyPy.js: Python in the web browser

#114
post #98

Earlier quoted context omitted.

I'm sure it is a thousand times slower than i++, but it should be the same code on each platform. But you made me think: indeed, the underlying call could be slower from JS whereas from pypy/python{2,3} it's fast (or vice versa). So I just tested a version where it checks how long ten million iterations take (n=3). Code here: https://hastebin.com/uyuhecabek.py browser/pypy 5.25 seconds python 3.6.6 2.16 seconds pytho…

Tested on Brython (with Firefox) - http://www.brython.info/tests/editor.html?lang=en Iteration 0 took -0.9660000801086426s Iteration 1 took -0.9519999027252197s Iteration 2 took -0.9499998092651367s It is about 10 times slower with Chrome on my computer.

Wow. That makes a huge difference.

    Iteration 0 took 0.6579999923706055s
    Iteration 1 took 0.6540000438690186s
    Iteration 2 took 0.6460001468658447s
(I noticed the code from hastebin was an old version, I forgot to swap the starttime-time.time() in the version I posted. Not that it matters much, just change the sign.)

Running it on OP's website again just to be sure, it now takes 4.9 seconds instead of 5.25, so there is some variance there, but this is still a big difference. This is Firefox as well (see another comment in this subthread), I don't have Chromium installed.

Re: PyPy.js: Python in the web browser

#115
post #52

The speed is surprisingly reasonable. Testing with a simple loop, I get about 1.3-1.4 million loops per second in python3, 2 million loops per second in python2, 14.5 million loops in pypy, and 0.61 million loops in the browser. My code basically calls int(time.time()) until its value changes, doing i+=1 for every time it did not change (n=10 for every platform (python2/3/pypy/browser)). My browser is Firefox 61, ful…

Fetching time in the browser isn't accurate because a lot of trackers exploit timing in order to fingerprint users. A lot of browsers now purposefully reduce the precision of the clock. See:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

https://johnresig.com/blog/accuracy-of-javascript-time/

Re: PyPy.js: Python in the web browser

#117
post #52

The speed is surprisingly reasonable. Testing with a simple loop, I get about 1.3-1.4 million loops per second in python3, 2 million loops per second in python2, 14.5 million loops in pypy, and 0.61 million loops in the browser. My code basically calls int(time.time()) until its value changes, doing i+=1 for every time it did not change (n=10 for every platform (python2/3/pypy/browser)). My browser is Firefox 61, ful…

Fetching time in the browser isn't accurate because a lot of trackers exploit timing in order to fingerprint users. A lot of browsers now purposefully reduce the precision of the clock. See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... https://johnresig.com/blog/accuracy-of-javascript-time/

Oh, how convenient. Sometimes I wonder if we shouldn't find a way to be able to run trusted applications instead of making everyone's life difficult all the time, and that's speaking as a security consultant, not even a developer... but yeah I don't see a good way to make that happen.

Re: PyPy.js: Python in the web browser

#118
post #99

It's really awesome to see how far this has come. I believe we at Repl.it were the first to try something like this in production. We emscriptined CPython to JavaScript and contributed quite a bit to the project in the process (including the initial virtual filesystem implementation). https://github.com/replit-archive/empythoned I deployed this at scale at Codecademy were millions of users were using it to learn Pyth…

I just wanted to say how much I love Repl.it and use it all the time. I use it as a testing scratchpad mostly but I know it's much powerful than that. I appreciate its simplicity. Thanks for a great product!

Thanks so much :)

Re: PyPy.js: Python in the web browser

#119
post #98

Earlier quoted context omitted.

I wonder if the time.time is the slow part there? Maybe worth doing a busy look for say 5 million iterations, timing that then doing the calculation for loops per second.

I'm sure it is a thousand times slower than i++, but it should be the same code on each platform. But you made me think: indeed, the underlying call could be slower from JS whereas from pypy/python{2,3} it's fast (or vice versa). So I just tested a version where it checks how long ten million iterations take (n=3). Code here: https://hastebin.com/uyuhecabek.py browser/pypy 5.25 seconds python 3.6.6 2.16 seconds pytho…

Are you sure pypy didn't just optimize out the loop entirely (as an aggressive C compiler would have done)?

Re: PyPy.js: Python in the web browser

#120
post #106

Earlier quoted context omitted.

You would use sum(range()) in a Python though. Remember that many things you do manually in JS have a better, automatic and faster way in the stlib. Besides, if calculations are important, you would use numpy.

Ah yes. sum(range()) in Python vs for(i=1...) in js was much closer, about 20% quicker in javascript.

Won't this calculation overflow to +Infinity in JavaScript after a while? On the other hand, Python will try to compute the actual result using arbitrary precision integers, which is that's slower.
Post reply on HN