Question about -Dscala.time

Hello,

Why the result below shows the scala script is even faster than the complied program?

$ scala -Dscala.time Hello nice buddy world
hello nice buddy world
[total 2ms]
$ scala -Dscala.time hello.scala nice buddy world
hello nice buddy world
[total 1ms]

$ scala -Dscala.time Hello nice buddy world
hello nice buddy world
[total 2ms]
$ scala -Dscala.time hello.scala nice buddy world
hello nice buddy world
[total 1ms]

(I have run above comparison twice, the results are the same)
The hello.scala is quite simple:

object Hello extends App {
  if (args.length > 0) 
    println(s"hello ${args.mkString(" ")}")
  else
    println("hello word")
}

Is “scala -Dscala.time” run differently with the system command “time”?

Thank you.

My guess is that classes are already loaded after compiling the script, whereas running the object starts cold.

The timing is just between loading the App class and after running the code. It uses currentTimeMillis instead of nanoTime.

My first guess was that the script compiler compiles to an in-memory file like the REPL does, but no. It would have to be a fast compiler and a slow disk.

As another caution, scanning the classpath can be expensive, for example if there is a large directory of class files.