20 ms·
You Should Compile Your Python and Here’s Why
- dikei 5y agoThis `mypyc` sounds too good to be true, I wonder what the catch's.
- fgeiger 5y agoThe obvious catch is that the compiled binary is not portable. You need to compile and deploy for each platform separately. You don't need to worry about platforms at all when deploying/sharing pure Python modules.
- tempnow987 5y agoBut you do have to worry about dependency management etc etc.
- shankr 5y agoyou do have to worry about the platform specific dependencies.
- roelschroeven 5y ago"Mypyc is currently alpha software. It’s only recommended for production use cases with careful testing, and if you are willing to contribute fixes or to work around issues you will encounter." At least for now, I wouldn't use it in production.
- wheelerof4te 4y agoInstead of making up some arcane ways to hack Python, try using builtin data structures and functions more. I see a bunch of Python code that is just a massive dump of custom classes on top of classes, inheriting other classes. The amount of pointer chasing to get to the actual primitives can be mind-bogling. Add that to a big nested loop and you've got a performance tragedy.
- tails4e 5y agoSome interesting tidbits on python code and compilation performance 1) During AoC I mostly used cpython and numpy. One day my code was running too slow so I fired up pypy. It ran even slower. Turns out numpy and pypy do not play well together in terms of performance. 2) I use matlab in work, and had some code that was slower than I liked, despite efforts to profile and optimise. I used the coder tool to compile it to C and got a 32x speedup. I was surprised, as matlab has a jit, so didn't expect a 32x performance delta, but there it is.
- jwilk 5y agoWhat's AoC?
- tails4e 5y agoAdvent of Code. Its a yearly coding challenge in December. Increasingly difficult problems each day, and an overall very fun way to brush up on algorithms or learn a new language.
- e12e 5y agoSee eg: https://news.ycombinator.com/item?id=29403522 https://news.ycombinator.com/item?id=29403522
- maxerickson 4y agoCython is good for little numpy shims.
- deleted 5y ago[deleted]
- fgeiger 5y agoFrankly, I would find it much easier to write the naive C version than this "moderately" optimized Python version in the article although I am much more familiar with Python than with C.
- lorenzfx 4y agoEspecially reading and understanding the "moderately" optimized Python version is a lot harder than reading the C version. Also I really dislike python optimization tricks like `output = stdout.buffer.write`.
- bsder 4y agoI really hate these kinds of articles because they don't DO anything useful in the sample programs. Grab a web page and parse it for some data. Suddenly, C looks like trash. The network latency negates any speed advantage from C and the fact that C has to manually manage memory everywhere makes your C code look like a dumpster fire relative to the Python code. You can always pick a task that favors one language or the other. What people forget is that fast/slow ALSO includes "time to develop the code". If I can write the Python code in 1 hour and the C code in 10 hours, the code has to be a lot slower or be run a lot more times before the time wasted writing the C code pays off.
- BenjiWiebe 4y agoGood point. I love C, but I can't imagine doing web scraping in it. I pick up Ruby for that.
- bcrl 4y agoAnd if he'd applied the same optimizations to the C version, it would have trounced the optimized Python version. I really don't like it when authors unfairly penalize their "not preferred" solution -- it makes me feel like the entire time spent reading the article was nothing more than listening to pointless chest thumping by the author. The whole article was barely about the actual compiling of Python code.
- infocollector 5y agoThanks for writing this! I just reproduced your experiment and could get the ~10x performance gain (0.42/0.02) on my mac m1 (I had to uninstall typing - which hopefully gets fixed soon). I think this is a great way of attacking both type checking, as well as a meaningful speedup in the CI cycle.
- sgtnoodle 5y agoI've been writing some python the last few days. Today I did something terrible. I like to believe it was out of wisdom rather than ignorance. I needed to do some nuanced bit manipulations, and so I built up a string of ascii 0's and 1's, sliced the string, and then converted back to integers. I worked on a codebase many years ago when I was an intern. That code would read frames off a CAN bus in a car. For some reason, it could only handle about 100 frames per second before running out of CPU. It turned out someone had done the same; they turned every 8 byte CAN payload into a string of 64 characters, sliced the relevant fields out, and then converted back to integers. There were about 2000 frames per second coming in, and it was just way too inefficient. In my usage, it will maybe never happen, outside of a unit test I wrote for it. I am really tempted to rewrite it tomorrow to use but shifts...
- taneq 4y agoReminds me of that Tao of Programming koan (if I remember it rightly) that went something like: The novice, in his frustration, struck the side of his computer. The master walked over and asked what he was doing. The novice exclaimed, "my computer is not working and I do not know why!" The master admonished him, saying "you cannot solve the problem by striking the computer without knowing what is wrong." Then the master struck the side of the computer, and it worked flawlessly. (I love this one because one time I was doing a gig and my computer refused to boot. Suspecting that the vibration from being lugged into the car and taken for a long drive had unseated something slightly, I gave it a sharp smack and power cycled it, and it started up fine.)
- bitwize 4y agoIt was one of the AI koans in Jargon: > A novice was trying to fix a broken Lisp machine by turning the power off and on. > Knight, seeing what the student was doing, spoke sternly: “You cannot fix a machine by just power-cycling it with no understanding of what is going wrong.” > Knight turned the machine off and on. > The machine worked.
- deleted 4y ago[deleted]
- xarope 5y agoHe says no inlining nor unrolling of code, but isn't the precompute_template (which generates pregenerated output) basically inlining+unrolling over the 3*5 combinations?
- nigma1337 4y agoThat part is just about the naïve C
- sgtnoodle 5y agoI played around with cython once. It was pretty cool! It couldn't handle simple list comprehensions properly, though, and would cause fun crashes like reference counting `None` down to 0 and then garbage collecting it.
- 1vuio0pswjnm7 5y ago"Python is Slow, And That's Fine, Because It's Fast Enough" At least this is honest. No matter what the script does, no matter how fast the libraries, the Python intepreter has a slow startup time. On multiple occasions I have seen people commenting on HN argue that Python is not slow. I think for these commenters Python is "fast enough". For others, like me, it may not be "fast enough". IOW, the question is not whether Python is "fast" or "slow", but whether it is "fast enough" or "too slow" for a given user. For some users, it might not be fast enough. And that's fine.
- ianai 4y ago(Thinking into the wild here…) Wonder if python could be sped up by not being loaded as usual per call. Ie something more like a service with careful namespace management. I’d think something that copies what’s already in ram to new registers would be much faster.
- taneq 4y agoPython, while slow, is "fast enough" because it's super easy to add native objects for anything that's too slow. It's kind of an inversion for the usual scripting language layout with fast native code loading slow snippets of script to provide customization, and tbh that's probably the better approach for most situations. Write the 99% of your application that's not performance critical in something quick and easy and flexible, and then optimize the heck out of the remaining 1% that matters. (Not a card carrying Python fan or anything, haven't used it in a decade, I just like this architecture.) > IOW, the question is not whether Python is "fast" or "slow", but whether it is "fast enough" or "too slow" for a given user. 100% this. There isn't REALLY any such thing as good/bad/fast/slow/cheap/expensive, there's just "meets spec better" / "doesn't meet spec as well". Everything is relative to your requirements.
- formerly_proven 4y agoThis works well for applications where you have that kind of split, but there are applications where most of the time is spent scattered around a lot of the code - and if you are in that situation in Python and need to go faster, it's an awkward place to be.
- fabian2k 5y ago> Unlike Cython, mypyc doesn’t directly support interfacing with C libraries or speeding up numeric code. I'm not sure I understand that part of the mypyc documentation (https://mypyc.readthedocs.io/en/latest/introduction.html#why-mypyc https://mypyc.readthedocs.io/en/latest/introduction.html#why...). Does that mean that you can't use something like numpy at all?
- hauntsaninja 4y agoYou can use numpy just fine. Unlike Cython, mypyc doesn’t give you a way of accessing numpy’s C API (because you’re basically just writing Python code).
- rmbyrro 5y agoOne thing I like about Python is the easiness of debugging issues from production logs. I think a mypyc version would be an order of magnitude harder to debug.
- mjburgess 4y agoI have rarely read a tech article this long without giving over to skim reading till the bottom. The tone and empathy with the target audience ('s thought process) is on-point.
- linsomniac 4y agoglyph is ridiculously good at the intersection of smart, funny, empathetic.
- mule1 4y agoNot to be shit-eating, but I never understood why people use python instead of Go other than for ML teams. I program mostly in Java and Go so would love a perspective.
- TrackerFF 4y agoPython is incredibly easy to learn, so more people learn it as their first language these days.
- Filligree 4y agoConsider that a lot of ML programs sit at 100% CPU use (on a single core), and 50-60% GPU use (if you're lucky). The common use of Python is a huge issue.
- 0xbadcafebee 4y agoProbably for the same reasons people use Java and Go instead of C. It's just more convenient sometimes. But sometimes not. In terms of building a team at a company? In my experience it's been arbitrary. Manager / Lead has experience in language X, decides to hire people who also know it. Or all the other teams are already using language X. Or Manager / Lead has heard "X language is good at Y" and decides to go with that. Or there's simply 10x more engineers (and cheaper) available for language X than Y. The times I've seen a language picked for a particular purpose: - Perl/Python used for web apps. It's interpreted so you can just upload your source code and refresh your browser. Faster and simpler than having to compile/package it, lots of useful frameworks and modules, lots of developers. - Erlang/OTP for telecom. Kind of on the nose, but there you are. - C and C++ for embedded applications.
- mule1 4y agoYeah I get that, the same reason why I question some of the teams I've been on choice of JS frameworks, it's just what the developers were familiar with. Go compiles so fast and reads cleaner imo, that I never saw the benefit of python. I always thought debugging python was clunky, could be unfamiliarity with interpreted languages. Also a few times I needed to use an ODBC driver with python left a bad taste in my mouth. But overall I get what you're saying, my new team writes all of their lambdas in python so I'm going to have to learn.
- FrenchAmerican 4y agoI'm learning Python. Do someone knows a resource to learn the well-known tricks for "fast" Python? A book, a website, a cheat sheet or even a MOOC or part of a MOOC?
- stefanos82 4y agoFluent Python; go for it!
- FrenchAmerican 4y agoThanks! I appreciate, really.
- stefanos82 4y agoI've just noticed it did not include my URL in original reply...weird. Anyway, here it is, this is the second edition of the book https://www.amazon.com/dp/1492056359 https://www.amazon.com/dp/1492056359 I read the first one at work a few years ago and was fascinated with all the goodies it shared with us. In my humble opinion, it's a must read by all Python developers, experienced or not; everyone will learn something new just by reading it.
- marcosdumay 4y agoI would advise against optimizing your Python programs for speed. Fighting against a language's nature never leads to good results, it's much easier and better to use a faster language if (or where) you need the speed. This is even more relevant if you are still learning the language. Focus on learning it, and leave arcane always changing implementation details for after you know it well.
- FrenchAmerican 4y agoOK, thanks, I will take that into account.
- detaro 4y ago
- zzzeek 4y agoI've been typing SQLAlchemy for some months now and there's lots of scenarios where if you end up on mypy's issue tracker (which is very often), you will see "oh use a #type: ignore to deal with this occasional use case" as official advice. And here we see a compiler that considers all "# type: ignore" to be bugs and will eventually refuse to compile them. That's simply not realistic for the way Python typing works right now and I am skeptical.
- hauntsaninja 4y agomypyc has escape hatches, e.g. casting to Any is a common trick.
- zzzeek 4y agohow, when mypy itself has only "type: ignore" as a workaround for many scenarios? here's a list of 41 issues where either the user or the mypy devs are saying "use type: ignore as a workaround for now": https://github.com/python/mypy/issues?q=is%3Aopen+label%3Afalse-positive+type%3A+ignore https://github.com/python/mypy/issues?q=is%3Aopen+label%3Afa...
- ok123456 4y agoA big portion of python's slowness to start up is that 'import' actually evaluates any code at the top level of the file it's importing. If you just have function and class definitions, this isn't too bad, but when you start doing things like setting up caches, reading files, and testing whether or not there's a GPU at the top level, you can add several seconds to the startup and balloon your memory usage. One wrong import somewhere in your code base, and your application will craw to a standstill on launch.
- lorenzfx 4y agoDoes anyone have any personal experience in applying mypyc on type-hinted, but otherwise not specially optimized python code? I'm especially interested in what kind of performance speed-ups could be achieved.
- linsomniac 4y agoglyph's article goes into this case and finds a 25% speed improvement in that case.
- hauntsaninja 4y agomypy compiles itself with mypyc, for about a 3.5-4x speed up. black very recently started shipping mypyc compiled binaries, for about a 2x speed up (iirc).
- byteface 4y agoAfter reading the article last night. I spent the whole evening about 5 hours getting one of my DOM libraries to compile with mypyc. It's a hacky codebase with little to no type hinting. I had to rewrite a fair abit to appease mypy and in most cases just used 'Any'. It's still got runtime issues and is buggy but I got at least a 2x speed increase on rendering a single node... https://github.com/byteface/htmlx/blob/mypyc/benchmarks.md https://github.com/byteface/htmlx/blob/mypyc/benchmarks.md However I was unable to compile from my mac, could only compile using linux. but that could be as I'm using older version of dev tools.
- 8organicbits 4y agoI sort of wish the precomputed template string was just a hard coded string literal. There's a lot of code dedicated to that, and you only need to understand the output it computes. Other than that, I think the python code is quite readable and understandable with the context provided in the post. Very well written post!
- whoevercares 4y agoI wish I can compile pandas
- mynameisash 4y agoTry looking into polars as a much faster pandas alternative.
- BiteCode_dev 4y agoIf you compile Python, you may also try nuitka, which can not only compile a standalone program, but also compile a single package.
- teaearlgraycold 4y agoHonestly for a language that’s supposed to be simple and easy to use Python seems really complicated and hard to understand. Anything big or performance critical should probably not be written in Python - and yet people are doing it. And then we end up with a house of cards with this project built on top of the very shaky foundation of mypy.
- teaearlgrey-hot 4y agoI think Python is simple to use but difficult to scale. It can be useful to think of programming languages as tools. Assembly is the workbench where tools are made and raw materials are handled. C++ is like your standard set of machining tools. Java is like a bag of tools, hammers, handsaws. Python is like an electric nail gun, or perhaps a gas chainsaw. It does many of the things the other languages say that you can do directly out of the box and often in a manner that’s more sensible to people initially using a tool. But when it comes to building a house and all you have is a chainsaw? You’re gonna have some very cool log cabins, and if we go outside the analogy for a moment: code that uses tricks to accomplish what others would do with better building, architecture, or execution. Now that said, how many people do you know who might go off and build a log cabin vs. a modern home on their own?
- mumblemumble 4y agoIt's because Python is both simple and easy to use, and complicated and hard to understand. It's a Janus of a programming language. There are lots of complexities that alternately tie software engineers up in knots and let them go into raptures of blissful hackery. But they generally aren't all that visible in the face it presents to data scientists and devops. And it's hard to make sweeping statements about "big or performance critical". A couple times in the past few years years I've seen or been involved in projects that successfully replaced big Java applications with Python implementations that were 1% the SLOC and had better performance. They were special situations, to be sure, though more so, I think, in terms of explaining the performance improvement than the cost savings.
- orangetuba 4y agoThis is absolutely incredible. Not only is the project amazing, but glyph has a way of communicating technical stuff that is on another level.
- mihaic 4y agoIt really is a shame that "it's fast enough" is repeated without saying what for. It really is fast enough for scripting logic, that so many other aspects would benefit imensly if more effort were put into speeding up the standard interpreter. I find Python to be a great language to describe business logic, and honestly it should end in 10 years to be within at most 2x slower than Javascript.
- pella 4y agoother alternative : PYSTON "Pyston is a faster and highly-compatible implementation of the Python programming language." "Our speedup on ARM (30% on a Graviton EC2 instance) is comparable to our speedup for x86 (34% on an Intel i7-6700)." https://blog.pyston.org/2022/04/01/pyston-v2-3-3-arm-support/ https://blog.pyston.org/2022/04/01/pyston-v2-3-3-arm-support... 2021.aug.30 : "Pyston Team Joins Anaconda to Expand Open-Source Project Development" https://www.anaconda.com/blog/pyston-team-joins-anaconda https://www.anaconda.com/blog/pyston-team-joins-anaconda Roadmap: https://blog.pyston.org/2021/10/26/pyston-roadmap/ https://blog.pyston.org/2021/10/26/pyston-roadmap/ Github: https://github.com/pyston/pyston https://github.com/pyston/pyston
- sli 4y agoAt this point, I think maybe Python ceases to be the correct tool for the purpose. There comes a point where trying to squeeze every ounce of performance out of something that is, relatively speaking, not focusing on raw performance is just stubbornness. Write the slow part in C/C++/Rust/Zig/Nim or something and be done with it instead of making your Python unreadable. At least your native code can be idiomatic instead of being filled with arcane tricks.