2
0

Benchfib.st 3.0 KB

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