Search code examples
javaalgorithmruntimejvm-hotspot

Java task runtime


First of all I have to admit that these are very basic and primitive questions... I want to demonstrate different Algorithms in Java for sorting and searching, and to get a value for the runtime. There're issues I cannot solve:

  1. there's Hotspot compiling - which is a runtime optimization I need to deactivate (I guess).

  2. How do I get time-values (seconds) for runtimes? Starting a timer before the execution and stopping it afterwards... seems a little primitive. And the timer-object itself consumes runtime... I need to avoid that.

Is there anything in the Java API one could utilize to solve these problems?

Thanks, Klaus


Solution

    1. Use -Xint JVM flag. Other options can be seen here.

    2. Use the ThreadMXBean API to get CPU/User times for your thread. An example can be seen here.