Guest User

jsinger GCBench in SML

a guest
Apr 26th, 2012
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 2.14 KB | None | 0 0
  1. datatype tree = Node of int * tree * tree
  2.                         | Leaf;
  3.  
  4. val kStretchTreeDepth = 24;
  5. val kLongLivedTreeDepth = 16;
  6. val kArraySize = 500000;
  7. val kMinTreeDepth = 4;
  8. val kMaxTreeDepth = 20;
  9.  
  10. fun mktree_bottomup(0) = Leaf
  11.   | mktree_bottomup(n) =
  12.     let val left = mktree_bottomup(n-1)
  13.         and right = mktree_bottomup(n-1)
  14.     in
  15.         Node(1,left,right)
  16.     end;
  17.    
  18. fun mktree_topdown 0 = Leaf
  19.   | mktree_topdown n = Node(1,mktree_topdown(n-1), mktree_topdown(n-1));
  20.    
  21.  
  22. fun treeSize(i) =
  23.     let val w = Word.fromInt(1)
  24.     in
  25.     Word.toInt(
  26.           Word.-(Word.<<(w,
  27.                          Word.+(Word.fromInt(i),
  28.                                                   w)),
  29.                       w))
  30.     end;
  31.    
  32. fun numIters(i) = 2* treeSize(kStretchTreeDepth) div treeSize(i);
  33.  
  34. fun printDiagnostics() =
  35.     let val freeMemory = 0
  36.     and totalMemory = 0
  37.     in
  38.     let val freeString = String.concat(["free mem available: ", Int.toString(freeMemory), " bytes\n"])
  39.         and totalString = String.concat(["total mem available: ", Int.toString(totalMemory), " bytes\n"])
  40.     in
  41.        
  42.         print(freeString);
  43.         print(totalString);
  44.         print("\n")
  45.     end
  46.     end;
  47.    
  48. fun timeConstrIter(0, _) = ()
  49.   | timeConstrIter(n, depth) =
  50.       let val tempTree = mktree_bottomup(depth)
  51.       in
  52.         timeConstrIter(n-1,depth)
  53.       end;
  54.  
  55. fun timeConstruction(depth) =
  56.     let val tStart = Time.now()
  57.     and iNumIters = numIters(depth)
  58.     in
  59.     let val x = timeConstrIter(iNumIters, depth);
  60.     in
  61.         let val tFinish = Time.now();
  62.         in
  63.         print(String.concat(["constructing ", Int.toString(iNumIters), " trees of depth ", Int.toString(depth), "\n"]));
  64.         Time.-(tFinish,tStart)
  65.         end
  66.     end
  67.     end;
  68.    
  69.  
  70. fun mainLoop(depth) =
  71.     if (depth<kMinTreeDepth)
  72.     then ()
  73.     else if (depth > kMaxTreeDepth)
  74.     then ()
  75.     else
  76.     let val longLivedTree = mktree_topdown(kLongLivedTreeDepth)
  77.         and longLivedArray = Array.tabulate(kArraySize,fn i=>if (i<kArraySize div 2) then 1.0/Real.fromInt(i) else 0.0)
  78.     in
  79.         let val t = timeConstruction(depth)
  80.         in
  81.         mainLoop(depth+2)
  82.         end
  83.     end;
Advertisement
Add Comment
Please, Sign In to add comment