BeanShell, Rhino and Java -- Performance Comparison
Author: Pankaj Kumar
Last Revision Date: 8/31/05, Publication Date: 8/28/05
8:00pm, 31 Aug 2005: JavaLobby listed my blog entry in their newsletter's "pick of the week"
section today, resulting in a fair amount of traffic. I also got a jython version of my program
from Jim Adrig, which I have ran on my box and updated the article with the observed numbers.
Jim's program used Java classes java.util.Random and java.util.Date (perhaps for a fair comparison)
and hence couldn't be run as a python program. I modified it to use python native modules
BTW, if you want me to include performance figures for your favorite scripting environment then just send me the bsort program for that environment.
I have been using BeanShell for exploratory programming and developing simple utilities for quite sometime. Lately, some of these utilities have become popular with my colleagues and are finding much wider use. Given the nature of these utilities, I never worried about their runtime execution speed, but this article on Java scripting reminded me that there are better alternatives available, at least with respect to raw execution speed. This got me thinking. Although most of my current programs are not performance sensitive, what I plan to write in next few months would perhaps benefit from a well performing execution environment.
This realization coincided with my frustration with BeanShell on a number of other, somewhat
unrelated fronts: (a) not able to write
derivable scripted objects;
(b) not able to construct lists and hashes with a much more compact syntax ( basically, able to write
The above mentioned article on Java scripting talks about some performance tests, but these tests are not representative of the kind of programs that I write. My programs usually do a lot of string processing and random number generation. So I decided to compare Rhino and BeanShell with such a program.
This program generates a number of strings, places them in an array and then sorts them in-pace using
bubble sort. It has
two distinct phases: genration of 20 character long random strings and
arranging the strings in lexicographycally descending order using bubble sort algorithm. Execution
time is reported for both these phases, generation and sorting, separately. The number of strings
can be changed for a given run by changing a variable named
I wrote four versions of this program:
Note: You can download the source files by clicking on the above links. You will find the
Enough talk. Let us now look at the numbers.
These numbers are for elapsed time in milli seconds for string generation and bubble sort, shown separately, my Windows XP machine with 2.93 MHz Pentium 4, 512 MB RAM, JDK 1.5.0, BeanShell-2.0b2 and Rhino-1.6R1.
For those who don't like looking at raw numbers, here are my conclusions:
The performance advantage of Rhino alone may not sufficient for me to switch, for most of my programs spend less than 10 percent of the total execution time running BeanShell scripts (rest is spent running Java byte code from existing class libraries), but it certainly is going to be a factor.
© 2005 by Pankaj Kumar. All Rights Reserved. You may use the content of this article, including the source code, in your programs and documentation. In general, you do not need to contact me for permission unless you are reproducing a significant portion of the article or code for commercial purposes. For example, writing a program that uses the code for personal or company use does not require permission. Republishing this article, even with proper attribution, does require permission. Similarly, incorporating the code in a program that you sell for profit requires permission.
You are welcome to send me comments, suggestions or bug fixes. I will look into those as and when time permits and may even publish the good ones on this page.
You can contact me by sending e-mail to firstname.lastname@example.org.