Get a feel for what your computer can do

When examining performance with an eye to optimization, it's worth developing a feel for how fast typical software operations actually are on modern hardware. That sense will help you know when to look deeper for the real cause of a bottleneck, or when to look elsewhere, or when to mistrust the results that your profiling tools are giving you.

For instance, I ran the following short bit of code:

TreeSet tree = new TreeSet();

int found = 0;

long time = System.currentTimeMillis();

System.out.println("add #'s to tree");

for (int i = 0; i <>

long time2 = System.currentTimeMillis();

System.out.println("elapsed:  " + (time2 - time));

System.out.println("find #'s in tree");

for (int i = 0; i <>

    if (tree.contains(Math.random())) found++;

long time3 = System.currentTimeMillis();

System.out.println("elapsed:  " + (time3 - time2));


Here's the output:

add #'s to tree

elapsed: 8240

find #'s in tree

elapsed: 5476


We see that filling a Java TreeSet with 1M random Doubles (which involves generating the random numbers, creating the Double objects, creating the TreeSet nodes for the objects, finding the correct insertion point in the tree for each object and occasionally rebalancing the tree – not to mention all the memory allocations) took about 8 seconds on my laptop, meaning an average of 8 microseconds for each iteration of the loop.


Generating a second set of 1M random Doubles and searching among the 1M in the tree for each of them took about 5.5 seconds, or 5.5 microseconds per iteration.


In short, to make a modern computer break a sweat you have to either be doing a huge number of operations, or operations of huge complexity or – more likely – both and/or waiting for slow system events like network or disk access.

Comments

Popular posts from this blog

Profiling Java @ 2019

Shared/Pro/Managed Hosting for your site - which to choose ? (NO AFFILIATE LINKS!)

ZVR converter