I would like to know the best (fastest) scripting language for use in Java. I don't much care if it takes long to load (as long as it is a one time load), as opposed to how fast it runs. I am currently using Jython (python) but I would like something faster.
-
2as a rule of thumb (fast to write) != (fast to execute)AK_– AK_2011-04-11 23:00:45 +00:00Commented Apr 11, 2011 at 23:00
-
as another rule of thumb, efficiency of your algorithm\program is a lot more important then the language\ environment\ interpreter speedAK_– AK_2011-04-11 23:03:51 +00:00Commented Apr 11, 2011 at 23:03
-
2@Hellfrost "I'm sorry I wrote such a long letter, but I didn't have time to write a short one." - Blaise Pascal... If only he knew how much that would apply to our profession!corsiKa– corsiKa2011-04-11 23:06:03 +00:00Commented Apr 11, 2011 at 23:06
-
@corsiKa and to the language named after him!Thorbjørn Ravn Andersen– Thorbjørn Ravn Andersen2012-08-01 14:02:50 +00:00Commented Aug 1, 2012 at 14:02
6 Answers
There's already a lot of benchmarks and discussions on this. While I don't give a lot of credit (none) to benchmarking. The top 2 contenders are (listed in order of performance speed):
- Scala
- Groovy++
I've tried both and their not the same in my use cases. Scala came out much faster than groovy++ (again.. this is MY use cases not and may show differently in your use cases). Scala was almost native java speeds.
Groovy (not Groovy++), Closure, JRuby are all really slow. Groovy and JRuby run approximately 8 times slower on simple algorithms compared to the java versions even after a decent amount of warmup.
I can't guarantee that you would get the same numbers that I did but this would be a decent order to try them in.
1 Comment
Groovy is a very good scripting language that plays very nicely with Java.
That being said, Java can run any scripting language via the command line, or it could run any other program if you wanted it to. So I would focus less on the Java side of it and more on the 'faster' side of it.
There's nothing from stopping you to writing a C++ program to compiled code and calling that.
Further, what profiling tests have you done with Jython/python? How slow is it that it doesn't meet your expectations? Is it causing problems? Is it perhaps in the python script?
8 Comments
LuaJIT https://github.com/gareins/dynamic_benchmarks
The above benchmarks show LuaJIT to be very fast but it still qualifies as a dynamic language.
Comments
This is an old question, but still very relevant and interesting. You can write Java itself as a script and run it from the command line now. But, if you are writing an embedded script for a particular application that enables JSR 223 support, you have to use an actual scripting language.
This is based on my own experience and speed testing. These are all scripting languages that run on the Java platform, which is the question the OP asked, unlike other answers.
On a modern, fast machine, running the latest JVM, it is hard to tell how slow these scripting languages are. Try running them on an an older machine. You can really see a difference. This is hard to do with Windows because supported versions won't run on older machines. Don't use an unsupported version of Windows like XP because you'll be infected as soon as you connect to the Internet to download a browser. The latest version of Linux with security patches can run on old machines and multiple architectures.
| Language | Speed |
|---|---|
| JRuby | Extremely slow, slower than Ruby |
| Groovy | Extremely slow |
| Jython 2.x | Extremely slow, slower than Python |
| JavaScript (Rhino) | Slow |
| BeanShell | Tolerable |
| Sleep | Faster and smaller than expected for a scripting language |
sleep.jar is 254kb, which is smaller than any of the jars for the other languages.
Although Sleep's syntax is somewhat similar to Perl, don't let that scare you. It's handling of objects is more like Java, which is what is important.