[Mono-list] mini VM experiments

Paolo Molaro lupus@ximian.com
Wed, 11 Jul 2001 12:04:14 +0200

Miguel asked me to post this one here as well: some results
from a prototype VM for CIL.

This afternoon I was tired of working on the disassembler
finding things not well or wrongly specified, so I started
writing a virtual machine for IL to get a grasp on some
possible implementation details.
I used the recursive fibonacci code as used in the comparisons in

=cut cut=
public class Fib {

	public static int fib (int n) {
		if (n < 2)
			return 1;
		return fib(n-2)+fib(n-1);
	public static int Main () {
		return fib(25);
=cut cut=

This was compiled with csc and run both in windows and on linux.

Note that I hardcoded 25 because we don't have an implementation
of the string class for the command line args...
I implemented only the code to run this sample, with a couple
of limitations: only integer arithmetrics and only one arg
in methods (both of these problems are easily solved).
No optimizations have been done, apart from a method lookup cache.

Ok, now with the surprise:
this implementation ran faster than the perl, rep and python
implementations, but also faster than the same program executed
in windows (bug report for the MS people, if they are listening:
the program dumps core with fib(25) and possibly bigger values).

Typical runs (fib(25)) in seconds:

gcc	0.020
mono	0.330
rep	0.460
mswin	0.780 (used fib(20) run in a cygwin environment)
perl	0.980
python	1.260

Some further notes: the mswin number is bad because it's slow
to startup, other programs show it's considerably faster than
a bare-bones interpreter (and with fib(25) it can be argued
it's faster too, since it crashes pretty soon;-).
Anyway the ratios with python/perl are mostly the same even
with the nested loops program.

Another interesting point: using GCC's computed goto feature
doesn't result in any performace gain over a switch statement.


lupus@debian.org                                     debian/rules
lupus@ximian.com                             Monkeys do it better