/bench/kilim/bench/Rec.java

http://github.com/kilim/kilim · 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. package kilim.bench;
  7. import kilim.*;
  8. public class Rec extends Task {
  9. static boolean pause = false;
  10. static boolean pausable = false;
  11. /**
  12. * benchmark recursion
  13. * @param args the number of passes and the depth of each pass, both optional
  14. */
  15. public static void main(String[] args) throws Exception {
  16. int n = args.length < 1 ? 1000000 : Integer.parseInt(args[0]);
  17. int d = args.length < 2 ? 100 : Integer.parseInt(args[1]);
  18. pausable = true;
  19. pause = true;
  20. testCont(new Rec(5,5));
  21. long tpause = testCont(new Rec(n, d));
  22. pause = false;
  23. testCont(new Rec(5,5));
  24. long tnopause = testCont(new Rec(n, d));
  25. pausable = false;
  26. testCont(new Rec(5, 5));
  27. long tbase = testCont(new Rec(n, d));
  28. System.out.println(n + " " + tbase + " " + tnopause + " " + tpause);
  29. }
  30. public static long testCont(Rec ex) throws NotPausable, Exception {
  31. long start = System.currentTimeMillis();
  32. if (pausable) {
  33. Fiber f = new Fiber(ex);
  34. while (true) {
  35. ex.execute(f.begin());
  36. if (f.end()) break;
  37. }
  38. } else {
  39. ex.noPauseRun();
  40. }
  41. return (System.currentTimeMillis() - start);
  42. }
  43. int n;
  44. int depth;
  45. public Rec(int an, int aDepth) {
  46. n = an;
  47. depth = aDepth;
  48. }
  49. public void execute() throws Pausable {
  50. for (int i = 0; i < n; i++) {
  51. rec(depth, "foo");
  52. }
  53. }
  54. public void noPauseRun() {
  55. for (int i = 0; i < n; i++) {
  56. recNoPause(depth, "foo");
  57. }
  58. }
  59. private void rec(int d, String s) throws Pausable {
  60. if (d == 1) {
  61. if (pause) {
  62. Task.yield();
  63. }
  64. return;
  65. }
  66. rec(d-1, s);
  67. }
  68. private void recNoPause(int d, String s) {
  69. if (d == 1) {
  70. return;
  71. }
  72. recNoPause(d-1, s);
  73. }
  74. }