0

I am learning scala, and when testing small codes I saw the delays were wrong.
For example, just doing

val start = System.currentTimeMillis() 
println((System.currentTimeMillis()-start)+"ms")

prints 25ms.

Futhermore, create a instance of a simple class takes way too long (creating a playing card with a number and type in parameter takes 15 ms the first time, then less than 1 ms the other times).

Is there something that I missed?

Thanks!

slouc
  • 9,508
  • 3
  • 16
  • 41
  • you need to "warm up" before benchmarking http://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java – Giovanni Caporaletti Mar 31 '16 at 16:21
  • Probably you should read about JIT. – vvg Mar 31 '16 at 16:21
  • Maybe, you need a better computer? Your code prints out "0ms" on my laptop. Replacing `currentTimeMillis` with `nanoTime` shows that it actually takes about 0.4 millisecond. – Dima Mar 31 '16 at 17:03
  • 1
    The resolution of `System.currentTimeMillis()` is not very high on some operating systems, you can't use it for accurately timing very short time intervals. – Jesper Mar 31 '16 at 17:27
  • 2
    If you are running in the REPL, by default your code runs as an object initializer, which is a class initializer, which isn't jitted. To echo the other comments, there's lots you are probably missing. – som-snytt Mar 31 '16 at 17:38
  • Possible duplicate of [Technique or utility to minimize Java "warm-up" time?](http://stackoverflow.com/questions/1481853/technique-or-utility-to-minimize-java-warm-up-time) – raam86 Mar 31 '16 at 20:25
  • "You must wait 48 hours to accept your own answer." "You can't accept comment as solved" Someone proposed me, as everyone was posting as comment and not answer, to put my own answer and accept it. – joel lupien Apr 01 '16 at 21:09

0 Answers0