News for hackers. Stuff that matters.
Re:Python 3.11 is faster than 3.8 by Shish2k on Wednesday October 26, 2022 @01:52PM (#33347084)
Checking for my own “benchmark”, a gameboy emulator in several different languages[0]; it’s CPU-bound but across ~3k lines of code, so slightly more representative of real-world apps than a single-function tight-loop microbenchmark:

     zig: Emulated 600 frames in  0.24s (2521fps)
      rs: Emulated 600 frames in  0.37s (1626fps)
     cpp: Emulated 600 frames in  0.40s (1508fps)
     nim: Emulated 600 frames in   1.6s (367fps)
      go: Emulated 600 frames in  1.75s (342fps)
     php: Emulated 600 frames in 23.74s (25fps)
      py: Emulated 600 frames in 33.10s (18fps)   # 3.11
      py: Emulated 600 frames in 61.43s (9fps)    # 3.10
Doubling the speed is pretty nice :D Still the slowest out of all implementations though :P

[0] https://github.com/shish/rosettaboy

Re:Python 3.11 is faster than 3.8 by winter_blue on Wednesday October 26, 2022 @02:16PM (#33347388)
It's amazing that Zig is faster than both Rust and C++. Kudos to the Zig team & Andrew Kelly!

I wonder what optimizations Zig does that lets it generate machine code / LLVM bitcode faster than both C++ and Rust (which certainly have larger teams backing them), at least in the case of this Gameboy emulator project.

Re:Python 3.11 is faster than 3.8 by Shish2k on Wednesday October 26, 2022 @02:23PM (#33347466)
Yeah, I’m really not sure how it managed that. I am slightly suspicious, because while I was working on the zig version I spent more time running into compiler bugs[1] than writing my code, and so there’s a chance that it’s running so fast by throwing away random chunks of important behaviour… but it still passes all of the test suite, so as far as I can tell this specific build is working correctly.

[1] half the time I’d make the compiler crash; the other half it would generate a binary which crashes at runtime, with weird heisenbug behaviour like “adding a print statement to log how far down a function I am causes the code to stop crashing at all” — like right now there is a load-bearing print statement which shows the address of the SDL Window object, because otherwise the compiler seems to optimise the Window out of existence and then a few lines later it segfaults on the null pointer…

Re:Python 3.11 is faster than 3.8 by sirsinsalot on Wednesday October 26, 2022 @04:21PM (#33348858)
> a load-bearing print statement

Is the most glorious thing I ever heard.

Re:Python 3.11 is faster than 3.8 by kristoff_it on Thursday October 27, 2022 @03:56AM (#33354540)
> like right now there is a load-bearing print statement which shows the address of the SDL Window object, because otherwise the compiler seems to optimise the Window out of existence

that sounds like you have undefined behavior in your program

Re:Python 3.11 is faster than 3.8 by cma on Wednesday October 26, 2022 @02:21PM (#33347444)
Does zig have computed goto? That's important for emulators and virtual machines and C/C++ don't have it. Projects will often go out of their way to have a MinGW/gcc built module for core loops with that (gcc has it as an extension) even if the main project is built with MSVC.
Re:Python 3.11 is faster than 3.8 by nerpderp82 on Wednesday October 26, 2022 @02:26PM (#33347493)
If Rust wasn't able to elide bounds checks, that is most likely where the perf difference is from. Analyzing the output the assembly output from goldbolt or looking at the MIR can help.

https://stefan-marr.de/2022/10/cost-of-safety-in-java/

Unsafety buys you a little more performance, but the baseline should the safe version, not the unsafe version. It is like having to explain on a case by case basis why you aren't using lead pipes for this application.

Always default to safety. The difference between safe and unsafe native code is usually single digit percentage points. Or weeks on a Moore scale.

Re:Python 3.11 is faster than 3.8 by cma on Wednesday October 26, 2022 @02:59PM (#33347857)
I think you replied to the wrong comment, mine was about zig vs C++.
Re:Python 3.11 is faster than 3.8 by nerpderp82 on Wednesday October 26, 2022 @03:23PM (#33348146)
I am talking about the whole stack of benchmarks and why there is a spread in perf. I am replying to you and the top level comment, if only the convos were a graph and not a tree.
Re:Python 3.11 is faster than 3.8 by vjerancrnjak on Wednesday October 26, 2022 @04:50PM (#33349230)
This should also be the case for nim. Generated C always looks very simple with -d:danger (when all memory access checks disappear).
Re:Python 3.11 is faster than 3.8 by cb321 on Wednesday October 26, 2022 @05:01PM (#33349387)
If I do nimble build -d:lto -d:danger --passC:-march=native I get 1920 fps while if I do nimble build -d:lto -d:release --passC:-march=native I still get 1775 fps. So, at least for Nim, the checks are only a 1.08x slowdown..Not so bad compared to the 2521 vs 1626 = 1.55x Zig-Rust slowdown on the author's machine.

Heck, I see 1.33x differences in run times between the first & second run of the rust branch of his benchmark, but only 1.05x diffs in run times between 1st & 2nd Nim branch.

In my experience, reasoning about things like this is rarely as simple as "bounds checks" which are (often) the most highly predictable branches.

Re:Python 3.11 is faster than 3.8 by vjerancrnjak on Thursday October 27, 2022 @02:17AM (#33353833)
True. Recently I noticed that std/sha1 takes 8x the time on M1 release vs danger. Yet on amd64 it's only a <10% difference.
Re:Python 3.11 is faster than 3.8 by insanitybit on Wednesday October 26, 2022 @02:13PM (#33347349)
Very cool idea for a benchmark, thanks for the numbers. Pretty readable code as well, nice work. Is your benchmark running headless? I feel like that could be a source of noise but idk.
Re:Python 3.11 is faster than 3.8 by Shish2k on Wednesday October 26, 2022 @02:25PM (#33347483)
Yeah, all these benchmarks are measured in headless mode - all the calculations of what should be on-screen are done, and pixels are written into a buffer ready to be displayed, but the Window is never opened and the buffer isn’t blitted.
Re:Python 3.11 is faster than 3.8 by humanistbot on Wednesday October 26, 2022 @02:06PM (#33347274)
I am shocked that PHP of all things has faster speed than python.
Re:Python 3.11 is faster than 3.8 by crote on Wednesday October 26, 2022 @02:15PM (#33347372)
I am not.

PHP is traditionally used solely for websites. Some of those have grown rather large, to the point that having engineers optimize the language is cheaper than buying more servers.

Python, on the other hand, is first and foremost a scripting language. When performance does matter, you often end up using a wrapper around a C library, like NumPy. This means there is relatively little money in optimizing the Python interpreter.

Re:Python 3.11 is faster than 3.8 by DeathArrow on Wednesday October 26, 2022 @03:09PM (#33347972)
There are websites and large scale software. Google uses Python for a lot of its products.

It would seem sensible that since Facebook poured a lot of resources in optimizing PHP, Google would have done the same for Python.

Also, Python is the first or second most popular language.

Re:Python 3.11 is faster than 3.8 by fuckstick on Wednesday October 26, 2022 @03:21PM (#33348119)
> It would seem sensible that since Facebook poured a lot of resources in optimizing PHP

Well they sort of half assed tried years ago - they employed GvR at one point. Then they gave up and they continue to lean heavily on C++, Java and there is thing they developed in the interim: Go.

> Also, Python is the first or second most popular language

Hogwash. For all the open source and other development that occurs and is “indexed” on internet discussion forums there is countless boring ass shit behind the scenes in sweatshops around the world and corporate back rooms. PHP, Java, and C# are still probably more popular to start.

Re:Python 3.11 is faster than 3.8 by hnzix on Wednesday October 26, 2022 @05:13PM (#33349538)
Corporate has a faceless horde of Java devs.
Re:Python 3.11 is faster than 3.8 by DeathArrow on Thursday October 27, 2022 @02:59AM (#33354156)
Lots of C#, too. And I specialized in C# since it seemed a little bit more enjoyable than Java at the time and the jobs were plenty. Of course, Java changed quite a bit since then so it might be more desirable than it was.
Re:Python 3.11 is faster than 3.8 by dragonwriter on Wednesday October 26, 2022 @05:38PM (#33349865)
> For all the open source and other development that occurs and is “indexed” on internet discussion forums there is countless boring ass shit behind the scenes in sweatshops around the world and corporate back rooms.

Much of which is also in Python.

Re:Python 3.11 is faster than 3.8 by ivan23178 on Wednesday October 26, 2022 @07:57PM (#33351273)
JS is also very popular. Not for the same things as Python but in some aspects it's becoming modern PHP (which is still popular, if maybe aging somewhat kinda like Perl). But Python is definitely very popular for new back room stuff (source: long digital sweatshop career)
Re:Python 3.11 is faster than 3.8 by fuckstick on Wednesday October 26, 2022 @08:28PM (#33351469)
I mean, sure, but I’m just saying point me to a source that is ranking Python as #1 or #2 most popular. If it’s TIOBE or something similar it’s meaningless.

I say this as someone that has made their career mostly on the “top 5” TIOBE languages - but that ranking is a bunch of crap for the overall commercial software world.

C? Give me a fucking break. Never in 20 years have I known there to be more C than Java jobs.

And now everyone and their dog including doctors and other non tech-savvy professionals are doing Intro to Python courses (and then never touching it again) so they too can feel like they know something about ML or stats. Lot of noise.

Re:Python 3.11 is faster than 3.8 by DeathArrow on Thursday October 27, 2022 @02:56AM (#33354135)
>I mean, sure, but I’m just saying point me to a source that is ranking Python as #1 or #2 most popular. If it’s TIOBE or something similar it’s meaningless.

According to 2022 Stack Overflow developer survey, if we exclude HTML, SQL and Bash, top five languages (as in being used by most developers) are: Javascript, Python, Typescript, Java and C#.

Re:Python 3.11 is faster than 3.8 by rovr138 on Friday October 28, 2022 @05:06AM (#33369209)
https://madnight.github.io/githut/#/pushes/

Checking here, if we go by pushes on GitHub, JS, Python, Ruby, Java, PHP are the top for 2021.

This is of course biased to only stuff on GitHu band public (assuming, but doubt they're publishing private data on BigQuery).

No idea how much of that is pet projects vs production of course.

Re:Python 3.11 is faster than 3.8 by mgkimsal on Wednesday October 26, 2022 @05:01PM (#33349384)
> Python is the first or second most popular language.

By what metrics? Redmond quarterly from June shows Python at #2, and PHP at #4.

https://redmonk.com/sogrady/2022/10/20/language-rankings-6-2...

Re:Python 3.11 is faster than 3.8 by dragonwriter on Wednesday October 26, 2022 @05:40PM (#33349883)
> > Python is the first or second most popular language.

> By what metrics?

Hmm, why don’t you answer your own question?

> Redmond quarterly from June shows Python at #2,

That clearly is one that puts it “first or second”, yes.

Re:Python 3.11 is faster than 3.8 by mgkimsal on Wednesday October 26, 2022 @05:54PM (#33350079)
Yes, it was partially an answer, and it's not saying Python isn't high up.

But still wanted to know by what metrics the GP was making their claim.

Re:Python 3.11 is faster than 3.8 by DeathArrow on Thursday October 27, 2022 @03:05AM (#33354198)
Most metrics. Tiobe, Redmonk, Pypl, Stack Overflow survey. Top 10 and even top 5 is generally the same, the order might differ a bit.

I've looked at various sources since the last 5 years and not much changed. I hoped some languages like Nim, Crystal or Zig would pick up a bit of steam, but no.

Go and Rust moved up a bit and now seem safer bets to invest some time into.

Re:Python 3.11 is faster than 3.8 by zitterbewegung on Saturday October 29, 2022 @05:48PM (#33388264)
Instagram is a Django website (Django is a Python web framework).
Re:Python 3.11 is faster than 3.8 by skybrian on Wednesday October 26, 2022 @03:34PM (#33348267)
It can't be explained by lack of effort. There have been several serious attempts to make Python run faster, including one by some Google engineers. Many of them fail, and the ones that succeeded to some extent aren't mainstream. It's hard.

For Python, the C integration probably makes things harder.

Re:Python 3.11 is faster than 3.8 by habibur on Thursday October 27, 2022 @02:58AM (#33354153)
Interesting how a language written solely for websites, beats others in their own game which isn't about websites.
Re:Python 3.11 is faster than 3.8 by bityard on Wednesday October 26, 2022 @02:14PM (#33347356)
It's not as surprising if you look at the primary use case of PHP. It takes a request, does a bunch of stuff, and returns the result to the web server. And it needs to do all of it before the user who clicked on the link gets bored and goes somewhere else. (Or the API client calling it times out...)
Re:Python 3.11 is faster than 3.8 by coldtea on Wednesday October 26, 2022 @03:27PM (#33348184)
How is that relevant? This is not about it being faster for "it's primary use case" (optimized for that, etc).

It's generally faster than Python in doing the same things as Python does, unrelated to web too.

Re:Python 3.11 is faster than 3.8 by lmm on Wednesday October 26, 2022 @08:21PM (#33351422)
The point is that PHP's primary, original use case is latency-sensitive in a way that Python's isn't. So it's always been subject to more performance pressure.
Re:Python 3.11 is faster than 3.8 by captn3m0 on Wednesday October 26, 2022 @02:20PM (#33347432)
PHP typically beats Python on CPU bound benchmarks: https://benchmarksgame-team.pages.debian.net/benchmarksgame/...

(Measures Python 3.10.4 against PHP 8.1.5 - so expecting these to change a bit).

Re:Python 3.11 is faster than 3.8 by sottol on Wednesday October 26, 2022 @02:18PM (#33347404)
I think they did a lot of similar work to what python 3.11 did and more when releasing PHP7. I'm absolutely not in the loop but remember postings here on HN a few years ago.

Meta/FB is also pretty invested in Hack (was once a php dialect, again, out of the loop), maybe they contributed a thing or two?

Re:Python 3.11 is faster than 3.8 by tedivm on Wednesday October 26, 2022 @02:48PM (#33347737)
php7 was such a huge leap they skipped php6 (although there were other reasons for that).
Re:Python 3.11 is faster than 3.8 by captn3m0 on Wednesday October 26, 2022 @03:06PM (#33347943)
> Version 6 is generally associated with failure in the world of dynamic languages. PHP 6 was a failure; Perl 6 was a failure. It's actually associated with failure also outside the dynamic language world - MySQL 6 also existed but never released.

TIL MySQL 6

https://wiki.php.net/rfc/php6

Re:Python 3.11 is faster than 3.8 by KwanEsq on Wednesday October 26, 2022 @06:49PM (#33350661)
Meanwhile Java(ECMA)Script bucking the trend by getting its failure out of the way at 4.
Re:Python 3.11 is faster than 3.8 by klyrs on Wednesday October 26, 2022 @08:25PM (#33351446)
Whereas Python doesn't even plan for 4 because 3 hurt so bad
Re:Python 3.11 is faster than 3.8 by hashar on Wednesday October 26, 2022 @03:17PM (#33348078)
PHP 5 was kind of slow, to a point it threatened the future of Facebook (now Meta). They eventually went to transform it to CPP for speed improvement hiphop-cpp then went to build a VM with jit compilation. You could then deploy your app by transfering a sqlite file containing the compiled byte code.

At Wikimedia we adopted it which has cut our CPU usage by half and has saved a few hundred of servers. We had some Facebook engineers helping which involved patching the Linux kernel while at it. Those were good times.

Eventually PHP 7 followed up with a similar approach and had more or less the same performance as HHVM. Facebook went then to focus on the Hack language (a dialect of PHP with strong typing) and eventually phased out back compat with Zend.

From what I remember, Sara Golemon at Facebook has done a lot of outreaching to Open Source project and gave us a lot of assistance (as well as others at Facebook).

Re:Python 3.11 is faster than 3.8 by nikcub on Wednesday October 26, 2022 @07:18PM (#33350928)
> PHP 5 was kind of slow

I remember it being slower than PHP4 for a while

Re:Python 3.11 is faster than 3.8 by booi on Wednesday October 26, 2022 @02:18PM (#33347405)
PHP has always been very fast of the untyped scripting languages even from the web 1.0 days.
Re:Python 3.11 is faster than 3.8 by kstrauser on Wednesday October 26, 2022 @03:40PM (#33348342)
That doesn't relate to Python, though.
Re:Python 3.11 is faster than 3.8 by nerpderp82 on Wednesday October 26, 2022 @02:18PM (#33347411)
Clearly we need a Python to PHP transpiler (trigger word) so we can be webscale (trigger word).
Re:Python 3.11 is faster than 3.8 by coldtea on Wednesday October 26, 2022 @03:25PM (#33348161)
PHP has always been several times faster than Python. Even more so with the speed updates post 7.
Re:Python 3.11 is faster than 3.8 by treeman79 on Wednesday October 26, 2022 @03:51PM (#33348485)
Back around 2003 I was hosting an internal web app for a fortune 50 company in python. It could handle 2 users at a time. I rewrote it in PHP. Scaled to hundreds with trivial work. Probably could have handled far more.
Re:Python 3.11 is faster than 3.8 by pphysch on Wednesday October 26, 2022 @08:41PM (#33351550)
The key here is that you rewrote the whole thing. The old and new languages isn't as relevant.
Re:Python 3.11 is faster than 3.8 by talideon on Wednesday October 26, 2022 @03:54PM (#33348519)
PHP 5 and onwards have had a lot of resources put int o making it fast. The surprise for me is how close Python is getting to closing that gap.
Re:Python 3.11 is faster than 3.8 by throwaway894345 on Wednesday October 26, 2022 @03:55PM (#33348545)
Facebook dumped a ton of effort into making PHP fast over the last couple of decades. No one has been able to make Python especially fast without breaking compatibility with important libraries (Python exposed virtually the entire interpreter as its extension surface, and since Python is so slow, extensions are a major part of the ecosystem as they're the main way to recoup performance, which in turn means that changing the extension interface to make things faster would break a bunch of the ecosystem, and the Python maintainers are pretty scarred after the 2->3 breaking changes). Pypy comes close, and it has been grinding away to get compatibility, but last I checked you still couldn't so much as talk to a Postgres database through a reputable package.
Re:Python 3.11 is faster than 3.8 by depr on Wednesday October 26, 2022 @05:10PM (#33349501)
The last couple of decades? Facebook is not even two decades old. And they turned it into their own language.
Re:Python 3.11 is faster than 3.8 by throwaway894345 on Wednesday October 26, 2022 @05:44PM (#33349941)
Yeah, it was a crude estimate. Call it 10-15 years if you want.
Re:Python 3.11 is faster than 3.8 by toast0 on Wednesday October 26, 2022 @04:33PM (#33349026)
PHP itself is pretty fast. It's the things that people do in PHP that get slow. Most of the Yahoo frontends were rebuilt in PHP in 200x because it was fast enough and much more usable than the thing they used before(trigger warning: hf2k)

Of course, people then go and build up sculptures of objects that will be thrown away after every request, and that stuff makes everything slow (that style of code is why PHP wasn't good enough for Facebook, IMHO), but you can build trash sculpture in any language.

Re:Python 3.11 is faster than 3.8 by mgkimsal on Wednesday October 26, 2022 @05:03PM (#33349418)
100% not shocked at all.

I recall a couple of times over the past 15-20 years where a current python significantly outperformed a current php version, but my recollection is that php was usually a bit faster, or sometimes a lot faster.

PHP 7 was released on dec 2015, and gained significant speed bumps and better memory usage, with the average php execution time being cut in half, or more, generally without any code change whatsoever. It was quite remarkable.

The path from 7.x-8.1 so far has generally seen incremental speed bumps again - usually somewhere between 3-8% improvements per release. Obviously this is going to be dependent on use cases, but overall it's been a fairly steady set of speed improvements over the last 7 years.

Re:Python 3.11 is faster than 3.8 by GlitchMr on Wednesday October 26, 2022 @07:17PM (#33350921)
PHP is a much simpler language which helps a lot with optimizations. For instance, consider something like property access.

In PHP, doing `$a->b` accesses field `b` of object `$a`. The implementation is essentially type check (making sure `$a` is an object) and hash table lookup. If this fails, then it calls `__get` method if it exists.

In Python on the other hand, `a.b` involves `__getattr__`, `__getattribute__`, `__mro__`, `__get__`, `__set__`, `__delete__` and `__dict__`. Here is a description of how the attribute access works: https://docs.python.org/3/howto/descriptor.html#overview-of-....

Re:Python 3.11 is faster than 3.8 by stavros on Wednesday October 26, 2022 @02:12PM (#33347335)
Can you try PyPy as well?
Re:Python 3.11 is faster than 3.8 by Shish2k on Wednesday October 26, 2022 @02:39PM (#33347632)
Apparently not :( (These benchmarks were done on an M1 MacBook Pro)

$ brew install pypy3 pypy3: The x86_64 architecture is required for this software.

Re:Python 3.11 is faster than 3.8 by verst on Wednesday October 26, 2022 @02:43PM (#33347677)
$ brew install pyenv

$ pyenv install pypy3.9-7.3.9

I like using PyEnv for managing my Python versions. It will natively compile Python builds and should be doing the same on M1 (which is what I'm using). `pyenv install --list` shows you what is available.

EDIT: Not sure why they don't have newer versions of PyPy there (I don't use PyPy) but all it takes is a PR to here: https://github.com/pyenv/pyenv

Re:Python 3.11 is faster than 3.8 by Shish2k on Wednesday October 26, 2022 @03:36PM (#33348294)
Thanks! Added to the list, it’s suspiciously only a little bit faster than CPython 3.11 though, which probably needs more investigation...
Re:Python 3.11 is faster than 3.8 by hnov on Wednesday October 26, 2022 @05:02PM (#33349396)
That makes sense because pypy can JIT a ladder of if/else to a compare/jump conditional each whereas a dict lookup can be an order of magnitude more complex. If they're 8bit opcodes, maybe having a list based lookup table will perform similarly on py3000 and still optimize on pypy?

Or maybe https://docs.python.org/3/library/array.html rather than list.

Re:Python 3.11 is faster than 3.8 by gw99 on Wednesday October 26, 2022 @01:57PM (#33347161)
Interesting. On the trifecta of money vs pain vs speed, Go seems to be a reasonable compromise.
Re:Python 3.11 is faster than 3.8 by Thaxll on Wednesday October 26, 2022 @02:15PM (#33347368)
It's probably "slow" because of sdl and cgo not native Go code. An emulator doesn't do much actually, it's about fixed array, bit shifting etc ...
Re:Python 3.11 is faster than 3.8 by jbverschoor on Wednesday October 26, 2022 @03:38PM (#33348317)
Yup.. a 240x performance difference (zig-py) has very little to do with the language, vm, or whatever. As soon as I saw that, I dismissed the benchmark.

By these standards a 10 year old cpu with a beefy GPU will beat any new cpu as well.

Re:Python 3.11 is faster than 3.8 by chlorion on Wednesday October 26, 2022 @09:20PM (#33351821)
What does a GPU have to do with any of this? I think you may be a little confused about something but I am not sure what.
Re:Python 3.11 is faster than 3.8 by logicchains on Thursday October 27, 2022 @03:35AM (#33354411)
>Yup.. a 240x performance difference (zig-py) has very little to do with the language, vm, or whatever.

It absolutely does. A simple for loop in the standard Python interpreter will literally take 100x longer than the same thing in a language like C/C++, try for yourself if you don't believe me. CPython is unbelievably slow.

Re:Python 3.11 is faster than 3.8 by jbverschoor on Thursday October 27, 2022 @05:55AM (#33355398)
Very interesting... Simple loop (0 -> 320000000) and add 1 to a variable.

I couldn't reproduce 100x (no optimization flags, otherwise it won't do anything)

  Apple clang version 14.0.0 (clang-1400.0.29.102) -> 0m0.347s
  ruby 3.1.2p20 -> 0m11.314s
  Python 3.8.12 -> 0m19.662s
So ruby is almost 60% faster, but the C version is "only" 32x faster than ruby. 55x python.

I thought the differences would be smaller these days

Re:Python 3.11 is faster than 3.8 by Shish2k on Wednesday October 26, 2022 @07:05PM (#33350811)
> It's probably "slow" because of sdl and cgo not native Go code

The other languages are also using SDL via their respective interacting-with-C interfaces - what makes Go special here?

Re:Python 3.11 is faster than 3.8 by Thaxll on Wednesday October 26, 2022 @08:04PM (#33351328)
Re:Python 3.11 is faster than 3.8 by chlorion on Wednesday October 26, 2022 @09:22PM (#33351840)
The author stated that the benchmarks run in headless mode, so I am not sure that it's SDL that's slowing it down here.

Even if it is SDL slowing it down, Go FFI being slow is still a real disadvantage compared to the other languages, and you can't just pretend like it doesn't exist in this case.

Re:Python 3.11 is faster than 3.8 by redox99 on Wednesday October 26, 2022 @02:43PM (#33347672)
It depends really. In this case, a game emulator, you can only get away with it because it's for an ancient game console. But otherwise you definitely cannot afford 5x slowdown compared to CPP for a game.
Re:Python 3.11 is faster than 3.8 by friedman23 on Wednesday October 26, 2022 @02:52PM (#33347775)
Maximize pain for mediocre speed and money?
Re:Python 3.11 is faster than 3.8 by Shish2k on Wednesday October 26, 2022 @02:57PM (#33347834)
FWIW I personally found Rust the least-painful language, but that may well be confirming my pre-established biases :)

- With Zig I kept running into compiler bugs, plus no package manager (I’ve vendored SDL and Clap into the source tree)

- C++ I’d occasionally shoot myself in the foot in ways that other languages would have caught, plus no package manager (OS-level package management does an OK job, so long as you don’t mind using old versions, and faffing about with different operating systems acting very differently)

- The pain from Rust was one time where the compiler wanted me to specify a lifetime, and I didn’t understand, so I just spammed lifetime specifiers in various places until it compiled. I’ve been using Rust for a couple of years now and I still don’t really understand lifetimes, but thankfully 99% of the time I can avoid them.

- Nim was a relatively nice language but massively lacking in available libraries (like even parsing command line arguments took me a day just trying to find a library which worked)

- Go is pretty nice, my main pain is the tolerable but constantly-annoying verboseness of error handling (`err := foo(); if err != nil {return err}` compared to rust’s `foo()?`)

- PHP I just hate on a deep and personal level thanks to years of being a PHP4/5 developer. The language is actually mostly-ok-ish these days, but the standard library is still full of frustration like inconsistent parameter orders within a family of functions.

- Python is all-round really nice to write, but the test suite takes like 20 minutes to run, which really messes with my flow-state

Re:Python 3.11 is faster than 3.8 by Thaxll on Wednesday October 26, 2022 @03:05PM (#33347932)
"Rust the least-painful language"

" I’ve been using Rust for a couple of years now and I still don’t really understand lifetimes"

Seems like a major pain point.

Re:Python 3.11 is faster than 3.8 by Shish2k on Wednesday October 26, 2022 @03:14PM (#33348034)
It would be if I ran into it regularly -- but after using the language for a variety of professional and personal projects for a couple of years, this is the only time I’ve actually needed to manually-specify lifetimes since the compiler normally figures it out for me :)
Re:Python 3.11 is faster than 3.8 by pkolaczk on Wednesday October 26, 2022 @03:26PM (#33348169)
As long as you don't get too crazy with references in structures or async code, lifetimes are not going to chase you.
Re:Python 3.11 is faster than 3.8 by galangalalgol on Wednesday October 26, 2022 @03:46PM (#33348421)
Generics too right?
Re:Python 3.11 is faster than 3.8 by pdimitar on Wednesday October 26, 2022 @09:23PM (#33351844)
I know people say that about everything but Rust generics are very readable and do make sense after you understand what problem are they solving.

They do look intimidating to start with, admittedly, and I'll concede that's a negative point for Rust. But it does get better if you practice for a bit.

Re:Python 3.11 is faster than 3.8 by galangalalgol on Wednesday October 26, 2022 @10:53PM (#33352497)
No I just meant using generics seems to require explicit lifetimes decently often. I don't understand why.
Re:Python 3.11 is faster than 3.8 by pdimitar on Wednesday October 26, 2022 @10:58PM (#33352543)
Not sure that's the case btw. I have noticed it with some libraries but I've used and crated a fair amount of generics without having to annotate stuff with lifetimes.

Lifetimes are necessary when you want to explicitly say "variable X will live just as long as variable Y", or sometimes it's more complex (i.e. you have to specify 2 or more separate lifetimes and then return something that pertains to only one of them) but it's still fairly predictable if you keep it all in your head while coding.

Don't get me wrong I still hate it but it's not as terrible as many people make it out to be. It's hard to get into but also very logical and graspable.

Re:Python 3.11 is faster than 3.8 by robocat on Wednesday October 26, 2022 @03:15PM (#33348049)
They are saying rust is painful. Just that they find the other languages even more painful.
Re:Python 3.11 is faster than 3.8 by davisoneee on Wednesday October 26, 2022 @03:26PM (#33348171)
Except that they don't really say rust was painful....just that there was one specific moment / aspect that they found tricky.
Re:Python 3.11 is faster than 3.8 by Ar-Curunir on Wednesday October 26, 2022 @03:49PM (#33348462)
The reason it's not a pain would be explained by the rest of the sentence which you omitted: "but thankfully 99% of the time I can avoid them"
Re:Python 3.11 is faster than 3.8 by UnpossibleJim on Wednesday October 26, 2022 @03:12PM (#33348024)
This is actually a really nice write up for people deciding which language to learn/use if they aren't constrained.
Re:Python 3.11 is faster than 3.8 by pjmlp on Wednesday October 26, 2022 @04:09PM (#33348706)
C++ has two relatively good package managers, conan and vcpkg.
Re:Python 3.11 is faster than 3.8 by cb321 on Wednesday October 26, 2022 @04:15PM (#33348767)
Re CLIs in Nim..Most find https://github.com/c-blake/cligen easy to use.
Re:Python 3.11 is faster than 3.8 by stock_toaster on Wednesday October 26, 2022 @08:36PM (#33351507)
Nim does seem nice. I worry that it will end up like D though... An interesting/cool/neat language with seemingly relatively little adoption. I'm keeping my eye on it though.
Re:Python 3.11 is faster than 3.8 by DeathArrow on Wednesday October 26, 2022 @03:11PM (#33348010)
Also C# is quite reasonable.
Re:Python 3.11 is faster than 3.8 by plainOldText on Wednesday October 26, 2022 @02:50PM (#33347757)
Just a quick glance at you repo, and I'm noticing you're running Zig `zig build -fstage1 -Drelease-fast=true` and Rust `cargo run --release` with the release flags on. You should do the same for Nim `nimble build -d:release --opt:speed`; Go too.
Re:Python 3.11 is faster than 3.8 by Shish2k on Wednesday October 26, 2022 @03:01PM (#33347887)
Updating nim’s compiler flags, seems to be ~4x faster now :D

    nim: Emulated 600 frames in  0.44s (1367fps)
Do you happen to know the right flags for release-mode Go? Last time I checked (admittedly years ago) I thought they just had the one “reasonably fast and reasonably debuggable” build mode
Re:Python 3.11 is faster than 3.8 by cb321 on Wednesday October 26, 2022 @03:10PM (#33347990)
With Nim `nimble build -d:danger -d:lto --passC:-march=native` I just got 1920 frames/s while with your rust build only 1237 fps on the same machine (EDIT: and 1307 frames/s with C++.)
Re:Python 3.11 is faster than 3.8 by plainOldText on Wednesday October 26, 2022 @03:14PM (#33348045)
In that case, did you also run rust with the equivalent RUSTFLAGS="-C target-cpu=native"? :)

Perhaps benchmarks should be compared on equal footing, say, with the default release flag or all optimizations turned on, otherwise they're improper.

Re:Python 3.11 is faster than 3.8 by cb321 on Wednesday October 26, 2022 @03:17PM (#33348084)
I tried. That actually made the rust slower for me (i7-6700k, gcc-12.2, rustc-1.64). 1180 frames/s. And without the -march=native the Nim was at 1620. And it also did not help the C++ branch (but helped Nim about 1.2x).

But really the original author/poster should do some set on his box. I cannot even compile/run all his things. The point of my comment was just to give a reference for how far off impressions can be from build flags. PGO (available to Nim, c++, but maybe not to Rust yet?) is a whole other set of maybe nothing burgers or maybe big improvements.

(But, btw, I could not agree more that all experiments in this entire general space should have various big, bold disclaimers. Over-concluding from these things is rampant.)

Re:Python 3.11 is faster than 3.8 by plainOldText on Wednesday October 26, 2022 @03:26PM (#33348180)
Yes, tweaking the compiler flags can alter the performance substantially. I'm glad to see Nim so fast though.
Re:Python 3.11 is faster than 3.8 by cb321 on Wednesday October 26, 2022 @04:00PM (#33348606)
And with the author's hot off the presses nim flags I get only 1464 fps. So, 1920/1464 = 1.31 for my nim compile flags vs. his new ones, only a little less than the 2521/1626 that was interesting people.

For something super jumpy like a simulator, I would find it unsurprising for PGO to make up (or surpass) the difference to Zig in both Nim and C++. 20 years ago there was this ACOVEA [1] project to try to discover great sets of gcc flags that could often find 2X improvements in object code speed for me.

The range from build flags/procedures is often much greater than the supposedly interesting cross-language variation. These things often more measure developer experience/persistence than something intrinsic (and build flags/procedures are only part of that experience/persistence).

[1] https://github.com/Acovea/libacovea

Re:Python 3.11 is faster than 3.8 by plainOldText on Wednesday October 26, 2022 @03:03PM (#33347907)
I do not, sorry. I'm sure some Go developer can chime in.
Re:Python 3.11 is faster than 3.8 by Thaxll on Wednesday October 26, 2022 @03:02PM (#33347894)
There is no release flag for Go.
Re:Python 3.11 is faster than 3.8 by throwaway894345 on Wednesday October 26, 2022 @03:51PM (#33348489)
To elaborate, Go is always in release mode because release compiles are about as fast as other languages' debug modes.
Re:Python 3.11 is faster than 3.8 by LukeShu on Wednesday October 26, 2022 @04:08PM (#33348696)
I'd phrase that as "release mode by default", not "always in release mode".

There are various debug things you can turn on, such as the race detector, the memory sanitizer, or coverage tracking.

Re:Python 3.11 is faster than 3.8 by streblo on Wednesday October 26, 2022 @03:11PM (#33348001)
Is there such a thing for go?
Re:Python 3.11 is faster than 3.8 by LukeShu on Wednesday October 26, 2022 @04:12PM (#33348741)
It's the default for Go; with Go you have to explicitly turn on the debug features that you want.
Re:Python 3.11 is faster than 3.8 by jetbalsa on Wednesday October 26, 2022 @03:29PM (#33348212)
I find it /really/ funny that PHP is faster then python at this. as a PHP code hacker, I love that PHP keeps coming up faster then python in a ton of tasks
Re:Python 3.11 is faster than 3.8 by throwaway894345 on Wednesday October 26, 2022 @04:10PM (#33348715)
That's not much of a flex, Python is deliberately slow--the reasoning was that the implementation should be simple and they'd just expose the entire interpreter as the extension API, and then people would just write extensions in C when they needed the speed. Since vanilla Python is so slow, the entire ecosystem is highly dependent on C extensions for passable performance, and since the C extension API is virtually the whole interpreter, changes that would make the interpreter fast would typically break much of the ecosystem.

Unfortunately, "just write the performance-sensitive bits in C" is pretty impractical because it only works when you're handing the C routine a relatively small amount of data relative to the amount of work to be done on that data (otherwise the costs of marshaling to C data structures will quickly eat up any gains from processing in C). And unfortunately, it turns out that a whole bunch of code works this way, so the whole bargain of "slow interpreter + easy C extensions" breaks down for a lot of real-world applications, but now we're locked into it.

Re:Python 3.11 is faster than 3.8 by wheelerof4te on Wednesday October 26, 2022 @05:04PM (#33349423)
CPython can be fast once you eliminate the main bottleneck: the interpreter itself.

Processing loops adds a lot of overhead because the interpreter has to make the case jumps after each loop. Figuring out a way to minimize that overhead by using built-in data structures and stdlib library will speed up your code by an order of magnitude.

Don't forget, the built-in types are already running in C.

Re:Python 3.11 is faster than 3.8 by throwaway894345 on Wednesday October 26, 2022 @05:12PM (#33349523)
I mean, yes, but that's probably not viable in many real-world applications. Usually you have a pretty complex data model (some graph-like structure) with Python methods that traverse it. You can't easily push that into native Python structures (at least not with any significant performance gain), and while you can push the whole thing into Rust or some faster language, at that point all of the interesting stuff is happening in Rust so why use Python at all (why pay the significant costs of a hybrid application)?
Re:Python 3.11 is faster than 3.8 by pbowyer on Wednesday October 26, 2022 @05:27PM (#33349730)
At least writing Python extensions is relatively easy (I understand, not done it) unlike PHP. PHP leans heavily on poorly documented C macros and the internals is tricky to grasp. At least it now has a minimal FFI module.
Re:Python 3.11 is faster than 3.8 by throwaway894345 on Wednesday October 26, 2022 @05:42PM (#33349903)
Honestly I think it’s much better to optimize for the native use case rather than FFI.

FFI can pervade an ecosystem making changes (including performance optimizations) to the host language more difficult. It also tends to complicate build and deployment stories. For example, from my Mac I can trivially build a native binary that will Just Work on any Linux system, even one without a libc. Contrast that with Python where I can’t even install popular packages onto most non-Ubuntu Linux distros. And there’s a lot of other things like that which crop up with pervasive FFI.

I’m convinced that the FFI sweet spot is “difficult but possible” and native performance should be good enough 99% of the time.

Re:Python 3.11 is faster than 3.8 by pcwalton on Wednesday October 26, 2022 @03:43PM (#33348372)
Looking at Rust vs. Zig, you have the FLAGS register in a bitfield for Zig but it's separate bools for Rust. This is probably making your Rust code slower because the CPU can't set multiple flags at once.

I'm also wondering if all your #[inline(always)] is slowing things down.

Re:Python 3.11 is faster than 3.8 by Shish2k on Wednesday October 26, 2022 @04:08PM (#33348685)
Yeah, I really like zig’s approach to bitfields, everything Just Works with no faffing about with bit-shifting and OR/AND'ing. I forget the exact reason I used separate bools for rust, but I remember spending a day trying to do something zig-like and failing…

IIRC each of the #[inline] statements was tested and each made a noticable performance improvement. That’s especially true for things like RAM::get() - since the gameboy does I/O by having different chunks of RAM act differently (some address ranges are just RAM, some are hardware controls, some read data from the cartridge, etc) you can replace the hundred-line generic get() with a single instruction if you happen to know that you are looking up one hard-coded address, and that address has no special behaviour.

Re:Python 3.11 is faster than 3.8 by Rusky on Thursday October 27, 2022 @01:54AM (#33353673)
I ran the Rust and Zig implementations through a profiler and (at least on my machine, using Zig nightly 0.10.0-dev.4583+875e98a57) the vast majority of the time difference is from the number of calls to the GPU paint_tile_line function- there's some behavioral difference upstream in the emulator and the GPU modules are just not doing the same work.

For the benchmark 600 frames, the Rust emulator calls it 2500049 times, but the Zig emulator only calls it 1808121 times. This is very roughly the ratio between the reported times.

At least one source of this discrepancy is that the Zig emulator doesn't think any sprites are active, but that doesn't account for all of it, and I'm hitting some crashes trying to run it with the display, so I'm probably not going to investigate it further.

(I also hit the same sort of strange Zig compiler unreliability as some other comments have mentioned, where I had to add some logging in seemingly random places to get the Zig emulator to run at all.

And the argument parser just blatantly returns a dead pointer to the ROM path; when I first ran it it just gave "error: InvalidUtf8" until I tracked that down.)

Re:Python 3.11 is faster than 3.8 by pcwalton on Thursday October 27, 2022 @08:50PM (#33366107)
Fascinating, thanks a bunch for looking into this. Benchmarking is hard!
Re:Python 3.11 is faster than 3.8 by talideon on Wednesday October 26, 2022 @03:52PM (#33348497)
I think a bigger thing to point out is that it's within shouting distance of PyPy, and there's plenty more work that can be done to make it faster. Next up is a mini-JIT, which should help in places with tight loops.
Re:Python 3.11 is faster than 3.8 by FerociousTimes on Wednesday October 26, 2022 @03:53PM (#33348515)
Can you do Node 19.0 and Ruby 3.1.x too please?
Re:Python 3.11 is faster than 3.8 by Shish2k on Wednesday October 26, 2022 @04:14PM (#33348763)
TypeScript and Java are the two remaining languages where I feel I know them well enough to translate 3k lines of code in a weekend each -- I have approximately zero knowledge of Ruby though, and no motivation to learn it ^^; Pull requests are welcome though!
Re:Python 3.11 is faster than 3.8 by FerociousTimes on Wednesday October 26, 2022 @04:35PM (#33349061)
OK, I'll watch your repo and track the updates and see when you release the JS/TS port and then I'll a give it a try and port it to Ruby and send it your way.

Best wishes

Re:Python 3.11 is faster than 3.8 by mgkimsal on Wednesday October 26, 2022 @05:05PM (#33349429)
Your comment in PHP src here https://github.com/shish/rosettaboy/blob/master/php/run.sh

says

# opcache in 8.1 gives a nice speedup (25s to 10s)

Is the 23.7 seconds above using the 8.1 opcache?

Re:Python 3.11 is faster than 3.8 by wk_end on Wednesday October 26, 2022 @06:12PM (#33350280)
I'd be super curious to run a profiler on those Python builds and see where it's spending most of its time.
Re:Python 3.11 is faster than 3.8 by b_mc2 on Wednesday October 26, 2022 @07:52PM (#33351223)
Very cool benchmark. I don't think this will be a huge difference but could you try mypyc? I'm definitely curious how it compares. It uses mypy to perform type checks and claims "Existing code with type annotations is often 1.5x to 5x faster when compiled. Code tuned for mypyc can be 5x to 10x faster."[1]

[1] https://mypyc.readthedocs.io/en/latest/introduction.html

Re:Python 3.11 is faster than 3.8 by lr1970 on Wednesday October 26, 2022 @08:07PM (#33351346)
To make Rust release build run faster you should enable LTO (Link Time Optimization). In the Cargo.toml put

[profile.release]

lto = true

Re:Python 3.11 is faster than 3.8 by justinsaccount on Wednesday October 26, 2022 @08:09PM (#33351356)
This might speed up the py impl a few %

  --- a/py/src/cpu.py
  +++ b/py/src/cpu.py
  @@ -260,7 +260,6 @@ class CPU:
               param = None
               cmd_str = cmd.name
               self.PC += 1
  -        self._debug_str = f"[{self.PC:04X}({ins:02X})]: {cmd_str}"
Re:Python 3.11 is faster than 3.8 by habibur on Wednesday October 26, 2022 @08:11PM (#33351365)
Here's his latest numbers after optimizations. From the repo.

     zig: Emulated 600 frames in  0.23s (2880fps)
      rs: Emulated 600 frames in  0.35s (1691fps)
     cpp: Emulated 600 frames in  0.40s (1519fps)
     nim: Emulated 600 frames in  0.44s (1367fps)
      go: Emulated 600 frames in  1.78s (338fps)
     php: Emulated 600 frames in  9.28s (65fps)
      py: Emulated 600 frames in 33.10s (18fps)
That's after Python cheated with native implementation of bitblt while all other implementations are doing it pixel by pixel, which is more correct.
Re:Python 3.11 is faster than 3.8 by cb321 on Thursday October 27, 2022 @05:32AM (#33355242)
Tables like this routinely lead to over-conclusion. Merely adding -d:lto (which changes no semantics) to the nimble build line boosts fps by 1.27x for me on nim-devel gcc-12 Skylake, making it 1.4x faster than the Rust on the same machine.

Applying BS scaling to the above table, Nim would score 1736, only slightly faster than the Rust. Who knows what little flag tweaks in cpp/rs/zig could similarly re-arrange the numbers?

Now, why is -d:lto (or -d:release) not the default build mode? Well, because it takes time and (some) people already complain about compile times and workflow (sometimes). Trade offs abound almost as much as over-conclusion. ;-)

Re:Python 3.11 is faster than 3.8 by sandGorgon on Wednesday October 26, 2022 @11:46PM (#33352836)
what about java graalvm ?
Re:Python 3.11 is faster than 3.8 by make3 on Sunday October 30, 2022 @03:38PM (#33397832)
do you use numpy for the numerical python stuff