/bench/kilim/bench/Rec.java
Java | 89 lines | 65 code | 15 blank | 9 comment | 11 complexity | 06b26553541689477f45517f22a127f8 MD5 | raw file
1/* Copyright (c) 2006, Sriram Srinivasan 2 * 3 * You may distribute this software under the terms of the license 4 * specified in the file "License" 5 */ 6 7package kilim.bench; 8 9import kilim.*; 10 11public class Rec extends Task { 12 static boolean pause = false; 13 static boolean pausable = false; 14 /** 15 * benchmark recursion 16 * @param args the number of passes and the depth of each pass, both optional 17 */ 18 public static void main(String[] args) throws Exception { 19 int n = args.length < 1 ? 1000000 : Integer.parseInt(args[0]); 20 int d = args.length < 2 ? 100 : Integer.parseInt(args[1]); 21 22 pausable = true; 23 24 pause = true; 25 testCont(new Rec(5,5)); 26 long tpause = testCont(new Rec(n, d)); 27 28 pause = false; 29 testCont(new Rec(5,5)); 30 long tnopause = testCont(new Rec(n, d)); 31 32 pausable = false; 33 testCont(new Rec(5, 5)); 34 long tbase = testCont(new Rec(n, d)); 35 System.out.println(n + " " + tbase + " " + tnopause + " " + tpause); 36 } 37 38 public static long testCont(Rec ex) throws NotPausable, Exception { 39 long start = System.currentTimeMillis(); 40 if (pausable) { 41 Fiber f = new Fiber(ex); 42 while (true) { 43 ex.execute(f.begin()); 44 if (f.end()) break; 45 } 46 } else { 47 ex.noPauseRun(); 48 } 49 return (System.currentTimeMillis() - start); 50 } 51 52 53 int n; 54 int depth; 55 public Rec(int an, int aDepth) { 56 n = an; 57 depth = aDepth; 58 } 59 60 public void execute() throws Pausable { 61 for (int i = 0; i < n; i++) { 62 rec(depth, "foo"); 63 } 64 } 65 66 public void noPauseRun() { 67 for (int i = 0; i < n; i++) { 68 recNoPause(depth, "foo"); 69 } 70 } 71 72 private void rec(int d, String s) throws Pausable { 73 if (d == 1) { 74 if (pause) { 75 Task.yield(); 76 } 77 return; 78 } 79 rec(d-1, s); 80 } 81 82 private void recNoPause(int d, String s) { 83 if (d == 1) { 84 return; 85 } 86 recNoPause(d-1, s); 87 } 88 89}