Recently I was in a discussion with a coworker on the benefits of using Utility classes over putting common functions in an Abstract class. These classes usually contain static methods that perform common functions. The major argument I gave was it allowed me to add functions later on without being concerned about all the classes that inherited from it. The vast amount of my coding style is a functional/Object oriented hybrid. Objects classes which call Utility classes. After that discussion I wondered what was the performance implications. So I wrote a Calculator app using both approaches and ran each 500,000 times to get an average performance. Here is what I found:
- Classes that inherit code from Abstract classes are around 30% slower to initialize.
- Classes that inherit code from Abstract classes are around 4% slower in execution of all their inherited methods versus calling all the methods from a Utility class.
- Classes that inherit code from Abstract classes are around 1.3% larger in compiled size.
- After examining the heap dump I found memory usage is indistinguishable between the two.
If you have any thought or advice, please leave them in the comments below.