Benchfib.st 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. Object subclass: #Benchfib
  2. instanceVariableNames: ''
  3. package: 'Benchfib'!
  4. !Benchfib class methodsFor: 'not yet classified'!
  5. main
  6. | result |
  7. result := 0 tinyBenchmarks.
  8. console log: '0 tinyBenchmarks => ' , result asString.
  9. result := 0 jstinyBenchmarks.
  10. console log: '0 jstinyBenchmarks => ' , result asString
  11. ! !
  12. !Number methodsFor: '*Benchfib'!
  13. benchFib
  14. "Handy send-heavy benchmark"
  15. "(result // seconds to run) = approx calls per second"
  16. " | r t |
  17. t := Time millisecondsToRun: [r := 26 benchFib].
  18. (r * 1000) // t"
  19. "138000 on a Mac 8100/100"
  20. ^ self < 2
  21. ifTrue: [1]
  22. ifFalse: [(self-1) benchFib + (self-2) benchFib + 1]
  23. !
  24. benchmark "Handy bytecode-heavy benchmark"
  25. "(500000 // time to run) = approx bytecodes per second"
  26. "5000000 // (Time millisecondsToRun: [10 benchmark]) * 1000"
  27. "3059000 on a Mac 8100/100"
  28. | size flags prime k count |
  29. size := 8190.
  30. 1 to: self do:
  31. [:iter |
  32. count := 0.
  33. flags := Array new.
  34. size timesRepeat: [ flags add: true].
  35. 1 to: size do:
  36. [:i | (flags at: i) ifTrue:
  37. [prime := i+1.
  38. k := i + prime.
  39. [k <= size] whileTrue:
  40. [flags at: k put: false.
  41. k := k + prime].
  42. count := count + 1]]].
  43. ^ count
  44. !
  45. tinyBenchmarks
  46. "Report the results of running the two tiny Squeak benchmarks.
  47. ar 9/10/1999: Adjusted to run at least 1 sec to get more stable results"
  48. "0 tinyBenchmarks"
  49. "On a 292 MHz G3 Mac: 22727272 bytecodes/sec; 984169 sends/sec"
  50. "On a 400 MHz PII/Win98: 18028169 bytecodes/sec; 1081272 sends/sec"
  51. | t1 t2 r n1 n2 |
  52. n1 := 1.
  53. [t1 := Date millisecondsToRun: [n1 benchmark].
  54. t1 < 1000] whileTrue:[n1 := n1 * 2]. "Note: #benchmark's runtime is about O(n)"
  55. n2 := 28.
  56. [t2 := Date millisecondsToRun: [r := n2 benchFib].
  57. t2 < 1000] whileTrue:[n2 := n2 + 1].
  58. "Note: #benchFib's runtime is about O(k^n),
  59. where k is the golden number = (1 + 5 sqrt) / 2 = 1.618...."
  60. ^ ((n1 * 500000 * 1000) / t1) printString, ' bytecodes/sec; ',
  61. ((r * 1000) / t2) printString, ' sends/sec'
  62. !
  63. jsbenchFib
  64. <if (this < 2) {
  65. return 1;
  66. } else {
  67. return (this-1)._jsbenchFib() + (this-2)._jsbenchFib() + 1;}>
  68. !
  69. jsbenchmark
  70. <
  71. var size = 8190;
  72. var count;
  73. for (var z=0;z<this;z++) {
  74. count = 0;
  75. var flags = new Array();
  76. for (var p=0; p<size; p++) {
  77. flags[p] = true;
  78. }
  79. for (var i=1;i<=size;i++) {
  80. if (flags[i-1]) {
  81. var prime = i+1;
  82. var k = i + prime;
  83. while (k <= size) {
  84. flags[k-1] = false;
  85. k = k + prime;
  86. }
  87. count = count + 1;
  88. }
  89. }
  90. }
  91. return count>
  92. !
  93. jstinyBenchmarks
  94. "0 jstinyBenchmarks"
  95. | t1 t2 r n1 n2 |
  96. n1 := 1.
  97. [t1 := Date millisecondsToRun: [n1 jsbenchmark].
  98. t1 < 1000] whileTrue:[n1 := n1 * 2]. "Note: #benchmark's runtime is about O(n)"
  99. n2 := 28.
  100. [t2 := Date millisecondsToRun: [r := n2 jsbenchFib].
  101. t2 < 1000] whileTrue:[n2 := n2 + 1].
  102. "Note: #jsbenchFib's runtime is about O(k^n),
  103. where k is the golden number = (1 + 5 sqrt) / 2 = 1.618...."
  104. ^ ((n1 * 500000 * 1000) / t1) printString, ' bytecodes/sec; ',
  105. ((r * 1000) / t2) printString, ' sends/sec'
  106. ! !