Monday, December 19, 2022

Thoughts on Optimization of Code

Donald E. Knuth famously said "Premature optimization is the root of all evil." The important word there is “premature.” Optimization is not a bad thing. It isn’t always required though.  The joke is that a programmer will spend hours coding a solution to a problem that can be solved in minutes manually. Is that a bad thing? It really depends on the circumstances. If one is getting paid by the hour one could easily argue that it is wrong. On the other hand if time is not really the issue and writing the code is fun – why not?

There have been times in my career when optimization has been very important. I helped spec a computer for high speed/bandwidth data collection. It was critical that the computer be able to take the data and place it in memory without losing data. We actually analyzed the speed of various machine language instructions for optimization purposes. That’s pretty exceptional of course.

I also remember writing a program of my own that took what seemed like a long time to process some data. I thought about trying to optimize it but as often as the program was to run it would not have been a good use of my time. Before I ran it again I got a new computer which made a bigger difference in execution time than I would probably have been able to get though “fixing” the code.

Old habits from the days of slower computers stick with me though and I tend to think a lot about code execution. Recently I had ChatGPT write some code for me (Coding with ChatGPT–Armstrong Numbers) I looked at the code very closely since I was trying to come to some conclusions about how good the code was. I noticed that the C# version it gave me was highly dependent on modulus and division which are somewhat time consuming compared to addition and multiplication. The program I write didn’t use any modulus or division but a lot of multiplication and addition. Both used the same number of raising to a power of 3 so it doesn’t play an important role in performance differences.

Having some time on my hands, I added some timing code to see if there was a difference. My program turns on to be much faster. Yea me. Of course both execution times are very small fractions of a second. Does it make sense to optimize this code? Probably not in this case. In fact, I would argue that the most understandable code may be a better goal for this project. That is a very subjective goal. Different people might come to honest but different conclusions.

Another goal might be scalability – the ease of adapting the program for other Narcissistic numbers. The ChatGPT probably wins there.

SO much depends on what we want to optimize:

  • Speed of execution
  • Ease of understanding
  • Ease of enhancement/modification
  • Possible other considerations(?)

Getting all three may not always be possible. It’s an interesting question though and one developers do need to ask.

2 comments:

Doug said...

Great post, Alfred.

I had to smile a bit at your recommendations at the bottom. They make sense if you are the only one doing the coding. Could you get a team to agree on the elements of all four?

Alfred C Thompson II said...

A team that agrees on the goals and priorities of the project could come to agreement on optimization goals. It requires good communication among the team members.