Pystone.st 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. Object subclass: #PyStoneRecord
  2. instanceVariableNames: 'ptrComp discr enumComp intComp stringComp'
  3. package: 'Pystone'!
  4. !PyStoneRecord commentStamp!
  5. Record class used in Pystone benchmark.!
  6. !PyStoneRecord methodsFor: 'accessing'!
  7. discr
  8. ^discr
  9. !
  10. discr: p
  11. discr := p
  12. !
  13. enumComp
  14. ^enumComp
  15. !
  16. enumComp: p
  17. enumComp := p
  18. !
  19. intComp
  20. ^intComp
  21. !
  22. intComp: p
  23. intComp := p
  24. !
  25. ptrComp
  26. ^ptrComp
  27. !
  28. ptrComp: p
  29. ptrComp := p
  30. !
  31. stringComp
  32. ^stringComp
  33. !
  34. stringComp: p
  35. stringComp := p
  36. ! !
  37. !PyStoneRecord methodsFor: 'copying'!
  38. copy
  39. ^PyStoneRecord ptrComp: ptrComp discr: discr enumComp: enumComp intComp: intComp stringComp: stringComp
  40. ! !
  41. !PyStoneRecord methodsFor: 'initialize-release'!
  42. ptrComp: p discr: d enumComp: e intComp: i stringComp: s
  43. ptrComp := p.
  44. discr := d.
  45. enumComp := e.
  46. intComp := i.
  47. stringComp := s
  48. ! !
  49. !PyStoneRecord class methodsFor: 'instance-creation'!
  50. new
  51. ^self ptrComp: nil discr: 0 enumComp: 0 intComp: 0 stringComp: 0
  52. !
  53. ptrComp: p discr: d enumComp: e intComp: i stringComp: s
  54. ^super new ptrComp: p discr: d enumComp: e intComp: i stringComp: s
  55. ! !
  56. Object subclass: #Pystone
  57. instanceVariableNames: 'nulltime ptrGlbNext ptrGlb ident1 ident3 ident2 ident4 ident5 ident6 intGlob boolGlob char1Glob char2Glob array1Glob array2Glob func3 func2 func1'
  58. package: 'Pystone'!
  59. !Pystone commentStamp!
  60. This is a straight translation of pystone 1.1 from Python to Squeak. Procedures have been mapped to instance side methods, functions have been mapped to blocks. Open a transcript and run:
  61. Pystone run!
  62. !Pystone methodsFor: 'as yet unclassified'!
  63. defineFunctions
  64. "Functions have been mapped to blocks, since that
  65. would be natural."
  66. func1 := [:charPar1 :charPar2 |
  67. | charLoc1 charLoc2 |
  68. charLoc1 := charPar1.
  69. charLoc2 := charLoc1.
  70. (charLoc2 = charPar2) ifTrue: [ident2] ifFalse: [ident1]].
  71. func2 := [:strParI1 :strParI2 |
  72. | intLoc charLoc |
  73. intLoc := 1.
  74. [intLoc <= 1] whileTrue: [
  75. ((func1 value: (strParI1 at: intLoc) value: (strParI1 at: intLoc + 1)) = ident1)
  76. ifTrue: [
  77. charLoc := 'A'.
  78. intLoc := intLoc + 1]].
  79. (charLoc >= 'W' and: [charLoc <= 'Z']) ifTrue: [
  80. intLoc := 7].
  81. (charLoc = 'X') ifTrue: [true] ifFalse: [
  82. (strParI1 > strParI2) ifTrue: [
  83. intLoc := intLoc + 7.
  84. true]
  85. ifFalse: [
  86. false]]].
  87. func3 := [:enumParIn |
  88. | enumLoc |
  89. enumLoc := enumParIn.
  90. enumLoc = ident3]
  91. !
  92. main: loops
  93. "Adaption of pystone.py version 1.9 from Python."
  94. ident1 := 1. ident2 := 2. ident3 := 3. ident4 := 4. ident5 := 5. ident6 := 6.
  95. intGlob := 0.
  96. boolGlob := false.
  97. char1Glob := String value: 0.
  98. char2Glob := String value: 0.
  99. array1Glob := Array new.
  100. 51 timesRepeat: [ array1Glob add: 0].
  101. array2Glob := ((1 to: 51) collect: [:i | array1Glob copy]) asArray.
  102. self defineFunctions.
  103. self pystones: loops block: [:benchtime :stones |
  104. self log: 'Pystone(1.1) time for ', loops asString, ' passes = ', benchtime asString.
  105. self log: 'This machine benchmarks at ',
  106. ((stones / 0.1) rounded * 0.1) asString, ' pystones/second']
  107. !
  108. log: aString
  109. (smalltalk at: #Transcript)
  110. ifNotNil: [
  111. Transcript show: aString;cr]
  112. ifNil: [
  113. console log: aString]
  114. !
  115. proc0: loops block: aBlock
  116. | string1Loc starttime intLoc1 intLoc2 string2Loc enumLoc intLoc3 charIndex benchtime |
  117. loops timesRepeat: [].
  118. benchtime := Date millisecondsToRun: [
  119. ptrGlbNext := PyStoneRecord new.
  120. ptrGlb := PyStoneRecord new.
  121. ptrGlb ptrComp: ptrGlbNext.
  122. ptrGlb discr: ident1.
  123. ptrGlb enumComp: ident3.
  124. ptrGlb intComp: 40.
  125. ptrGlb stringComp: 'DHRYSTONE PROGRAM, SOME STRING'.
  126. string1Loc := 'DHRYSTONE PROGRAM, 1''ST STRING'.
  127. (array2Glob at: 8) at: 7 put: 10.
  128. "1 to: loops - 1 do: [:i | Changed this to use timesRepeat: since i is not used at all in the loop"
  129. loops timesRepeat: [
  130. self proc5; proc4.
  131. intLoc1 := 2.
  132. intLoc2 := 3.
  133. string2Loc := 'DHRYSTONE PROGRAM, 2''ND STRING'.
  134. enumLoc := ident2.
  135. boolGlob := (func2 value: string1Loc value: string2Loc) not.
  136. [intLoc1 < intLoc2] whileTrue: [
  137. intLoc3 := 5 * intLoc1 - intLoc2.
  138. intLoc3 := self proc7: intLoc1 with: intLoc2.
  139. intLoc1 := intLoc1 + 1].
  140. self proc8:array1Glob with: array2Glob with: intLoc1 with: intLoc3.
  141. ptrGlb := self proc1: ptrGlb.
  142. charIndex := 'A'.
  143. [charIndex <= char2Glob] whileTrue: [
  144. (enumLoc = (func1 value: charIndex value: 'C'))
  145. ifTrue: [enumLoc := self proc6: ident1].
  146. charIndex := String value: (charIndex asciiValue + 1)].
  147. intLoc3 := intLoc2 * intLoc1.
  148. intLoc2 := intLoc3 / intLoc1.
  149. intLoc2 := 7 * (intLoc3 - intLoc2) - intLoc1.
  150. intLoc1 := self proc2: intLoc1]].
  151. ^ aBlock value: (benchtime / 1000) value: (loops / benchtime) * 1000
  152. !
  153. proc1: ptrParIn
  154. | nextRecord tmp |
  155. tmp := ptrParIn.
  156. nextRecord := ptrGlb copy.
  157. ptrParIn ptrComp: nextRecord.
  158. ptrParIn intComp: 5.
  159. nextRecord intComp: ptrParIn intComp.
  160. nextRecord ptrComp: ptrParIn ptrComp.
  161. nextRecord ptrComp: (self proc3: nextRecord ptrComp).
  162. (nextRecord discr = ident1) ifTrue: [
  163. nextRecord intComp: 6.
  164. nextRecord enumComp: (self proc6: ptrParIn enumComp).
  165. nextRecord ptrComp: ptrGlb ptrComp.
  166. nextRecord intComp: (self proc7: nextRecord intComp with: 10) ]
  167. ifFalse: [
  168. tmp := nextRecord copy].
  169. nextRecord ptrComp: nil.
  170. ^tmp
  171. !
  172. proc2: intParIO
  173. | tmp intLoc enumLoc |
  174. tmp := intParIO.
  175. intLoc := intParIO + 10.
  176. [true] whileTrue: [
  177. (char1Glob = 'A') ifTrue: [
  178. intLoc := intLoc - 1.
  179. tmp := intLoc - intGlob.
  180. enumLoc := ident1].
  181. (enumLoc = ident1) ifTrue: [
  182. ^ tmp]]
  183. !
  184. proc3: ptrParOut
  185. | tmp |
  186. tmp := ptrParOut.
  187. ptrGlb ifNotNil: [
  188. tmp := ptrGlb ptrComp]
  189. ifNil: [
  190. intGlob := 100].
  191. ptrGlb intComp: (self proc7: 10 with: intGlob).
  192. ^tmp
  193. !
  194. proc4
  195. | boolLoc |
  196. boolLoc := char1Glob = 'A'.
  197. boolLoc := boolLoc | boolGlob.
  198. char2Glob := 'B'
  199. !
  200. proc5
  201. char1Glob := 'A'.
  202. boolGlob := false
  203. !
  204. proc6: enumParIn
  205. | enumParOut |
  206. enumParOut := enumParIn.
  207. (func3 value: enumParIn) ifFalse: [
  208. enumParOut := ident4].
  209. (enumParIn = ident1) ifTrue: [
  210. enumParOut := ident1] ifFalse: [
  211. (enumParIn = ident2) ifTrue: [
  212. intGlob > 100 ifTrue: [
  213. enumParOut := ident1]
  214. ifFalse: [
  215. enumParOut := ident4]] ifFalse: [
  216. (enumParIn = ident3) ifTrue: [
  217. enumParOut := ident2] ifFalse: [
  218. (enumParIn = ident4) ifTrue: [] ifFalse: [
  219. (enumParIn = ident5) ifTrue: [
  220. enumParOut := ident3]]]]].
  221. ^enumParOut
  222. !
  223. proc7: intParI1 with: intParI2
  224. | intLoc intParOut |
  225. intLoc := intParI1 + 2.
  226. intParOut := intParI2 + intLoc.
  227. ^ intParOut
  228. !
  229. proc8: array1Par with: array2Par with: intParI1 with: intParI2
  230. | intLoc |
  231. intLoc := intParI1 + 5.
  232. array1Par at: intLoc put: intParI2.
  233. array1Par at: intLoc + 1 put: (array1Par at: intLoc).
  234. array1Par at: intLoc + 30 put: intLoc.
  235. intLoc to: intLoc + 1 do: [:intIndex |
  236. (array2Par at: intLoc) at: intIndex put: intLoc.
  237. (array2Par at: intLoc) at: intLoc - 1 put: ((array2Par at: intLoc) at: intLoc - 1) + 1.
  238. (array2Par at: intLoc + 20) at: intLoc put: (array1Par at: intLoc)].
  239. intGlob := 5
  240. !
  241. pystones: loops block: aBlock
  242. ^self proc0: loops block: aBlock
  243. ! !
  244. Pystone class instanceVariableNames: 'nulltime'!
  245. !Pystone class methodsFor: 'as yet unclassified'!
  246. main
  247. "self main"
  248. self run: 50000
  249. !
  250. run: loops
  251. "self run: 50000"
  252. self new main: loops
  253. ! !