So I was trying to measure the time two different algorithm implementations took to accomplish a given task, and here is the result:
i alg1 alg2
4 0.00
You should consider running your algorithms many times and averaging the result.
There are three reasons for this:
It makes timing easier, for the reasons you have identified.
The JVM "warms up" and the performance of your code will change as it does: The hotspot JVM won't compile fully until you've run a method a large number of times. If you don't get past this your results won't be representative.
It's always a good idea to average out the times, to avoid spurious effects due to external events like GC, or other stuff running on your computer.
As a rule of thumb, try running your algos 10,000 times as a warm up, then 10,000 times afterwards. Modify the numbers to suit your runtimes...
Here's an article explaining the same thing.