Benchfib.js 12 KB

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