Benchfib.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. smalltalk.addPackage('Benchfib');
  2. smalltalk.addClass('Benchfib', smalltalk.Object, [], 'Benchfib');
  3. smalltalk.addMethod(
  4. smalltalk.method({
  5. selector: "main",
  6. category: 'not yet classified',
  7. fn: function (){
  8. var self=this;
  9. var result;
  10. return smalltalk.withContext(function($ctx1) {
  11. result=(0)._tinyBenchmarks();
  12. _st(console)._log_("0 tinyBenchmarks => ".__comma(result));
  13. return self}, function($ctx1) {$ctx1.fill(self,"main",{result:result},smalltalk.Benchfib.klass)})},
  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. smalltalk.method({
  22. selector: "benchFib",
  23. category: '*Benchfib',
  24. fn: function (){
  25. var self=this;
  26. return smalltalk.withContext(function($ctx1) {
  27. var $2,$1;
  28. $2=self.__lt((2));
  29. if(smalltalk.assert($2)){
  30. $1=(1);
  31. } else {
  32. $1=_st(_st(_st(self.__minus((1)))._benchFib()).__plus(_st(self.__minus((2)))._benchFib())).__plus((1));
  33. };
  34. return $1;
  35. }, function($ctx1) {$ctx1.fill(self,"benchFib",{},smalltalk.Number)})},
  36. args: [],
  37. source: "benchFib\x0a\x09\x22Handy send-heavy benchmark\x22\x0a\x09\x22(result // seconds to run) = approx calls per second\x22\x0a\x09\x22\x09| r t |\x0a\x09\x09t := Time millisecondsToRun: [r := 26 benchFib].\x0a\x09\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]",
  38. messageSends: ["ifTrue:ifFalse:", "+", "benchFib", "-", "<"],
  39. referencedClasses: []
  40. }),
  41. smalltalk.Number);
  42. smalltalk.addMethod(
  43. smalltalk.method({
  44. selector: "benchmark",
  45. category: '*Benchfib',
  46. fn: function (){
  47. var self=this;
  48. var size,flags,prime,k,count;
  49. function $Array(){return smalltalk.Array||(typeof Array=="undefined"?nil:Array)}
  50. return smalltalk.withContext(function($ctx1) {
  51. var $1,$2;
  52. size=(8190);
  53. (1)._to_do_(self,(function(iter){
  54. return smalltalk.withContext(function($ctx2) {
  55. count=(0);
  56. count;
  57. flags=_st($Array())._new();
  58. flags;
  59. _st(size)._timesRepeat_((function(){
  60. return smalltalk.withContext(function($ctx3) {
  61. return _st(flags)._add_(true);
  62. }, function($ctx3) {$ctx3.fillBlock({},$ctx2)})}));
  63. return (1)._to_do_(size,(function(i){
  64. return smalltalk.withContext(function($ctx3) {
  65. $1=_st(flags)._at_(i);
  66. if(smalltalk.assert($1)){
  67. prime=_st(i).__plus((1));
  68. prime;
  69. k=_st(i).__plus(prime);
  70. k;
  71. _st((function(){
  72. return smalltalk.withContext(function($ctx4) {
  73. return _st(k).__lt_eq(size);
  74. }, function($ctx4) {$ctx4.fillBlock({},$ctx3)})}))._whileTrue_((function(){
  75. return smalltalk.withContext(function($ctx4) {
  76. _st(flags)._at_put_(k,false);
  77. k=_st(k).__plus(prime);
  78. return k;
  79. }, function($ctx4) {$ctx4.fillBlock({},$ctx3)})}));
  80. count=_st(count).__plus((1));
  81. return count;
  82. };
  83. }, function($ctx3) {$ctx3.fillBlock({i:i},$ctx2)})}));
  84. }, function($ctx2) {$ctx2.fillBlock({iter:iter},$ctx1)})}));
  85. $2=count;
  86. return $2;
  87. }, function($ctx1) {$ctx1.fill(self,"benchmark",{size:size,flags:flags,prime:prime,k:k,count:count},smalltalk.Number)})},
  88. args: [],
  89. 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\x09| size flags prime k count |\x0a\x09size := 8190.\x0a\x091 to: self do:\x0a\x09\x09[:iter |\x0a\x09\x09count := 0.\x0a\x09\x09flags := Array new.\x0a\x09\x09size timesRepeat: [ flags add: true].\x0a\x09\x091 to: size do:\x0a\x09\x09\x09[:i | (flags at: i) ifTrue:\x0a\x09\x09\x09\x09[prime := i+1.\x0a\x09\x09\x09\x09k := i + prime.\x0a\x09\x09\x09\x09[k <= size] whileTrue:\x0a\x09\x09\x09\x09\x09[flags at: k put: false.\x0a\x09\x09\x09\x09\x09k := k + prime].\x0a\x09\x09\x09\x09count := count + 1]]].\x0a\x09^ count",
  90. messageSends: ["to:do:", "new", "timesRepeat:", "add:", "ifTrue:", "+", "whileTrue:", "at:put:", "<=", "at:"],
  91. referencedClasses: ["Array"]
  92. }),
  93. smalltalk.Number);
  94. smalltalk.addMethod(
  95. smalltalk.method({
  96. selector: "jsbenchFib",
  97. category: '*Benchfib',
  98. fn: function (){
  99. var self=this;
  100. return smalltalk.withContext(function($ctx1) {
  101. if (this < 2) {
  102. return 1;
  103. } else {
  104. return (this-1)._jsbenchFib() + (this-2)._jsbenchFib() + 1;};
  105. return self}, function($ctx1) {$ctx1.fill(self,"jsbenchFib",{},smalltalk.Number)})},
  106. args: [],
  107. source: "jsbenchFib\x0a\x0a\x09<if (this < 2) {\x0areturn 1;\x0a} else {\x0areturn (this-1)._jsbenchFib() + (this-2)._jsbenchFib() + 1;}>",
  108. messageSends: [],
  109. referencedClasses: []
  110. }),
  111. smalltalk.Number);
  112. smalltalk.addMethod(
  113. smalltalk.method({
  114. selector: "jsbenchmark",
  115. category: '*Benchfib',
  116. fn: function (){
  117. var self=this;
  118. return smalltalk.withContext(function($ctx1) {
  119. var size = 8190;
  120. var count;
  121. for (var z=0;z<this;z++) {
  122. count = 0;
  123. var flags = new Array();
  124. for (var p=0; p<size; p++) {
  125. flags[p] = true;
  126. }
  127. for (var i=1;i<=size;i++) {
  128. if (flags[i-1]) {
  129. var prime = i+1;
  130. var k = i + prime;
  131. while (k <= size) {
  132. flags[k-1] = false;
  133. k = k + prime;
  134. }
  135. count = count + 1;
  136. }
  137. }
  138. }
  139. return count;
  140. return self}, function($ctx1) {$ctx1.fill(self,"jsbenchmark",{},smalltalk.Number)})},
  141. args: [],
  142. source: "jsbenchmark\x0a\x0a<\x0avar size = 8190;\x0avar count;\x0afor (var z=0;z<this;z++) {\x0a\x09count = 0;\x0a\x09var flags = new Array();\x0a\x09for (var p=0; p<size; p++) {\x0a\x09flags[p] = true;\x0a\x09}\x0a\x09for (var i=1;i<=size;i++) {\x0a\x09\x09if (flags[i-1]) {\x0a\x09\x09\x09var prime = i+1;\x0a\x09\x09\x09var k = i + prime;\x0a\x09\x09\x09while (k <= size) {\x0a\x09\x09\x09\x09flags[k-1] = false;\x0a\x09\x09\x09\x09k = k + prime;\x0a\x09\x09\x09}\x0a\x09\x09\x09count = count + 1;\x0a\x09\x09}\x0a\x09}\x0a}\x0areturn count>",
  143. messageSends: [],
  144. referencedClasses: []
  145. }),
  146. smalltalk.Number);
  147. smalltalk.addMethod(
  148. smalltalk.method({
  149. selector: "jstinyBenchmarks",
  150. category: '*Benchfib',
  151. fn: function (){
  152. var self=this;
  153. var t1,t2,r,n1,n2;
  154. function $Date(){return smalltalk.Date||(typeof Date=="undefined"?nil:Date)}
  155. return smalltalk.withContext(function($ctx1) {
  156. var $1;
  157. n1=(1);
  158. _st((function(){
  159. return smalltalk.withContext(function($ctx2) {
  160. t1=_st($Date())._millisecondsToRun_((function(){
  161. return smalltalk.withContext(function($ctx3) {
  162. return _st(n1)._jsbenchmark();
  163. }, function($ctx3) {$ctx3.fillBlock({},$ctx2)})}));
  164. t1;
  165. return _st(t1).__lt((1000));
  166. }, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}))._whileTrue_((function(){
  167. return smalltalk.withContext(function($ctx2) {
  168. n1=_st(n1).__star((2));
  169. return n1;
  170. }, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}));
  171. n2=(28);
  172. _st((function(){
  173. return smalltalk.withContext(function($ctx2) {
  174. t2=_st($Date())._millisecondsToRun_((function(){
  175. return smalltalk.withContext(function($ctx3) {
  176. r=_st(n2)._jsbenchFib();
  177. return r;
  178. }, function($ctx3) {$ctx3.fillBlock({},$ctx2)})}));
  179. t2;
  180. return _st(t2).__lt((1000));
  181. }, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}))._whileTrue_((function(){
  182. return smalltalk.withContext(function($ctx2) {
  183. n2=_st(n2).__plus((1));
  184. return n2;
  185. }, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}));
  186. $1=_st(_st(_st(_st(_st(_st(_st(n1).__star((500000))).__star((1000))).__slash(t1))._printString()).__comma(" bytecodes/sec; ")).__comma(_st(_st(_st(r).__star((1000))).__slash(t2))._printString())).__comma(" sends/sec");
  187. return $1;
  188. }, function($ctx1) {$ctx1.fill(self,"jstinyBenchmarks",{t1:t1,t2:t2,r:r,n1:n1,n2:n2},smalltalk.Number)})},
  189. args: [],
  190. 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\x09((r * 1000) / t2) printString, ' sends/sec'",
  191. messageSends: ["whileTrue:", "*", "millisecondsToRun:", "jsbenchmark", "<", "+", "jsbenchFib", ",", "printString", "/"],
  192. referencedClasses: ["Date"]
  193. }),
  194. smalltalk.Number);
  195. smalltalk.addMethod(
  196. smalltalk.method({
  197. selector: "tinyBenchmarks",
  198. category: '*Benchfib',
  199. fn: function (){
  200. var self=this;
  201. var t1,t2,r,n1,n2;
  202. function $Date(){return smalltalk.Date||(typeof Date=="undefined"?nil:Date)}
  203. return smalltalk.withContext(function($ctx1) {
  204. var $1;
  205. n1=(1);
  206. _st((function(){
  207. return smalltalk.withContext(function($ctx2) {
  208. t1=_st($Date())._millisecondsToRun_((function(){
  209. return smalltalk.withContext(function($ctx3) {
  210. return _st(n1)._benchmark();
  211. }, function($ctx3) {$ctx3.fillBlock({},$ctx2)})}));
  212. t1;
  213. return _st(t1).__lt((1000));
  214. }, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}))._whileTrue_((function(){
  215. return smalltalk.withContext(function($ctx2) {
  216. n1=_st(n1).__star((2));
  217. return n1;
  218. }, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}));
  219. n2=(16);
  220. _st((function(){
  221. return smalltalk.withContext(function($ctx2) {
  222. t2=_st($Date())._millisecondsToRun_((function(){
  223. return smalltalk.withContext(function($ctx3) {
  224. r=_st(n2)._benchFib();
  225. return r;
  226. }, function($ctx3) {$ctx3.fillBlock({},$ctx2)})}));
  227. t2;
  228. return _st(t2).__lt((1000));
  229. }, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}))._whileTrue_((function(){
  230. return smalltalk.withContext(function($ctx2) {
  231. n2=_st(n2).__plus((1));
  232. return n2;
  233. }, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}));
  234. $1=_st(_st(_st(_st(_st(_st(_st(n1).__star((500000))).__star((1000))).__slash(t1))._printString()).__comma(" bytecodes/sec; ")).__comma(_st(_st(_st(r).__star((1000))).__slash(t2))._printString())).__comma(" sends/sec");
  235. return $1;
  236. }, function($ctx1) {$ctx1.fill(self,"tinyBenchmarks",{t1:t1,t2:t2,r:r,n1:n1,n2:n2},smalltalk.Number)})},
  237. args: [],
  238. 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\x09((r * 1000) / t2) printString, ' sends/sec'",
  239. messageSends: ["whileTrue:", "*", "millisecondsToRun:", "benchmark", "<", "+", "benchFib", ",", "printString", "/"],
  240. referencedClasses: ["Date"]
  241. }),
  242. smalltalk.Number);