Compilation Features in MacRuby
Foreword
Yesterday I listened to Ruby NoName Podcast (by the way, I highly recommend it to everyone), in which I heard the news that MacRuby 0.5 had left the beta stage. Also, gentlemen, the presenters said that the MacRuby package includes the excellent Ruby compiler - MacRubyC, which now compiles into dynamic libraries. Naturally, I became interested and decided to experiment.
Process
So what are we doing? We go to the official MacRuby website and download the latest version of the package. Unpack the archive, set. Next, launch the Terminal and try:

Does everything work? Excellent.
Now let's try the MacRubyC compiler. To do this, we need any .rb file, for example, hello.rb, containing only one line: We write in the terminal: The output is a neat executable file that can be safely run like a regular Unix application: However, if we try to run our program on another machine where MacRuby is not installed, the result will be disappointing - the program lacks the dynamic MacRuby library: There is a solution - static compilation. But here it’s not so simple: if you don’t have to
puts "Hello world!"
macrubyc hello.rb -o hello


LLVM , then the program will not compile:

The most logical way out is to install this LLVM: I warn you in advance that LLVM is going to take quite a while, it took me about an hour.
svn co -r 82747 llvm.org/svn/llvm-project/llvm/trunk llvm-trunk
cd llvm-trunk
./configure
UNIVERSAL=1 UNIVERSAL_ARCH="i386 x86_64" ENABLE_OPTIMIZED=1 make -j2
sudo env UNIVERSAL=1 UNIVERSAL_ARCH="i386 x86_64" ENABLE_OPTIMIZED=1 make install
Most delicious
Our programs can be easily compiled in statics and, accordingly, run on someone else's machine. It is wonderful. Well, let's do a little research.
First, size: the source code hello.rb weighs only 19 bytes. If we look at the size of the first binary, we will see that it weighs only 13.6 kilobytes. Not so much as it might seem. But we compiled it in dynamics! Let's take a look at the size of the binary compiled in statics - 14.3 megabytes! And this is only “Hello world”. However, do not worry too much: in fact, with an increase in the amount of code, the size of the executable file will not increase that much. It is quite obvious that these 14 megabytes contain libraries that the program inadvertently drags along.
Now let's touch on the most interesting - performance.
In order to measure performance, we write a program that consists of only one line: Then we compile our program in statics and dynamics, respectively, with the --static option and without it: In order to see how quickly (or slowly) our programs run programs, use the built-in Unix utility time. Measurements were carried out several times, but the result is generally the same. What do we see? We see that the program executed by the interpreter runs almost an order of magnitude faster than the standalon program. In this case, static compilation wins a few seconds over dynamic. But if you take the tests more seriously, for example, the Mandelbrot function
10000000.times { |i| puts i }
macrubyc cycle.rb -o cycle
macrubyc cycle.rb --static -o cycle-static

, then everything falls into place: the compiled program runs an order of magnitude faster - only 0.32 seconds, while the interpreted program runs about 8 seconds. Similar results are observed if we take the Takeyuchi function as a test function (about 21 seconds for interpretation and 0.5 seconds for compilation):

Instead of an afterword
That, in fact, is all. In general, the conclusions are ambiguous: on the one hand, the compiled program lags behind in linear cases, on the other hand, good optimization is used during compilation, which gives a good result in more complex cases. However, the decision to use compilation in your projects is up to you.