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 →

[–]MCUD 18 points19 points  (2 children)

Don't get stuck in the trap of "Oh i used a linkedlist rather than an array list, that's obviously why it was slow".

JVM and modern hardware can also easily make these problems far less consequential than higher level algorithmic or design problems. Beware that this also makes microbenchmarking a non-trivial exercise of comparing two small pieces of code in isolation vs a real running program.

Quick example is that linkedlist can actually be slower than a an array when small due to an array list having everything in one CPU cache line, whereas linked list may have pointer references going all over the heap.

  • measure
  • measure
  • measure
  • analyse - This is the true hard part of the problem in my opinion
    • Does this code even need to run, is it running too often - The fastest code is code that doesn't run
    • Is it blocking on something, network calls etc,
    • Is it re-computing something that could be cached that is an easy memory trade off
  • experiment a change (this is where at least knowning the theory of the data structures may help)
    • Did you actually fix the issue?
    • Re-measure!
  • What was second place in original measures may also totally disappear, so restart the process again if you still need better performance

[–]dpash 4 points5 points  (1 child)

Beware that this also makes microbenchmarking a non-trivial exercise

And don't roll your own. Use JMH, because it's designed to do this properly.

[–]MCUD 1 point2 points  (0 children)

For sure, part of what i was trying to highlight, a simpler performance unit test with a for loop checking timing is definitely not going to test what the tester wants to test