-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Welcome to the fscala wiki!
See this blog entry and http://www.scala-lang.org/node/166 as examples of how and why to use Scala as a scripting language.
But the latency to start a Scala script is really high. The helloworld script (see examples/oldscala.scala
takes as much as 8.5 seconds to run on a 2.66GHz Duo Core Linux box. fscala
tries to eliminate unnecessary steps in running the scripts to reduce start time.
To use fscala, put it in your PATH, and make sure it is executable. Then in the first line of your scala script, write #!/usr/bin/env fscala
, and make it executable too. That's it! You do not need !#
to define the end of the script header. In fact if you have it then it won't compile. If you don't like to change your first line of the script, you can also run fscala your_script.scala
.
There are two variables implicitly available in your script: argv
and args
. args
is similar to Java's default args behavior. argv
contains one extra element at the head which is the path to the script itself, i.e. $0 $1 ... $n
. This is closer to the argv in other scripting language, e.g. Python. You can ignore them and redefine to whatever you like. See examples/hello3.scala
as an example.
What fscala does is very simple. Suppose your script is named hello.scala. fscala will wrap hello.scala into an object called ScriptMain
, write the new source file into .hello.classes/hello.scala
. Then it calls fsc
to compile the file. If the compiled class file is newer than hello.scala, compilation is skipped. Finally it calls java
with the correct classpath to launch your script. fscala is faster than the -savecompiled option with scala for around 0.1-0.3 seconds.
Here we compare the performance of a very simple hello world script in both fscala and scala with the -savecompiled
option. The two test scripts hello1.scala
and oldscala.scala
can be found in examples/
.
$ touch hello1.scala && time ./hello1.scala
Hello, world, from a script! List()
real 0m0.864s
user 0m0.610s
sys 0m0.090s
$ touch oldscala.scala && time ./oldscala.scala
Hello world, from a script! List()
real 0m1.371s
user 0m0.710s
sys 0m0.040s
$ time ./hello1.scala
Hello, world, from a script! List()
real 0m0.217s
user 0m0.270s
sys 0m0.020s
$ time ./oldscala.scala
Hello world, from a script! List()
real 0m0.545s
user 0m0.600s
sys 0m0.060s