1
0

Benchfib.js 11 KB

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