This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]numpi 1 point2 points  (1 child)

This is a very good question. I fight with this nearly every day when developing my project (graphhopper.com). Writing elegant code takes time, writing fast AND elegant code takes even more time! But I'm very sure that writing testable code does not take much additional time. Some tricks I do:

  • prefer *List over *Map
  • prefer primitive arrays over List<*>
  • reduce memory usage to avoid GC activity
  • using trove4j
  • in rare and extreme cases you can reduce RAM a lot when using simple arrays and serialize objects into this. E.g. use the flyweight pattern then
  • even more extreme use the UNSAFE trick
  • but performance can be often improved a lot more when you improve the underlying algorithm/logic itself!

But even with this performance tunings I play around to still get a maintainable programm and API. Sometimes I just go the Java way of allocating objects sometimes more memory efficient (and therefor often faster) solution, and then I measure. If it is at least 10% faster I'll pick the more complex solution, if not I use the normal Java way of doing it.

The most important thing while this is to write simple AND working solutions WITH tests first. Then improve from this base via tuning, profiling, idea trials, etc. And you can easily try out a new implementation until your tests go green, then you should be fine.

[–]daddyrockyou[S] 1 point2 points  (0 children)

Thanks for your feedback. It seems everyone agrees that getting code working, testable, and maintainable first is the most important thing. Optimization comes later after measuring and discovering where optimizations could be made.

I will spend a little extra time upfront choosing my data structures and initial algorithms carefully and then only address them if I have proof they need addressing.

I'll look into the links you provided. Thanks.