Performance Versus Readability

When I code, I try to solve the lesson on my own so I can learn. Once I get the code working, I will optimize it, then I will ask A.I. to optimize it. Most of the time A.I. does a better job then I by shaving 4-5 milliseconds. Then I will ask A.I. to generate the code without looking at mine. A.I. does a good job creating optimized code. Problem is readability of A.I. generated code. I will also look at community solutions, especially fewest lines. Issue with highly optimized code is readability. If I have to go back 1 to 2 years later and update my code, or worse, somebody else has to do it. At what point is optimized code to optimized?

In general, I recommend optimizing code based on performance needs. If the program isn’t taking too long to run and isn’t expensive to run, it doesn’t need optimizing.

1 Like

I generally optimize for the performance of understanding, and so, readability, first, until the need to optimize for something else, which sometimes forces somewhat less readability.

All of these things are “optimizations” and without identifying what is wanted to be optimize for there is no way to understand what is being talked about.

  1. readability
  2. time
  3. space
  4. and others

And the reason for readability is exactly because I am likely not the one to come back and have to make improvements, and I really do not want to spend an extra amount of time to document what should have been readable and understandable in the first place (by someone other than me, including my future self.)

2 Likes

I sometimes optimize my code for “teaching” as the most relevant optimization. That means, using a very idiomatic way to solve the problem within the constraints given. This is not generally the most readable solution, but often the most helpful for others to get better in the language.

This also often enables compilers / runtimes to execute faster / more efficient.