1
0

Benchfib.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. define("amber_core/Benchfib", ["amber_vm/smalltalk", "amber_vm/nil", "amber_vm/_st", "amber_core/Kernel-Objects"], function(smalltalk,nil,_st){
  2. smalltalk.addPackage('Benchfib');
  3. smalltalk.packages["Benchfib"].transport = {"type":"amd","amdNamespace":"amber_core"};
  4. smalltalk.addClass('Benchfib', smalltalk.Object, [], 'Benchfib');
  5. smalltalk.addMethod(
  6. smalltalk.method({
  7. selector: "main",
  8. category: 'not yet classified',
  9. fn: function (){
  10. var self=this;
  11. var result;
  12. return smalltalk.withContext(function($ctx1) {
  13. result=(0)._tinyBenchmarks();
  14. _st(console)._log_("0 tinyBenchmarks => ".__comma(result));
  15. return self}, function($ctx1) {$ctx1.fill(self,"main",{result:result},smalltalk.Benchfib.klass)})},
  16. args: [],
  17. source: "main\x0a\x0a\x09| result |\x0a\x09result := 0 tinyBenchmarks.\x0a\x09console log: '0 tinyBenchmarks => ' , result",
  18. messageSends: ["tinyBenchmarks", "log:", ","],
  19. referencedClasses: []
  20. }),
  21. smalltalk.Benchfib.klass);
  22. smalltalk.addMethod(
  23. smalltalk.method({
  24. selector: "benchFib",
  25. category: '*Benchfib',
  26. fn: function (){
  27. var self=this;
  28. return smalltalk.withContext(function($ctx1) {
  29. var $2,$5,$4,$7,$6,$3,$1;
  30. $2=self.__lt((2));
  31. if(smalltalk.assert($2)){
  32. $1=(1);
  33. } else {
  34. $5=self.__minus((1));
  35. $ctx1.sendIdx["-"]=1;
  36. $4=_st($5)._benchFib();
  37. $ctx1.sendIdx["benchFib"]=1;
  38. $7=self.__minus((2));
  39. $6=_st($7)._benchFib();
  40. $3=_st($4).__plus($6);
  41. $1=_st($3).__plus((1));
  42. $ctx1.sendIdx["+"]=1;
  43. };
  44. return $1;
  45. }, function($ctx1) {$ctx1.fill(self,"benchFib",{},smalltalk.Number)})},
  46. args: [],
  47. 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]",
  48. messageSends: ["ifTrue:ifFalse:", "<", "+", "benchFib", "-"],
  49. referencedClasses: []
  50. }),
  51. smalltalk.Number);
  52. smalltalk.addMethod(
  53. smalltalk.method({
  54. selector: "benchmark",
  55. category: '*Benchfib',
  56. fn: function (){
  57. var self=this;
  58. var size,flags,prime,k,count;
  59. function $Array(){return smalltalk.Array||(typeof Array=="undefined"?nil:Array)}
  60. return smalltalk.withContext(function($ctx1) {
  61. var $2,$1,$3;
  62. size=(8190);
  63. $1=(1)._to_do_(self,(function(iter){
  64. return smalltalk.withContext(function($ctx2) {
  65. count=(0);
  66. count;
  67. flags=_st($Array())._new();
  68. flags;
  69. _st(size)._timesRepeat_((function(){
  70. return smalltalk.withContext(function($ctx3) {
  71. return _st(flags)._add_(true);
  72. }, function($ctx3) {$ctx3.fillBlock({},$ctx2,2)})}));
  73. return (1)._to_do_(size,(function(i){
  74. return smalltalk.withContext(function($ctx3) {
  75. $2=_st(flags)._at_(i);
  76. if(smalltalk.assert($2)){
  77. prime=_st(i).__plus((1));
  78. $ctx3.sendIdx["+"]=1;
  79. prime;
  80. k=_st(i).__plus(prime);
  81. $ctx3.sendIdx["+"]=2;
  82. k;
  83. _st((function(){
  84. return smalltalk.withContext(function($ctx4) {
  85. return _st(k).__lt_eq(size);
  86. }, function($ctx4) {$ctx4.fillBlock({},$ctx3,5)})}))._whileTrue_((function(){
  87. return smalltalk.withContext(function($ctx4) {
  88. _st(flags)._at_put_(k,false);
  89. k=_st(k).__plus(prime);
  90. $ctx4.sendIdx["+"]=3;
  91. return k;
  92. }, function($ctx4) {$ctx4.fillBlock({},$ctx3,6)})}));
  93. count=_st(count).__plus((1));
  94. return count;
  95. };
  96. }, function($ctx3) {$ctx3.fillBlock({i:i},$ctx2,3)})}));
  97. }, function($ctx2) {$ctx2.fillBlock({iter:iter},$ctx1,1)})}));
  98. $ctx1.sendIdx["to:do:"]=1;
  99. $3=count;
  100. return $3;
  101. }, function($ctx1) {$ctx1.fill(self,"benchmark",{size:size,flags:flags,prime:prime,k:k,count:count},smalltalk.Number)})},
  102. args: [],
  103. 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",
  104. messageSends: ["to:do:", "new", "timesRepeat:", "add:", "ifTrue:", "at:", "+", "whileTrue:", "<=", "at:put:"],
  105. referencedClasses: ["Array"]
  106. }),
  107. smalltalk.Number);
  108. smalltalk.addMethod(
  109. smalltalk.method({
  110. selector: "jsbenchFib",
  111. category: '*Benchfib',
  112. fn: function (){
  113. var self=this;
  114. return smalltalk.withContext(function($ctx1) {
  115. if (this < 2) {
  116. return 1;
  117. } else {
  118. return (this-1)._jsbenchFib() + (this-2)._jsbenchFib() + 1;};
  119. return self}, function($ctx1) {$ctx1.fill(self,"jsbenchFib",{},smalltalk.Number)})},
  120. args: [],
  121. source: "jsbenchFib\x0a\x0a\x09<if (this < 2) {\x0areturn 1;\x0a} else {\x0areturn (this-1)._jsbenchFib() + (this-2)._jsbenchFib() + 1;}>",
  122. messageSends: [],
  123. referencedClasses: []
  124. }),
  125. smalltalk.Number);
  126. smalltalk.addMethod(
  127. smalltalk.method({
  128. selector: "jsbenchmark",
  129. category: '*Benchfib',
  130. fn: function (){
  131. var self=this;
  132. return smalltalk.withContext(function($ctx1) {
  133. var size = 8190;
  134. var count;
  135. for (var z=0;z<this;z++) {
  136. count = 0;
  137. var flags = new Array();
  138. for (var p=0; p<size; p++) {
  139. flags[p] = true;
  140. }
  141. for (var i=1;i<=size;i++) {
  142. if (flags[i-1]) {
  143. var prime = i+1;
  144. var k = i + prime;
  145. while (k <= size) {
  146. flags[k-1] = false;
  147. k = k + prime;
  148. }
  149. count = count + 1;
  150. }
  151. }
  152. }
  153. return count;
  154. return self}, function($ctx1) {$ctx1.fill(self,"jsbenchmark",{},smalltalk.Number)})},
  155. args: [],
  156. 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>",
  157. messageSends: [],
  158. referencedClasses: []
  159. }),
  160. smalltalk.Number);
  161. smalltalk.addMethod(
  162. smalltalk.method({
  163. selector: "jstinyBenchmarks",
  164. category: '*Benchfib',
  165. fn: function (){
  166. var self=this;
  167. var t1,t2,r,n1,n2;
  168. function $Date(){return smalltalk.Date||(typeof Date=="undefined"?nil:Date)}
  169. return smalltalk.withContext(function($ctx1) {
  170. var $1,$2,$9,$8,$7,$6,$5,$12,$11,$10,$4,$3;
  171. n1=(1);
  172. $1=_st((function(){
  173. return smalltalk.withContext(function($ctx2) {
  174. t1=_st($Date())._millisecondsToRun_((function(){
  175. return smalltalk.withContext(function($ctx3) {
  176. return _st(n1)._jsbenchmark();
  177. }, function($ctx3) {$ctx3.fillBlock({},$ctx2,2)})}));
  178. $ctx2.sendIdx["millisecondsToRun:"]=1;
  179. t1;
  180. return _st(t1).__lt((1000));
  181. $ctx2.sendIdx["<"]=1;
  182. }, function($ctx2) {$ctx2.fillBlock({},$ctx1,1)})}))._whileTrue_((function(){
  183. return smalltalk.withContext(function($ctx2) {
  184. n1=_st(n1).__star((2));
  185. $ctx2.sendIdx["*"]=1;
  186. return n1;
  187. }, function($ctx2) {$ctx2.fillBlock({},$ctx1,3)})}));
  188. $ctx1.sendIdx["whileTrue:"]=1;
  189. n2=(28);
  190. $2=_st((function(){
  191. return smalltalk.withContext(function($ctx2) {
  192. t2=_st($Date())._millisecondsToRun_((function(){
  193. return smalltalk.withContext(function($ctx3) {
  194. r=_st(n2)._jsbenchFib();
  195. return r;
  196. }, function($ctx3) {$ctx3.fillBlock({},$ctx2,5)})}));
  197. t2;
  198. return _st(t2).__lt((1000));
  199. }, function($ctx2) {$ctx2.fillBlock({},$ctx1,4)})}))._whileTrue_((function(){
  200. return smalltalk.withContext(function($ctx2) {
  201. n2=_st(n2).__plus((1));
  202. return n2;
  203. }, function($ctx2) {$ctx2.fillBlock({},$ctx1,6)})}));
  204. $9=_st(n1).__star((500000));
  205. $ctx1.sendIdx["*"]=3;
  206. $8=_st($9).__star((1000));
  207. $ctx1.sendIdx["*"]=2;
  208. $7=_st($8).__slash(t1);
  209. $ctx1.sendIdx["/"]=1;
  210. $6=_st($7)._printString();
  211. $ctx1.sendIdx["printString"]=1;
  212. $5=_st($6).__comma(" bytecodes/sec; ");
  213. $12=_st(r).__star((1000));
  214. $11=_st($12).__slash(t2);
  215. $10=_st($11)._printString();
  216. $4=_st($5).__comma($10);
  217. $ctx1.sendIdx[","]=2;
  218. $3=_st($4).__comma(" sends/sec");
  219. $ctx1.sendIdx[","]=1;
  220. return $3;
  221. }, function($ctx1) {$ctx1.fill(self,"jstinyBenchmarks",{t1:t1,t2:t2,r:r,n1:n1,n2:n2},smalltalk.Number)})},
  222. args: [],
  223. 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'",
  224. messageSends: ["whileTrue:", "millisecondsToRun:", "jsbenchmark", "<", "*", "jsbenchFib", "+", ",", "printString", "/"],
  225. referencedClasses: ["Date"]
  226. }),
  227. smalltalk.Number);
  228. smalltalk.addMethod(
  229. smalltalk.method({
  230. selector: "tinyBenchmarks",
  231. category: '*Benchfib',
  232. fn: function (){
  233. var self=this;
  234. var t1,t2,r,n1,n2;
  235. function $Date(){return smalltalk.Date||(typeof Date=="undefined"?nil:Date)}
  236. return smalltalk.withContext(function($ctx1) {
  237. var $1,$2,$9,$8,$7,$6,$5,$12,$11,$10,$4,$3;
  238. n1=(1);
  239. $1=_st((function(){
  240. return smalltalk.withContext(function($ctx2) {
  241. t1=_st($Date())._millisecondsToRun_((function(){
  242. return smalltalk.withContext(function($ctx3) {
  243. return _st(n1)._benchmark();
  244. }, function($ctx3) {$ctx3.fillBlock({},$ctx2,2)})}));
  245. $ctx2.sendIdx["millisecondsToRun:"]=1;
  246. t1;
  247. return _st(t1).__lt((1000));
  248. $ctx2.sendIdx["<"]=1;
  249. }, function($ctx2) {$ctx2.fillBlock({},$ctx1,1)})}))._whileTrue_((function(){
  250. return smalltalk.withContext(function($ctx2) {
  251. n1=_st(n1).__star((2));
  252. $ctx2.sendIdx["*"]=1;
  253. return n1;
  254. }, function($ctx2) {$ctx2.fillBlock({},$ctx1,3)})}));
  255. $ctx1.sendIdx["whileTrue:"]=1;
  256. n2=(16);
  257. $2=_st((function(){
  258. return smalltalk.withContext(function($ctx2) {
  259. t2=_st($Date())._millisecondsToRun_((function(){
  260. return smalltalk.withContext(function($ctx3) {
  261. r=_st(n2)._benchFib();
  262. return r;
  263. }, function($ctx3) {$ctx3.fillBlock({},$ctx2,5)})}));
  264. t2;
  265. return _st(t2).__lt((1000));
  266. }, function($ctx2) {$ctx2.fillBlock({},$ctx1,4)})}))._whileTrue_((function(){
  267. return smalltalk.withContext(function($ctx2) {
  268. n2=_st(n2).__plus((1));
  269. return n2;
  270. }, function($ctx2) {$ctx2.fillBlock({},$ctx1,6)})}));
  271. $9=_st(n1).__star((500000));
  272. $ctx1.sendIdx["*"]=3;
  273. $8=_st($9).__star((1000));
  274. $ctx1.sendIdx["*"]=2;
  275. $7=_st($8).__slash(t1);
  276. $ctx1.sendIdx["/"]=1;
  277. $6=_st($7)._printString();
  278. $ctx1.sendIdx["printString"]=1;
  279. $5=_st($6).__comma(" bytecodes/sec; ");
  280. $12=_st(r).__star((1000));
  281. $11=_st($12).__slash(t2);
  282. $10=_st($11)._printString();
  283. $4=_st($5).__comma($10);
  284. $ctx1.sendIdx[","]=2;
  285. $3=_st($4).__comma(" sends/sec");
  286. $ctx1.sendIdx[","]=1;
  287. return $3;
  288. }, function($ctx1) {$ctx1.fill(self,"tinyBenchmarks",{t1:t1,t2:t2,r:r,n1:n1,n2:n2},smalltalk.Number)})},
  289. args: [],
  290. 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'",
  291. messageSends: ["whileTrue:", "millisecondsToRun:", "benchmark", "<", "*", "benchFib", "+", ",", "printString", "/"],
  292. referencedClasses: ["Date"]
  293. }),
  294. smalltalk.Number);
  295. });