2
0

Benchfib.st 3.0 KB

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