2
0

Benchfib.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. smalltalk.addPackage('Benchfib', {});
  2. smalltalk.addClass('Benchfib', smalltalk.Object, [], 'Benchfib');
  3. smalltalk.addMethod(
  4. "_main",
  5. smalltalk.method({
  6. selector: "main",
  7. category: 'not yet classified',
  8. fn: function (){
  9. var self=this;
  10. var result=nil;
  11. (result=smalltalk.send((0), "_tinyBenchmarks", []));
  12. smalltalk.send((typeof console == 'undefined' ? nil : console), "_log_", [smalltalk.send("0 tinyBenchmarks => ", "__comma", [result])]);
  13. return self;},
  14. args: [],
  15. source: "main\x0a\x0a\x09| result |\x0a\x09result := 0 tinyBenchmarks.\x0a\x09console log: '0 tinyBenchmarks => ' , result",
  16. messageSends: ["tinyBenchmarks", "log:", ","],
  17. referencedClasses: []
  18. }),
  19. smalltalk.Benchfib.klass);
  20. smalltalk.addMethod(
  21. "_benchFib",
  22. smalltalk.method({
  23. selector: "benchFib",
  24. category: '*Benchfib',
  25. fn: function (){
  26. var self=this;
  27. return ((($receiver = self < (2)).klass === smalltalk.Boolean) ? ($receiver ? (function(){return (1);})() : (function(){return ((($receiver = ((($receiver = smalltalk.send(self - (1), "_benchFib", [])).klass === smalltalk.Number) ? $receiver +smalltalk.send(self - (2), "_benchFib", []) : smalltalk.send($receiver, "__plus", [smalltalk.send(self - (2), "_benchFib", [])]))).klass === smalltalk.Number) ? $receiver +(1) : smalltalk.send($receiver, "__plus", [(1)]));})()) : smalltalk.send($receiver, "_ifTrue_ifFalse_", [(function(){return (1);}), (function(){return ((($receiver = ((($receiver = smalltalk.send(self - (1), "_benchFib", [])).klass === smalltalk.Number) ? $receiver +smalltalk.send(self - (2), "_benchFib", []) : smalltalk.send($receiver, "__plus", [smalltalk.send(self - (2), "_benchFib", [])]))).klass === smalltalk.Number) ? $receiver +(1) : smalltalk.send($receiver, "__plus", [(1)]));})]));
  28. return self;},
  29. args: [],
  30. source: "benchFib \x0a\x09\x22Handy send-heavy benchmark\x22\x0a\x09\x22(result // seconds to run) = approx calls per second\x22\x0a\x09\x22 | r t |\x0a\x09 t := Time millisecondsToRun: [r := 26 benchFib].\x0a\x09 (r * 1000) // t\x22\x0a\x09\x22138000 on a Mac 8100/100\x22\x0a\x09^ self < 2 \x0a\x09\x09ifTrue: [1] \x0a\x09\x09ifFalse: [(self-1) benchFib + (self-2) benchFib + 1]",
  31. messageSends: ["ifTrue:ifFalse:", "<", "+", "benchFib", "-"],
  32. referencedClasses: []
  33. }),
  34. smalltalk.Number);
  35. smalltalk.addMethod(
  36. "_benchmark",
  37. smalltalk.method({
  38. selector: "benchmark",
  39. category: '*Benchfib',
  40. fn: function (){
  41. var self=this;
  42. var size=nil;
  43. var flags=nil;
  44. var prime=nil;
  45. var k=nil;
  46. var count=nil;
  47. (size=(8190));
  48. smalltalk.send((1), "_to_do_", [self, (function(iter){(count=(0));(flags=smalltalk.send((smalltalk.Array || Array), "_new", []));smalltalk.send(size, "_timesRepeat_", [(function(){return smalltalk.send(flags, "_add_", [true]);})]);return smalltalk.send((1), "_to_do_", [size, (function(i){return ((($receiver = smalltalk.send(flags, "_at_", [i])).klass === smalltalk.Boolean) ? ($receiver ? (function(){(prime=((($receiver = i).klass === smalltalk.Number) ? $receiver +(1) : smalltalk.send($receiver, "__plus", [(1)])));(k=((($receiver = i).klass === smalltalk.Number) ? $receiver +prime : smalltalk.send($receiver, "__plus", [prime])));(function(){while((function(){return ((($receiver = k).klass === smalltalk.Number) ? $receiver <=size : smalltalk.send($receiver, "__lt_eq", [size]));})()) {(function(){smalltalk.send(flags, "_at_put_", [k, false]);return (k=((($receiver = k).klass === smalltalk.Number) ? $receiver +prime : smalltalk.send($receiver, "__plus", [prime])));})()}})();return (count=((($receiver = count).klass === smalltalk.Number) ? $receiver +(1) : smalltalk.send($receiver, "__plus", [(1)])));})() : nil) : smalltalk.send($receiver, "_ifTrue_", [(function(){(prime=((($receiver = i).klass === smalltalk.Number) ? $receiver +(1) : smalltalk.send($receiver, "__plus", [(1)])));(k=((($receiver = i).klass === smalltalk.Number) ? $receiver +prime : smalltalk.send($receiver, "__plus", [prime])));(function(){while((function(){return ((($receiver = k).klass === smalltalk.Number) ? $receiver <=size : smalltalk.send($receiver, "__lt_eq", [size]));})()) {(function(){smalltalk.send(flags, "_at_put_", [k, false]);return (k=((($receiver = k).klass === smalltalk.Number) ? $receiver +prime : smalltalk.send($receiver, "__plus", [prime])));})()}})();return (count=((($receiver = count).klass === smalltalk.Number) ? $receiver +(1) : smalltalk.send($receiver, "__plus", [(1)])));})]));})]);})]);
  49. return count;
  50. return self;},
  51. args: [],
  52. source: "benchmark \x0a\x09\x22Handy bytecode-heavy benchmark\x22\x0a\x09\x22(500000 // time to run) = approx bytecodes per second\x22\x0a\x09\x225000000 // (Time millisecondsToRun: [10 benchmark]) * 1000\x22\x0a\x09\x223059000 on a Mac 8100/100\x22\x0a | size flags prime k count |\x0a size := 8190.\x0a 1 to: self do:\x0a [:iter |\x0a count := 0.\x0a flags := Array new.\x0a size timesRepeat: [ flags add: true].\x0a 1 to: size do:\x0a [:i | (flags at: i) ifTrue:\x0a [prime := i+1.\x0a k := i + prime.\x0a [k <= size] whileTrue:\x0a [flags at: k put: false.\x0a k := k + prime].\x0a count := count + 1]]].\x0a ^ count",
  53. messageSends: ["to:do:", "new", "timesRepeat:", "add:", "ifTrue:", "at:", "+", "whileTrue:", "<=", "at:put:"],
  54. referencedClasses: ["Array"]
  55. }),
  56. smalltalk.Number);
  57. smalltalk.addMethod(
  58. "_jsbenchFib",
  59. smalltalk.method({
  60. selector: "jsbenchFib",
  61. category: '*Benchfib',
  62. fn: function (){
  63. var self=this;
  64. if (this < 2) {
  65. return 1;
  66. } else {
  67. return (this-1)._jsbenchFib() + (this-2)._jsbenchFib() + 1;};
  68. return self;},
  69. args: [],
  70. source: "jsbenchFib\x0a \x0a\x09<if (this < 2) {\x0areturn 1;\x0a} else {\x0areturn (this-1)._jsbenchFib() + (this-2)._jsbenchFib() + 1;}>",
  71. messageSends: [],
  72. referencedClasses: []
  73. }),
  74. smalltalk.Number);
  75. smalltalk.addMethod(
  76. "_jsbenchmark",
  77. smalltalk.method({
  78. selector: "jsbenchmark",
  79. category: '*Benchfib',
  80. fn: function (){
  81. var self=this;
  82. var size = 8190;
  83. var count;
  84. for (var z=0;z<this;z++) {
  85. count = 0;
  86. var flags = new Array();
  87. for (var p=0; p<size; p++) {
  88. flags[p] = true;
  89. }
  90. for (var i=1;i<=size;i++) {
  91. if (flags[i-1]) {
  92. var prime = i+1;
  93. var k = i + prime;
  94. while (k <= size) {
  95. flags[k-1] = false;
  96. k = k + prime;
  97. }
  98. count = count + 1;
  99. }
  100. }
  101. }
  102. return count;
  103. return self;},
  104. args: [],
  105. source: "jsbenchmark\x0a\x0a<\x0avar size = 8190;\x0avar count;\x0afor (var z=0;z<this;z++) {\x0a count = 0;\x0a var flags = new Array();\x0a for (var p=0; p<size; p++) {\x0a flags[p] = true;\x0a }\x0a for (var i=1;i<=size;i++) {\x0a if (flags[i-1]) {\x0a var prime = i+1;\x0a var k = i + prime;\x0a while (k <= size) {\x0a flags[k-1] = false;\x0a k = k + prime;\x0a }\x0a count = count + 1;\x0a }\x0a }\x0a}\x0areturn count>",
  106. messageSends: [],
  107. referencedClasses: []
  108. }),
  109. smalltalk.Number);
  110. smalltalk.addMethod(
  111. "_jstinyBenchmarks",
  112. smalltalk.method({
  113. selector: "jstinyBenchmarks",
  114. category: '*Benchfib',
  115. fn: function (){
  116. var self=this;
  117. var t1=nil;
  118. var t2=nil;
  119. var r=nil;
  120. var n1=nil;
  121. var n2=nil;
  122. (n1=(1));
  123. (function(){while((function(){(t1=smalltalk.send((smalltalk.Date || Date), "_millisecondsToRun_", [(function(){return smalltalk.send(n1, "_jsbenchmark", []);})]));return ((($receiver = t1).klass === smalltalk.Number) ? $receiver <(1000) : smalltalk.send($receiver, "__lt", [(1000)]));})()) {(function(){return (n1=((($receiver = n1).klass === smalltalk.Number) ? $receiver *(2) : smalltalk.send($receiver, "__star", [(2)])));})()}})();
  124. (n2=(28));
  125. (function(){while((function(){(t2=smalltalk.send((smalltalk.Date || Date), "_millisecondsToRun_", [(function(){return (r=smalltalk.send(n2, "_jsbenchFib", []));})]));return ((($receiver = t2).klass === smalltalk.Number) ? $receiver <(1000) : smalltalk.send($receiver, "__lt", [(1000)]));})()) {(function(){return (n2=((($receiver = n2).klass === smalltalk.Number) ? $receiver +(1) : smalltalk.send($receiver, "__plus", [(1)])));})()}})();
  126. return smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send(((($receiver = ((($receiver = ((($receiver = n1).klass === smalltalk.Number) ? $receiver *(500000) : smalltalk.send($receiver, "__star", [(500000)]))).klass === smalltalk.Number) ? $receiver *(1000) : smalltalk.send($receiver, "__star", [(1000)]))).klass === smalltalk.Number) ? $receiver /t1 : smalltalk.send($receiver, "__slash", [t1])), "_printString", []), "__comma", [" bytecodes/sec; "]), "__comma", [smalltalk.send(((($receiver = ((($receiver = r).klass === smalltalk.Number) ? $receiver *(1000) : smalltalk.send($receiver, "__star", [(1000)]))).klass === smalltalk.Number) ? $receiver /t2 : smalltalk.send($receiver, "__slash", [t2])), "_printString", [])]), "__comma", [" sends/sec"]);
  127. return self;},
  128. args: [],
  129. source: "jstinyBenchmarks\x0a\x09\x220 jstinyBenchmarks\x22\x0a\x0a\x09| t1 t2 r n1 n2 |\x0a\x09n1 := 1.\x0a\x09[t1 := Date millisecondsToRun: [n1 jsbenchmark].\x0a\x09t1 < 1000] whileTrue:[n1 := n1 * 2]. \x22Note: #benchmark's runtime is about O(n)\x22\x0a\x0a\x09n2 := 28.\x0a\x09[t2 := Date millisecondsToRun: [r := n2 jsbenchFib].\x0a\x09t2 < 1000] whileTrue:[n2 := n2 + 1]. \x0a\x09\x22Note: #jsbenchFib's runtime is about O(k^n),\x0a\x09\x09where k is the golden number = (1 + 5 sqrt) / 2 = 1.618....\x22\x0a\x0a\x09^ ((n1 * 500000 * 1000) / t1) printString, ' bytecodes/sec; ',\x0a\x09 ((r * 1000) / t2) printString, ' sends/sec'",
  130. messageSends: ["whileTrue:", "millisecondsToRun:", "jsbenchmark", "<", "*", "jsbenchFib", "+", ",", "printString", "/"],
  131. referencedClasses: ["Date"]
  132. }),
  133. smalltalk.Number);
  134. smalltalk.addMethod(
  135. "_tinyBenchmarks",
  136. smalltalk.method({
  137. selector: "tinyBenchmarks",
  138. category: '*Benchfib',
  139. fn: function (){
  140. var self=this;
  141. var t1=nil;
  142. var t2=nil;
  143. var r=nil;
  144. var n1=nil;
  145. var n2=nil;
  146. (n1=(1));
  147. (function(){while((function(){(t1=smalltalk.send((smalltalk.Date || Date), "_millisecondsToRun_", [(function(){return smalltalk.send(n1, "_benchmark", []);})]));return ((($receiver = t1).klass === smalltalk.Number) ? $receiver <(1000) : smalltalk.send($receiver, "__lt", [(1000)]));})()) {(function(){return (n1=((($receiver = n1).klass === smalltalk.Number) ? $receiver *(2) : smalltalk.send($receiver, "__star", [(2)])));})()}})();
  148. (n2=(16));
  149. (function(){while((function(){(t2=smalltalk.send((smalltalk.Date || Date), "_millisecondsToRun_", [(function(){return (r=smalltalk.send(n2, "_benchFib", []));})]));return ((($receiver = t2).klass === smalltalk.Number) ? $receiver <(1000) : smalltalk.send($receiver, "__lt", [(1000)]));})()) {(function(){return (n2=((($receiver = n2).klass === smalltalk.Number) ? $receiver +(1) : smalltalk.send($receiver, "__plus", [(1)])));})()}})();
  150. return smalltalk.send(smalltalk.send(smalltalk.send(smalltalk.send(((($receiver = ((($receiver = ((($receiver = n1).klass === smalltalk.Number) ? $receiver *(500000) : smalltalk.send($receiver, "__star", [(500000)]))).klass === smalltalk.Number) ? $receiver *(1000) : smalltalk.send($receiver, "__star", [(1000)]))).klass === smalltalk.Number) ? $receiver /t1 : smalltalk.send($receiver, "__slash", [t1])), "_printString", []), "__comma", [" bytecodes/sec; "]), "__comma", [smalltalk.send(((($receiver = ((($receiver = r).klass === smalltalk.Number) ? $receiver *(1000) : smalltalk.send($receiver, "__star", [(1000)]))).klass === smalltalk.Number) ? $receiver /t2 : smalltalk.send($receiver, "__slash", [t2])), "_printString", [])]), "__comma", [" sends/sec"]);
  151. return self;},
  152. args: [],
  153. source: "tinyBenchmarks \x0a\x09\x22Report the results of running the two tiny Squeak benchmarks.\x0a\x09ar 9/10/1999: Adjusted to run at least 1 sec to get more stable results\x22\x0a\x09\x220 tinyBenchmarks\x22\x0a\x09\x22On a 292 MHz G3 Mac: 22727272 bytecodes/sec; 984169 sends/sec\x22\x0a\x09\x22On a 400 MHz PII/Win98: 18028169 bytecodes/sec; 1081272 sends/sec\x22\x0a\x09| t1 t2 r n1 n2 |\x0a\x09n1 := 1.\x0a\x09[t1 := Date millisecondsToRun: [n1 benchmark].\x0a\x09t1 < 1000] whileTrue:[n1 := n1 * 2]. \x22Note: #benchmark's runtime is about O(n)\x22\x0a\x0a\x09n2 := 16.\x0a\x09[t2 := Date millisecondsToRun: [r := n2 benchFib].\x0a\x09t2 < 1000] whileTrue:[n2 := n2 + 1]. \x0a\x09\x22Note: #benchFib's runtime is about O(k^n),\x0a\x09\x09where k is the golden number = (1 + 5 sqrt) / 2 = 1.618....\x22\x0a\x0a\x09^ ((n1 * 500000 * 1000) / t1) printString, ' bytecodes/sec; ',\x0a\x09 ((r * 1000) / t2) printString, ' sends/sec'",
  154. messageSends: ["whileTrue:", "millisecondsToRun:", "benchmark", "<", "*", "benchFib", "+", ",", "printString", "/"],
  155. referencedClasses: ["Date"]
  156. }),
  157. smalltalk.Number);