Code design: performance vs maintainability [closed]

心不动则不痛 提交于 2019-12-05 17:24:25

Approach 1 has the potentical to be much faster and uses less space.

Especially for a byte code instrumenter, I would first implement approach 1.
And then when it works, replace both Lists with non generic lists that use primitive types instead of the Integer and Double object.

Note that an int needs 4 bytes while an Integer (Object) need 16 - 20 bytes, depending on the machine (16 at PC, 20 at android).

The List can be replaced with GrowingIntArray (I have found that in an statistic package of Apache if I remeber correctly) which uses primitive ints. (Or maybe just replaced by an int[] once you know that the content cannot change anymore) Then you just write your own GrowingDoubleArray (or use double[])

Remember Collections are handy but slower.
Objects use 4 times more space than primitives.

A byte code instrumenter needs performance, it is not a software that is run once a week.

Finally I would not replace that Maps with non generic ones, that seems for me to much work. But you may try it as last step.

As a final optimization step: look how many elements are in your lists or maps. If that are usually less than 16 (you have to try that out), you may switch to a linear search, which is the fastest, for a very low number of elements. You even can make your code intelligent to switch the search algorithms once the number of elements exceed a specific number. (Sun/Oracle java does this, and Apple/ios, to) in some of their Collections. However this last step will make you code much more complex.

Space as an exmample:
DecisionNode: 16 for the class + 4 (id) + 20 (Double) +4 (boolean) = 44 + 4 padding to then next multiple of 8 = 48 bytes.

In general you should always go for maintenance, and not for supposed performance. There are few good reasons for this:

  • We tend to be fascinated by speed difference between array vs HashMap, but in real enterprise application these differences are not big enough to account in visible difference in application speed.
  • Most common bottlenecks in application are in either database or network.
  • JVM optimizes code to some extent

It is very unlikely that your application will have performance issues due to maintainable code. More likely case is your boss will run out of money when you will have millions lines of unmaintainable code .

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!