Benchfib.js 9.1 KB

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