[svn] r5987: nemerle/trunk/snippets/shootout: . Makefile ackermann.n binarytrees.n fibo.n

malekith svnadmin at nemerle.org
Wed Nov 30 00:08:17 CET 2005


Log:
Add tests target (comparse with C# version). Add binarytrees implementation (it is much slower than C# version, why?), update fibo and ackermann.

Author: malekith
Date: Wed Nov 30 00:08:16 2005
New Revision: 5987

Added:
   nemerle/trunk/snippets/shootout/binarytrees.n
Modified:
   nemerle/trunk/snippets/shootout/   (props changed)
   nemerle/trunk/snippets/shootout/Makefile
   nemerle/trunk/snippets/shootout/ackermann.n
   nemerle/trunk/snippets/shootout/fibo.n

Modified: nemerle/trunk/snippets/shootout/Makefile
==============================================================================
--- nemerle/trunk/snippets/shootout/Makefile	(original)
+++ nemerle/trunk/snippets/shootout/Makefile	Wed Nov 30 00:08:16 2005
@@ -27,6 +27,7 @@
 #
 
 THISDIR = $(shell if cygpath --help 2>/dev/null 1>&2; then echo `cygpath -m $(CURDIR)`; else echo $(CURDIR); fi)
+TIME = @/usr/bin/time --format '%C  : real %e, %U+%S (%P%%)' $(EXECUTE) 
 
 ############################################################
 # TARGETS
@@ -38,3 +39,29 @@
 clean:
 	rm -f *.exe *.il *.dll *.pdb *.netmodule ext_test.out core core.[0-9]*
 	rm -f test.err test_error.log a.out
+
+run-test: $(TESTNAME).exe cs/$(TESTNAME).exe
+	$(TIME) ./$(TESTNAME).exe $(ARGS) > $(TESTNAME).log
+	$(TIME) ./cs/$(TESTNAME).exe $(ARGS) > $(TESTNAME)-cs.log
+
+%.exe: %.csharp
+	gmcs $< -out:$@
+
+%.exe: %.n
+	$(EXECUTE) ../../ncc/out.stage3/ncc.exe $(ADDFLAGS) -i $< -out:$@
+
+t: tests
+
+links:
+	ln -sf ../../ncc/out.stage3/*.dll .
+
+tests: links ackermann fibo binarytrees
+
+ackermann:
+	$(MAKE) TESTNAME=$@ ARGS=9 run-test
+
+fibo:
+	$(MAKE) TESTNAME=$@ ARGS=32 run-test
+	
+binarytrees:
+	$(MAKE) TESTNAME=$@ ARGS=16 run-test

Modified: nemerle/trunk/snippets/shootout/ackermann.n
==============================================================================
--- nemerle/trunk/snippets/shootout/ackermann.n	(original)
+++ nemerle/trunk/snippets/shootout/ackermann.n	Wed Nov 30 00:08:16 2005
@@ -1,17 +1,12 @@
 using System;
-using Nemerle.IO;
 
-class App {
-  public static Main(args : array[string]) : int {
-    mutable n = Convert.ToInt32(args[0]);
-    when (n < 1) n = 1;
+def args = Environment.GetCommandLineArgs ();
+def n = if (args.Length < 2) 1 else int.Parse (args [1]);
       
-    def Ack (M, N) {
-      | (0, _) => N + 1
-      | (_, 0) => Ack (M - 1, 1)
-      | _ => Ack (M - 1, Ack (M, (N - 1)))
-    };
-    printf ("Ack(3,%d): %d\n", n, Ack (3, n));
-    0
-  }
+def Ack (M, N) {
+  if (M == 0) N + 1
+  else if (N == 0) Ack (M - 1, 1)
+  else Ack (M - 1, Ack (M, (N - 1)))
 }
+
+Console.WriteLine ($ "Ack(3,$n): $(Ack (3, n))");

Added: nemerle/trunk/snippets/shootout/binarytrees.n
==============================================================================
--- (empty file)
+++ nemerle/trunk/snippets/shootout/binarytrees.n	Wed Nov 30 00:08:16 2005
@@ -0,0 +1,49 @@
+using System
+
+[Record] \
+class TreeNode 
+   left : TreeNode
+   right : TreeNode
+   item : int
+
+   this (i : int)
+     item = i
+
+   internal static bottomUp (item : int,  depth : int) : TreeNode
+     if (depth > 0)
+       TreeNode (bottomUp (2 * item - 1, depth - 1),
+                 bottomUp (2 * item, depth - 1), item)
+     else
+       TreeNode (item)
+
+   internal itemCheck () : int
+      // if necessary deallocate here 
+      if (left == null) item
+      else item + left.itemCheck () - right.itemCheck ()
+
+
+def minDepth = 4
+
+def args = Environment.GetCommandLineArgs ()
+def n = if (args.Length < 2) 0 else int.Parse (args [1])
+def maxDepth = Math.Max (minDepth + 2, n)
+def stretchDepth = maxDepth + 1
+
+mutable check = TreeNode.bottomUp (0, stretchDepth).itemCheck ()
+Console.WriteLine ($ "stretch tree of depth $stretchDepth\t check: $check")
+
+def longLivedTree = TreeNode.bottomUp (0, maxDepth);
+
+for (mutable depth = minDepth; depth <= maxDepth; depth += 2)
+  def iterations = 1 << (maxDepth - depth + minDepth)
+
+  check = 0;
+  for (mutable i = 1; i <= iterations; i++)
+    check += (TreeNode.bottomUp(i,depth)).itemCheck()  
+    check += (TreeNode.bottomUp(-i,depth)).itemCheck()
+
+  Console.WriteLine ($ "$(iterations * 2)\t trees of depth $depth\t "
+                       "check: $check")
+
+Console.WriteLine ($ "long lived tree of depth $maxDepth\t "
+                     "check: $(longLivedTree.itemCheck ())")

Modified: nemerle/trunk/snippets/shootout/fibo.n
==============================================================================
--- nemerle/trunk/snippets/shootout/fibo.n	(original)
+++ nemerle/trunk/snippets/shootout/fibo.n	Wed Nov 30 00:08:16 2005
@@ -1,15 +1,10 @@
 using System;
-using Nemerle.IO;
 
-class App {
-  public static Main(args : array[string]) : int {
-    mutable N = Convert.ToInt32(args[0]);
-    when (N < 1) N = 1;
+def args = Environment.GetCommandLineArgs ();
+def n = if (args.Length < 2) 1 else int.Parse (args [1]);
     
-    def fib(n) : int {
+def fib(n) : int {
       if (n < 2) 1 else fib (n - 2) + fib (n - 1)
-    };
-    printf ("%d\n", fib (N));
-    0;
-  }
 }
+
+Console.WriteLine (fib (n));



More information about the svn mailing list