python is too slow...
Вставка
- Опубліковано 10 лют 2025
- ... for a text editor? I talk about the architecture of babi and how I made it "fast enough" and ultimately that "sometimes it doesn't matter if it's slow"
playlist: • anthony explains
==========
twitch: / anthonywritescode
dicsord: / discord
twitter: / codewithanthony
github: github.com/aso...
stream github: github.com/ant...
I won't ask for subscriptions / likes / comments in videos but it really helps the channel. If you have any suggestions or things you'd like to see please comment below!
Lemme open up my text editor written in javascript that imports a browser to show you how it's done
true. I should have timed `code babi/screen.py` as a fair comparison 😂
I would love a video tutorial on how to create these plots/flowcharts to investigate which part of your code takes what amount of time.
"Sometimes it is okay to be slow" is a wonderful affirmation
My father researched psychology with respect to how users (specifically, a POS terminal operator) responds to user interfaces. Users actually have an easier time cognitively switching between screens (and thereby an easier time switching tasks) in the software when the UI goes blank for at least a perceivable (but still very small) amount of time. If the program switches screens too quickly, the user's brain is 'left behind' in the previous screen context and so they have a harder time switching tasks quickly. So, he programmed his POS software to intentionally have a short delay when switching screens. This actually improves user productivity. Or at least that's what some studies would suggest.
nice
what made the terminal operator a piece of shit?
I was a little obsessed with HCI for a bit and this is actually just a misconceived rumor. The broader research suggests that intentionally inducing wait times or hiding information which would otherwise be instantaneous generally harms user experience rather than helping it. Nielsen (1993) found that users expect near-instant feedback-around 0.1 seconds feels instantaneous, while delays around 1 second disrupt flow and cause frustration. Studies like Rogers and Monsell (1995) and Monsell (2003) do show that visual cues for state changes is important, but they also show that it's only for longer running tasks that would already be in the second to multi-second range; anything less than that and it's been found to be jarring and/or unnecessary
Out of topic, but this moment when you realise your Python boy crush is actually the originator of pre-commit, mind blown...
Great content, as always! :-)
Most people when editing videos would cut part when Anthony is trying to figure out what’s wrong with identify module, but it is so nice to see that sometimes things mess up even if you are really experienced
Thanks for your videos, learned a lot from them, to some extend thank you that made me Python-ish enough to get my first job 4 years ago
heh yeah I tend to not edit unless I'm doing something that's going to get me in trouble. plus it was late at night and after a beer and I decided wevs
The -S mixup is sending me
but is it -sending you or is it -Sending you??
Python is my go to language. I was learning some JS/TS in December and decided to do some Advent of Code.
One problem in particular I decided to code in Python and noticed it was twice as slow (32s compared to 17s).
I jumped on IRC and asked in #python what V8 could be doing that makes it twice as fast. They mentioned it could be JIT related and asked that I try it in pypy.
To my amazement pypy was able to run it in just 2.5 seconds w/ the same code.
CPython: 32
Deno (V8): 17
PyPy: 2.5
Crazy stuff
pypy's jit is pretty great -- especially for advent of code where it's usually running an extremely homogeneous workload
Great video Anthony! I love watching to an Anthony writes code video during breakfast (:
I love the topic of perceived performance
To mention, if your client is on a 240hz monitor you have 4ms to complete the action without introducing delay.
that's only true if you can perceive 240hz
@@anthonywritescode and everyone knows you can't perceive more than 30hz
@@anthonywritescode so like everyone?
@@anthonywritescode Yes, you can perceive it, but we start to dig more into 'feeling' than 'seeing'
Your SSD is the bottleneck
Hi Anthony. Sorry, this is a bit off topic, but could you please share your keyboard / trackball setup? I've been looking for a good split keyboard for some time. I've tried a few but still haven't found one that I'm happy with. Thanks!
I second this, curious to hear how your setup has worked for you Anthony.
there is treesitter syntax highlighter quite fast. A lot of editors use it now days. Have you considerate moving to it?
I ran into an issue recently where requests was proving to be slow (being on sometimes flakey mobile/public wifi didn't help) but downloads went twice as fast using curl instead and it was simpler to call the curl binary than to use a different library for the large downloads
Good video, but I will love something more in detail about maybe what is somethings that you should avoid when writing python (because slowness), how to measure execution times, and what things are fast in python
1:35 The real slowness was maintaining the package dependencies we made along the way
anthony you are so cool, thanks for all your amazing work!
I think contexts matters a lot. Yes, python is slow. Yes, there are fast options out there. Yes, I am currently using python to load test and collect data from golang application running in k8s. I think the use case matters a lot.
@@jamesfitzpatrick9607 it is extremely hard to.justify a single use case for Python. Its "simplicity" is a myth. It is a low level language, so it does not really imporve the development time and costs. It is just popular, because worse is better.
I am going to check out your editor. I had no idea.
Now... let's discuss the networking stack for a moment :p
Modules screwed up in the first minute. That happens to me every time I try to use Python for anything. Been happening for years with no sign of improvement. So much so that people have given me Python code in Docker containers recently. One of the many reasons for my never wanting to use Python.
I definitely need to hear more about that type hint! WTF
You should certainly rewrite it in C
C deez
@anthonywritescode that's my boi
Why not in rust?
@whythosenames Because Rust isn't cool anymore - it's Zig, or even Odin nowadays.
Rust was never cool
I used Qtile for years, tiling window manager written and configured in Python. It always felt snappy... well until there was a huge mouse problem with high polling rates. I think its fixed now, but that counts as a bug. Also there are lot of tools written in Python, like yt-dlp where the slow parts of Python just doesn't matter.
Then there are optimized libraries that use C in the backend. Its a bit cheating, but basically every language cheats by using C. And there is Pypy too.
Hey Anthony, do you recommend using the future annotations import in every file? Was curious whether the performance issue in its absence was still a significant thing in more recent versions of python
I use it in every file. there's some certain libraries that I avoid that it doesn't play nicely with but I avoid them
nice start of the video, "WTF is wrong" lmao
For 95% of my "python is slow"-issues, numba is the perfect solution.
that's not really the point of the video. also numba is usually hilariously overkill and is a nightmare from a packaging compatibility perspective
@ My usecase is usually number crunching and packaging was no issue so far fortunately. But yeah, I totally understand it might not be a good a solution for other use cases.
It would be interesting to see benchmarks of different python 'flavors' on example of babi - CPython, PyPy and Codon :)
easy to just try it
I’m not a huge fan of python but it’s 💯 % true.
One note though, until it starts to matter. Unfortunately in a lot of cases the only thing that is left to do is a rewrite.
I make the face at 2:06 quite a few times a day. Love to see it's not just me!
i don't think `neovim` is any faster than `babi` startup once you start adding all the dependencies and plugins.
2:40 "startup time of an application is pretty important, but for a text editor? i don't know if it's that important"
ah, so it's safe to guess you don't use VS Code very often?
Yeah, but how often do you restart your IDE? Once a day? Once a week? Sure, faster is never wrong, but I'd agree, not such a big deal compared to other criteria.
@@hansdietrich1496 Depends on language, some people need to restart their editor often, because LSP is written so bad (ekhm typescript one ekhm) that without restart you will have more trouble
actually that's part of the reason I don't use vs code
‘:comment +’ ❤
Did you have prior knowledge of how tect editors worked or you just figured out how things worked along the way as you were building it out?
I just made it up as I went along for the most part
have you explored treesitter based highlighting?
unfortunately I started the project mere months before treesitter existed slash was popular so I haven't
Hi Anthony - would you have any tips/best practices when using a vendor api to do crud operations? How would you build out a project for something like this and say maybe make a cli tool?
record requests and responses and write a bunch of tests around those. periodically refresh the recorded responses to make sure they're reasonable. have monitoring around success rate of interactions. and beyond that Ts and Ps :)
@ :) thanks for responding!
Hi, if you optimized some cpu intensive part do you have some general tips like the one about typing or you generally figure out optimization based on which part is slow in the profiler?
general tips are sort of useless (even the type annotation one: it was only load bearing because it was in a tight loop but otherwise wouldn't have been impactful at all). profile it to find what's slow and optimize from there
People that say python is slow are the same people who buy 240 Hz monitors and an expensive laser mouse so they can lose at Starcraft because they're
99% of python use cases, not 99% of software use cases. python use cases are definitionally the subset that tolerate extreme slowness
In 99% of my use cases Python itself won't even fit onto a device I'm writing code for.
@@vitalyl1327 Congrats, you are the 1%
@tylermfdurden in the era of the cloud and costs per CPU time, 100% must care about their code perfotmance. Those who still use Python are delusional.
@@vitalyl1327 you definitely spend your time wisely.
Are you sure that those import list runs at every key press? Are you evaling a py file? Otherwise, a key press should run a callable which was already imported once by the interpreter. Or I forgot python because I dropped it at v2.7~3.1
Its not imported on every key press, the page is RENDERED (probably with curses? @anthonywritescode) on every key press. In games development, the canvas is rendered 60 times per second. So, that is pretty normal.
ah you're misinterpreting. it has to *highlight and render* all of that on every execution.
Got it thank you for the heads up 👌
Just run in mojo environment
If you shift nano over by one you get bBI
with two hands!
Anthony, are you aware of the fun-fact that "babi" happens to mean "pig" in Indonesian? Or was that somehow intentional?!
it's just a typo of "nano"
I actually explain the name at the beginning. unfortunately I'm not fluent in every language so the overlap in others is just going to be inconvenient
I guess that's all well and good if your goal is just to write working software to address actual real-world use cases, but isn't the REAL purpose of programming to feel superior? That's why I only write assembly, Rust, and Brainfuck
Hello and welcome to another video... ? 😩
hello hello!
love it
It’s funny cause yeah it’s slow and that’s ok!
What are the specs on your computer?
am too fast tho
Lol... most websites perform way worse than Babi. :D
1:30 is pythons true biggest fault.
what? running your code with disabled importing of site module?
@@yakimka5620 really man? you really have no problem with what just happened there?
the -S flag worked exactly as intended "don't load site". it was entirely operator error here on an exceedingly rare flag while recording late at night after a beer and a very long day
@@anthonywritescodefor sure, I am just saying python has so many foot guns around dependencies around imports. Great video btw, definitely didn't know type annotations would impact performance.
I don't really agree (and the thing in the video definitely isn't one people would ever run into unless they're me). the only one I can really think of is if you run a script vs a module but ez don't run as a script. and I guess cycles but also just don't have cycles
doesn't matter unless you want to not spend long hours optimizing and still be too slow to go full IDE
I think you missed the entire point. the structure was set up specifically to be simple and not worry about micro optimizing because it's fast enough
Uhhhh…. Vim
telling on yourself
To make Python fast is very easy: compile your Python using Codon.
10-200x speedup is that easy.
Codon deez nuts
I/O is slower.
I/O is going to be about the same in every language
@@anthonywritescode Sorry, I meant that I/O is slower than Python and many applications spend the bulk of the time in I/O. Python works!