Can't be done. Unless you intend to build your own super fast vm. On my box Perl consistently runs in around 2 seconds, Ruby takes 9. There is nothing that can be done to the source (other than getting the code to actually compile - your source is broken) that can improve more than a second or so on that performance. Haven't tried it under JRuby though, you might experience better performance there. But to honest I wouldn't sweat it that Ruby is not the best tool for this sort of thing (number crunching), my wife's car cannot best a dragster but it gets her where she needs to be.