Measuring time in Java

前端 未结 4 1199
粉色の甜心
粉色の甜心 2021-01-03 01:17

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         


        
4条回答
  •  青春惊慌失措
    2021-01-03 01:32

    You should consider running your algorithms many times and averaging the result.

    There are three reasons for this:

    1. It makes timing easier, for the reasons you have identified.

    2. 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.

    3. 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.

提交回复
热议问题