r/AskProgramming • u/Rscc10 • Sep 10 '26
Python How much readability should I trade for efficiency?
I'm developing a chess engine in python right now, just a hobby project. At first I began programming it any way I knew how to, and along the way I would learn optimization tricks which I would then apply when refactoring. I'm now realizing I'm changing a lot of my simple readable code into a bunch of optimized bitwise operations and just different logic structures in general.
I keep optimizing because I'm not satisfied with the speed of my chess engine and I usually rewrite entire functions or change the logic here and there just to save on another few milliseconds of thinking time for the engine. At one point, I rewrote a whole function just for it to be 0.1 or so seconds faster, at the cost of the simplicity of the code of course. This was kinda when I took a step back to think if it's even worth it.
I'm just wondering for future projects in general, are there any pointers to know when readability and simplicity of the code just isn't worth trading? Cause for this project which is only 1-2k lines and only in 3 files, maintenance and debugging has been bearable but I'm sure the same can't be said if I did the same thing in larger projects. Any tips and experiences shared will be largely appreciated.