Few weeks ago I published a runtime performance comparison article comparing performance of bubble sort programs under Beanshell, JavaScript (Rhino) and Java. Lateron, based on a program contributed by Jim Adrig, I added Jython and Python programs as well.
Although I had received a few requests to add Groovy in the mix, no one actually came forward with a working program. However, this changed yesterday and I got a groovy bsort program from Graeme Sutherland. I ran it on my box under the same conditions as other programs and am reporting the performance figures in the following updated table:
No. of strings | num: 1000 | num: 5000 | num: 10000 | |||
---|---|---|---|---|---|---|
Program | gen. | sort | gen. | sort | gen. | sort |
bsh/bsort (BeanShell, interpreted) | 688 | 15500 | 2515 | 402000 | 4780 | 1623000 |
js/bsort1 (intepreted) | 140 | 450 | 344 | 11050 | 600 | 46330 |
js/bsort1 (compiled) | 140 | 440 | 344 | 10940 | 580 | 46020 |
js/bsort2 (intepreted) | 440 | 1660 | 1360 | 41580 | 2516 | 172000 |
js/bsort2 (compiled) | 440 | 1625 | 1345 | 40390 | 2470 | 168000 |
java/bsort (java, compiled) | 32 | 47 | 63 | 1093 | 109 | 4625 |
jython/bsort (jython-2.1) | 172 | 328 | 391 | 8860 | 688 | 44360 |
python/bsort (python-2.4.1) | 204 | 370 | 1020 | 9200 | 2040 | 37100 |
python/bsort (jython-2.1) | 515 | 300 | 1500 | 8400 | 2800 | 43400 |
groovy-1.0-jsr-03/bsort (interpreted) | 300 | 4800 | 800 | 118000 | 1345 | 469000 |
groovy-1.0-jsr-03/bsort (compiled) | 300 | 4700 | 840 | 117000 | 1400 | 465000 |
As you can see, Groovy performs better than Beanshell but falls behind Jython and JavaScript(Rhino). Interestingly, the compiled version performed only marginally better than the interpreted one. The same was true for JavaScript as well.
As Graeme wasn't confident that his code is optimal for Groovy, I am not updating my article yet. Perhaps Groovy fans can take a look and let me know what they think about it.
Comments (5)
Just had a couple of minutes to spare on the Groovy script, and I noticed the sample script is using lists instead of arrays. By using arrays, the results are roughly 30 to 40% better.
Posted by Guillaume Laforge | October 13, 2005 2:54 AM
Posted on October 13, 2005 02:54
Here is the script I used (hoping the comment won't be truncated or rejected...):
// Generates n random strings and then sorts them using bubble sort.
import java.util.Random;
class bsort {
@Property num = 1000
@Property ssize = 20
char[] chars = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
]
public void sort ( String[] array ) {
for ( i in ( array.size () - 1 )..1 ) {
for ( j in 1..i ) {
def current = array[j]
def last = array[j - 1]
if ( current
Posted by Guillaume Laforge | October 13, 2005 2:55 AM
Posted on October 13, 2005 02:55
I'll be the first to admit that Groovy is slow and there are tons of optimizations that should be done at some point. But I think the real point of Groovy is to be able to use the Java APIs natively from a scripting language so using Groovy to do the sort rather than just calling Collections.sort() seems like it would never be seen in practice.
A more interesting comparison, one that Groovy would probably still lose, would be to measure how long it takes a language to lookup an API call and dispatch since that is mostly what your Groovy code will be doing.
Posted by Sam Pullara | October 13, 2005 10:39 PM
Posted on October 13, 2005 22:39
although it is a microbenchmark, it is good to see Java is kicking ass with at least 10 times better performance..
Posted by aaa | October 14, 2005 8:50 AM
Posted on October 14, 2005 08:50
I think the java.util.RandomAccess interface should have the following a methods added and supported by all mutable implementations and that these methods be used by Collections.sort() mehods:
void sort();
void sort(Comparactor c);
void sort(int from, int to);
void sort(int from, int to, Comparactor c);
int binarySearch(Object key);
int binarySearch(Object key,Comparator c);
int binarySearch(Object key, int from, int to);
int binarySearch(Object key, int from, int to, Comparactor c);
IMHO Collections.sort() is very inefficient for java.util.RandomAccess and rather limnited too, it neadlessly requires n*(2+log(n)) steps instead of n*log(n), if the sort was done directly on the array inside the class! This was easy to do with a Vector subclass, but annoyingly impossible for ArrayList because the properties are all stupidly private instead of protected!
Me thinks Josh Bloch and Neal Gafter lost the plot and a great opertunity for optimisimg sorting and binary searching random access collections lists.
Posted by Infernoz | October 19, 2005 12:48 PM
Posted on October 19, 2005 12:48