4 ms·
It's even slower than java compiler and jit combined! Java has never been know to be fast for command line programs. My M1 MacBook: 1.716s for min.py AND 0.29
by silvestrov 2y ago
It's even slower than java compiler and jit combined! Java has never been know to be fast for command line programs.
My M1 MacBook: 1.716s for min.py AND 0.295s for javac and 0.067s for running the compiled code.
It is a straightforward conversion to java using long for i and r.
- mrguyorama 2y agoI once was using a bunch of handmade python scripts to work with terrain heightmaps and ground terrain textures in an attempt to improve FSX visuals before flight sim 2020 was announced. I had 12Kx12K pixel images of the ground in spring, and wanted to make some winter versions for fun. Some basic toying around showed you could reasonably resample a "snow" texture into these photos wherever you had pixels that were "mostly green", ie grass or trees. I wrote this up in python using the simple PIL python library for working with image data. It took 45 minutes per image. I fired up a text editor and within 30 minutes had a java version of the code, most of that time was spent remembering how to write java. 30 seconds per image. I don't care what nonsensical "optimizations" I could have done, like oh just pull it into numpy arrays and work there, oh PIL is slow for pixel manipulation so don't do that, oh you could rewrite it like X and it will be faster I don't care. The simple, naive, amateurish attempt in Java, a language I hadn't touched in 6 years, blew the pants off of the python version, a language I used at my day job. It took zero more effort to write in java than in python. Python is great for doing simple one off scripts, but it is terrible at actual work. Add to that, the tooling around python is mediocre, and for some reason python programmers don't seem to care, while the tooling around java is so good IDEs basically write all the supposedly awful boilerplate code for you. Stuff that I had to do by hand in python was automated in java.
- glass-z13 2y agoI had a similar experience when i was in highschool and learning python. I wrote a image->ascii image convertor using PIL as you did, one image would take a few hundred ms and it was fine, but as i played with it and added more features/learned more i tried to do it on video frames extracted with ffmpeg and it would take atleast 4x the duration of the video or even more to convert it in ascii video. Few months ago i coded it in go since i got back into programming and it was significantly faster(The overall code was also smaller but maybe that's just because i have a bit more experience now) even tho in both cases the implementations was the most naive one i could come up with, without any optimizations. Edit: I went ahead and tested both, took me 30 mintues to set them with identic parameters on 3602 frames(roughly 1 minute video) Keep in mind that this is just a naive implementation with 2 nested loops itereting in a step of 12 over the pixels of the image, no fancy math or numpy used in the python version. Image for reference[0]. Also keep in mind that the step and length of the ascii charmap used affects performance but here i used a simple one "_^:-=fg#%@" so it's a bit easier for the python code, here are the results on my i7 9th gen laptop 1 Minute video 720p 60fps GO - Execution time: 359647 ms, 5.9 minutes ("asciifying the frames") + 30~ seconds ffmpeg video conversion Python 3.12.3 - Execution time: 0:16:25.002945, minus ~25-30 seconds ffmpeg [0]: https://i.postimg.cc/CK30z1jt/frame-000039.png https://i.postimg.cc/CK30z1jt/frame-000039.png [1]: Video using a more detailed ascii charmap(" .,:;i1tfgLCG08@#") in the go version -> https://streamable.com/7h8jc3 https://streamable.com/7h8jc3
- rbanffy 2y agoA lot of these notes follow a similar pattern: we start with Python, get a working program, and then rewrite it in X with a huge performance gain. That’s fair. We use the easy language to get a correct program, then we use the “hard” language to get a fast program, now that we know what a correct one looks like. This is the key here. If you try to start with the lower level language, you might spend a much longer time getting to the correct program. It’s much easier to optimise a program that’s working than to debug one that isn’t.
- pjmlp 2y agoOr just use something that offers both from the start. One day, Python will finally catch up with 1990's Smalltalk, SELF, Lisp, Dylan and Scheme.
- glass-z13 2y agoWhat would you recommend that gives both worlds? You can learn most of the syntax of python/go to a usable level in about a day.
- pjmlp 2y agoGo I agree, as for Python using it on and off since version 1.6, that would be a very very basic knowledge after one day. Since you mention Go, placing dynamic and static on the same basket. Common Lisp, Scheme, Raket, Clojure, Scala, Kotlin, F#, OCaml, Pharo, JavaScript/Typescript. There are other candidates, only listing the ones with good out of the box REPL experience, JIT/AOT toolchains in the reference implementation, and not going crazy with their type systems for strong typing.
- tejinderss 2y agoOne of the strengths python has is its ecosystem. All the languages you listed don’t come close to python’s ecosystem except perhaps TypeScript/nodeJS (whose quality of ecosystem is questionable although improving in recent times).