1
0

Benchfib.st 2.9 KB

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