Skip to content
Browse files

mix/benchmark: copied from ~/project/test/benchmark

  • Loading branch information...
1 parent 9cd2ca1 commit 68496031e9f8a5a58d17ecebd7cee9aa11ca306b @godfat committed Feb 8, 2011
View
13 mix/benchmark/Fib.java
@@ -0,0 +1,13 @@
+
+public class Fib{
+ static private int fib(int n){
+ if(n<2)
+ return n;
+ else
+ return fib(n-1) + fib(n-2);
+ }
+ static public void main(String[] args){
+ for(int i=0; i<35; ++i)
+ System.out.println("n="+i+" => "+fib(i));
+ }
+}
View
22 mix/benchmark/fib-clisp241.fas
@@ -0,0 +1,22 @@
+(|SYSTEM|::|VERSION| '(20060802.))
+#0Y |CHARSET|::|UTF-8|
+#Y(#:|2 6 (DEFUN FIB (N) ...)-1|
+ #20Y(00 00 00 00 00 00 00 00 00 01 DA 2F 01 DA DC 32 83 C5 19 01)
+ (|COMMON-LISP-USER|::|FIB| |SYSTEM|::|REMOVE-OLD-DEFINITIONS|
+ #Y(|COMMON-LISP-USER|::|FIB|
+ #35Y(00 00 00 00 01 00 00 00 06 02 AD DA 90 01 2F 10 97 01 6C 76 DB AF
+ 73 02 35 6C 6F 33 02 35 19 02 9E 19 02)
+ (2. -2.) (|COMMON-LISP|::|T| |COMMON-LISP|::|T| |COMMON-LISP|::|T|)
+ (|COMMON-LISP-USER|::|N|) |COMMON-LISP|::|NIL|))
+ (|COMMON-LISP|::|T| |COMMON-LISP|::|T| |COMMON-LISP|::|T|))
+#Y(#:|8 8 (LOOP FOR I ...)-2|
+ #34Y(00 00 00 00 00 00 00 00 00 01 DA 1B 0C DC 6B 03 AE AF 6F 04 33 03 15 85
+ 00 AC DB 91 01 30 6E 00 19 02)
+ (0. 34.
+ #Y(#:|8 8 (LOOP FOR I ...)-2-1|
+ #42Y(00 00 00 00 03 00 00 00 01 19 DA B1 38 02 31 8B B0 01 06 B6 2D 08
+ 01 DC B1 38 02 31 8B B0 01 06 B5 2D 08 01 B0 31 8D 9E 19 05)
+ ("n=" |SYSTEM|::|DO-FORMAT-DECIMAL| " => ")
+ (|COMMON-LISP|::|T| |COMMON-LISP|::|T| |COMMON-LISP|::|T|))
+ |COMMON-LISP|::|*STANDARD-OUTPUT*| |COMMON-LISP-USER|::|FIB|)
+ (|COMMON-LISP|::|T| |COMMON-LISP|::|T| |COMMON-LISP|::|T|))
View
3 mix/benchmark/fib-clisp241.lib
@@ -0,0 +1,3 @@
+#0Y |CHARSET|::|UTF-8|
+(|SYSTEM|::|C-DEFUN| '|COMMON-LISP-USER|::|FIB|
+ (|SYSTEM|::|LAMBDA-LIST-TO-SIGNATURE| '(|COMMON-LISP-USER|::|N|)))
View
14 mix/benchmark/fib.c
@@ -0,0 +1,14 @@
+
+#include <stdio.h>
+
+int fib(int n){
+ if(n<2)
+ return n;
+ else
+ return fib(n-1) + fib(n-2);
+}
+
+int main(){
+ for(int i=0; i<35; ++i)
+ printf("n=%d => %d\n", i, fib(i));
+}
View
16 mix/benchmark/fib.cs
@@ -0,0 +1,16 @@
+
+using System;
+
+class Fib{
+ private static int fib(int n){
+ if(n<2)
+ return n;
+ else
+ return fib(n-1) + fib(n-2);
+ }
+ public static int Main(String[] args){
+ for(int i=0; i<35; ++i)
+ Console.WriteLine("n={0} => {1}", i, fib(i));
+ return 0;
+ }
+}
View
14 mix/benchmark/fib.d
@@ -0,0 +1,14 @@
+
+import std.stdio;
+
+int fib(int n){
+ if(n<2)
+ return n;
+ else
+ return fib(n-1) + fib(n-2);
+}
+
+void main(){
+ for(int i=0; i<35; ++i)
+ writefln("n=%d => %d", i, fib(i));
+}
View
11 mix/benchmark/fib.hs
@@ -0,0 +1,11 @@
+
+import Control.Monad
+import Text.Printf
+
+fib :: Int -> Int
+fib 0 = 0
+fib 1 = 1
+fib n = fib (n-1) + fib (n-2)
+
+main = forM_ [0..34] $ \i ->
+ printf "n=%d => %d\n" i (fib i)
View
8 mix/benchmark/fib.lisp
@@ -0,0 +1,8 @@
+
+(defun fib (n)
+ (if (< n 2)
+ n
+ (+ (fib (- n 1))
+ (fib (- n 2)))))
+
+(loop for i from 0 to 34 do (format t "n=~d => ~d~%" i (fib i)))
View
11 mix/benchmark/fib.ml
@@ -0,0 +1,11 @@
+
+let rec fib = fun n ->
+ if n<2
+ then n
+ else fib (n-1) + fib (n-2)
+ ;;
+
+for i = 0 to 34 do
+ Printf.printf "n=%d => %d\n" i (fib i)
+done
+;;
View
BIN mix/benchmark/fib.o
Binary file not shown.
View
10 mix/benchmark/fib.php
@@ -0,0 +1,10 @@
+<?php
+ function fib($n){
+ if($n<2)
+ return $n;
+ else
+ return fib($n-1) + fib($n-2);
+ }
+ for($i=0; $i<35; ++$i)
+ echo "n=$i => ".fib($i)."\n";
+?>
View
13 mix/benchmark/fib.pl
@@ -0,0 +1,13 @@
+use strict;
+sub fib{
+ if($_[0] < 2){
+ return $_[0];
+ }
+ else{
+ return fib($_[0] - 1) + fib($_[0] - 2);
+ }
+}
+
+for(my $i=0; $i<35; ++$i){
+ print "n=$i => ".fib($i)."\n"
+}
View
8 mix/benchmark/fib.py
@@ -0,0 +1,8 @@
+def fib(n):
+ if n<2:
+ return n
+ else:
+ return fib(n-1) + fib(n-2)
+
+for i in range(35):
+ print "n=%d => %d" % (i, fib(i))
View
11 mix/benchmark/fib.rb
@@ -0,0 +1,11 @@
+def fib n
+ if n<2
+ n
+ else
+ fib(n-1) + fib(n-2)
+ end
+end
+
+35.times{ |i|
+ puts "n=#{i} => #{fib(i)}"
+}
View
15 mix/benchmark/fib2.hs
@@ -0,0 +1,15 @@
+
+module Fib2 where
+
+fibs = 0 : 1 : [ x + y | (x, y) <- zip fibs (tail fibs)]
+
+fib :: Int -> Int
+fib n = fib' id n where
+ fib' f n | n < 2 = f n
+ fib' f n = fib' (\v -> fib' (\m -> v + f m) (n-2)) (n-1)
+
+--
+
+mydrop :: Int -> [a] -> [a]
+mydrop 0 xs = xs
+mydrop (n+1) (x:xs) = mydrop n xs
View
76 mix/benchmark/forkrb.rb
@@ -0,0 +1,76 @@
+
+require 'benchmark'
+require 'thread'
+require 'rubygems'
+require 'facets'
+require 'forkoff'
+
+module Enumerable
+ def fork2(max_number_of_threads=nil, &block)
+ thread_limiter = EV::ThreadLimiter.new(max_number_of_threads)
+ collect do |x|
+ thread_limiter.fork do
+ Thread.current.abort_on_exception = true
+
+ r, w = IO.pipe
+
+ if pid = Process.fork
+ w.close
+ Process.wait(pid)
+ data = r.read
+ r.close
+ Marshal.load(data)
+ else
+ r.close
+ Marshal.dump(block.call(x), w)
+ w.close
+ exit
+ end
+ end
+ end.collect do |t|
+ t.value
+ end
+ end
+end
+module EV
+ class ThreadLimiter
+ def initialize(max_number_of_threads)
+ @number_of_threads = 0
+ @max_number_of_threads = max_number_of_threads
+
+ yield(self) if block_given?
+ end
+
+ def fork(*args, &block)
+ Thread.pass while @max_number_of_threads and
+ @max_number_of_threads > 0 and
+ @number_of_threads > @max_number_of_threads
+
+ # If this methods is called from several threads, then
+ # @number_of_threads might get bigger than @max_number_of_threads.
+ # This usually a) isn't the case and b) doesn't really matter (to me...).
+ # I'm willing to accept this "risk", because a) Thread.exclusive is
+ # much, much faster than Mutex#synchronize and b) we can't run into
+ # deadlocks.
+
+ Thread.exclusive{@number_of_threads += 1}
+
+ Thread.fork do
+ begin
+ res = block.call(*args)
+ ensure
+ Thread.exclusive{@number_of_threads -= 1}
+ end
+
+ res
+ end
+ end
+ end
+end
+
+Benchmark.bm{ |bm|
+ slow = lambda{ |i| (0..200_000).to_a.inject(&:+) }
+ bm.report('raw'){ (0..5).to_a.each(&slow) }
+ bm.report('fork2'){ (0..5).to_a.fork2(2, &slow) }
+ bm.report('forkoff'){ (0..5).to_a.forkoff(&slow) }
+}
View
18 mix/benchmark/make.sh
@@ -0,0 +1,18 @@
+
+ghc fib.hs -o fib-ghc661 --make
+
+gdc fib.d -o fib-gdc024 --expensive-optimizations --release -O3 -Os
+
+como fib.c -o fib-como438 --c99 -DNDEBUG -O3 -Os
+gcc fib.c -o fib-gcc4-apple -std=c99 -DNDEBUG --expensive-optimizations -O3 -Os
+gcc-mp-4.2 fib.c -o fib-gcc42-mp -std=c99 -DNDEBUG --expensive-optimizations -O3 -Os
+gcc-mp-4.3 fib.c -o fib-gcc43-mp -std=c99 -DNDEBUG --expensive-optimizations -O3 -Os
+
+ocamlopt fib.ml -o fib-ocaml -unsafe
+
+javac Fib.java
+gcj-mp-4.2 Fib.java -o fib-gcj42-mp --main=Fib --expensive-optimizations -O3 -Os
+
+mcs fib.cs
+
+clisp -c -ansi fib.lisp -o fib-clisp241
View
38 mix/benchmark/name.rb
@@ -0,0 +1,38 @@
+
+require 'benchmark'
+
+Benchmark.bm{ |bm|
+ n = 50000
+ puts 'NameError'
+ bm.report{
+ n.times{
+ begin; var; rescue; end
+ }
+ }
+ puts 'local_variables.include?'
+ bm.report{
+ n.times{
+ local_variables.include? 'var'
+ }
+ }
+ puts 'global_variables.include?'
+ bm.report{
+ n.times{
+ global_variables.include? 'var'
+ }
+ }
+ 10.times{ |i| eval("$a#{i} = 1") }
+ puts 'many variables in global_variables.include?'
+ bm.report{
+ n.times{
+ global_variables.include? 'var'
+ }
+ }
+ puts 'hash optimized many variables in global_variables'
+ bm.report{
+ vars = Hash[*global_variables.zip([true]*global_variables.size).flatten]
+ n.times{
+ vars['var']
+ }
+ }
+}
View
124 mix/benchmark/run.sh
@@ -0,0 +1,124 @@
+
+
+# real 0m0.597s
+# user 0m0.344s
+# sys 0m0.008s
+#
+# real 0m0.363s
+# user 0m0.341s
+# sys 0m0.005s
+#
+# real 0m0.346s
+# user 0m0.340s
+# sys 0m0.005s
+#
+# real 0m0.345s
+# user 0m0.339s
+# sys 0m0.004s
+#
+# real 0m0.370s
+# user 0m0.332s
+# sys 0m0.005s
+#
+# real 0m0.311s
+# user 0m0.301s
+# sys 0m0.006s
+#
+# real 0m1.294s
+# user 0m0.362s
+# sys 0m0.080s
+#
+# real 0m4.993s
+# user 0m0.544s
+# sys 0m0.134s
+#
+# real 0m0.486s
+# user 0m0.406s
+# sys 0m0.017s
+#
+# real 0m2.144s
+# user 0m2.081s
+# sys 0m0.020s
+#
+# real 0m8.642s
+# user 0m8.468s
+# sys 0m0.018s
+#
+# real 0m25.642s
+# user 0m25.481s
+# sys 0m0.033s
+#
+# real 0m6.187s
+# user 0m6.168s
+# sys 0m0.011s
+#
+# real 0m30.681s
+# user 0m29.794s
+# sys 0m0.071s
+#
+# real 0m15.055s
+# user 0m14.823s
+# sys 0m0.076s
+#
+# real 0m14.892s
+# user 0m14.185s
+# sys 0m0.114s
+#
+# real 0m36.07s
+# user 0m35.88s
+# sys 0m0.03s
+
+# 1st
+echo './fib-gdc024'
+time ./fib-gdc024
+
+# 2nd
+echo './fib-como438'
+time ./fib-como438
+echo './fib-gcc4-apple'
+time ./fib-gcc4-apple
+echo './fib-gcc42-mp'
+time ./fib-gcc42-mp
+echo './fib-gcc43-mp'
+time ./fib-gcc43-mp
+
+#
+echo './fib-ocaml'
+time ./fib-ocaml
+
+# 3rd
+echo 'java Fib'
+time java -server Fib
+
+# 4th
+echo './fib-gcj42-mp'
+time ./fib-gcj42-mp
+
+# 5th
+echo './mono fib.exe'
+time mono fib.exe
+
+# 6th
+echo './fib-ghc661'
+time ./fib-ghc661
+
+###############
+
+echo 'clisp241'
+time clisp fib-clisp241
+
+echo 'ruby18'
+time ruby fib.rb
+echo 'ruby19'
+time /opt/local/ruby-svn/bin/ruby-svn fib.rb
+
+echo 'php524'
+time php fib.php
+
+echo 'python24'
+time python2.4 fib.py
+echo 'python25'
+time python2.5 fib.py
+
+echo 'perl58'
+time perl fib.pl

0 comments on commit 6849603

Please sign in to comment.
Something went wrong with that request. Please try again.