Simple integer math benchmark
- Posted by ed_davis Dec 05, 2013
- 6328 views
I know I'm preaching to the choir as it were here, but you might be interested in a little benchmarking I did recently.
I tested the following simple/naive/stupid prime number generator with a number of interpreters, and Euphoria came out looking very nice.
integer n, lim, k, p, pc pc = 0 n = 1 lim = 5000000 while n < lim do k = 3 p = 1 n = n + 2 while k * k <= n and p do p = floor(n / k) * k != n k = k + 2 end while if p then pc = pc + 1 end if end while ? pc
I adapted the above code to a number of interpreters, and here are the results:
prime.c 1.00 seconds - test program, compiled with gcc -O3 Euphoria 8.27 seconds - openeuphoria.com gforth 8.56 seconds - complang.tuwien.ac.at/forth/gforth tinypas.c 15.82 seconds - tiny-c.c 17.10 seconds - iro.umontreal.ca/~felipe/IFT2030-Automne2002/Complements/tinyc.c toy2.c 17.64 seconds - QDepartment/Files/Ed-Davis-stuff/toy.c Pike 30.43 seconds - pike.lysator.liu.se jwillia basic 50.26 seconds - github.com/jwillia3/BASIC oc 66.44 seconds - exmortis.narod.ru/pcode_src/oc.zip atlast 77.08 seconds - fourmilab.ch/atlast NaaLaa 92.98 seconds - naalaa.com Lua 109.56 seconds - lua.org Perl 137.69 seconds - perl.org si 189.23 seconds - drdobbs.com/cpp/si-a-c-like-script-interpreter/184408141 Yabasic 203.94 seconds - yabasic.de cint 217.31 seconds - root.cern.ch/drupal/content/cint Python 225.83 seconds - python.org Ruby 242.52 seconds - ruby-lang.org ch 294.08 seconds - softintegration.com LB Booster 533.65 seconds - bbcbasic.co.uk/lbb Little C 767.34 seconds - books.mcgraw-hill.com/downloads/products/0072121246/0072121246_code.zip PicoC 795.76 seconds - code.google.com/p/picoc
Of course you can write a faster primer number generator, but that wasn't the goal. The goal was to get a (perhaps weak) sense of interpreter speed, compared to C, and a simple interpreter I'd written, regarding integer operations and looping.
- prime.c - Test program compiled with gcc -O3
- Euphoria - It rocks!
- gforth - Fast Forth interpreter
- tinypas.c - C translation of Jan van de Snepscheut's Pascal S. Compiles to a VM. The compiler does some simple peephole optimization.
- tiny-c - Marc Feeley's Tiny C. This is a tiny C subset VM compiler/interpreter. Modified to add the operators needed to run the benchmark.
- toy2.c - Slightly modified version of toy.c. The one listed at QDepartment takes 51.01 seconds.
- Pike - Neat C-like scripting language.
- jwillia basic- A really cool BASIC interpreter, all in one header file! Compiles to a VM.
- oc - A tiny C subset VM compiler/interpreter. Was as 1991 OCCC Winner. The unobfuscated version is actually quite readable.
- atlast - Forth interpreter
- NaaLaa - Cool language for making games.
- Lua - Popular scripting language.
- Perl - There is always more than one way to do it...
- si - A tiny C subset interpreter by Al Stevens, circa 1989. This is almost a pure interpreter, however it does tokenize the source before interpreting it.
- Yabasic - Nice basic interpreter - sadly, no longer developed as of 2013.
- cint - Full C interpreter.
- Python - Popular scripting language.
- Ruby - Popular scripting language.
- ch - Full C interpreter.
- LB Booster - Runs Liberty BASIC programs faster than Liberty BASIC.
- Little C - Herbert Schildt's - pure (infamous?) tiny C subset interpreter.
- PicoC - Large subset of C interpreter.
I actually did not realize that Euphoria was so fast and/or set out to show the same. I was testing the speed of a simple interpreter I'd written, and I wanted to see how it stacked up against a few other interpreters.
And finally, like all benchmarks, take this one with a grain of salt