1
|
1 |
"
|
5
|
2 |
COPYRIGHT (c) 1988 by Claus Gittinger
|
159
|
3 |
All Rights Reserved
|
1
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
|
12 |
|
|
13 |
Number subclass:#Integer
|
|
14 |
instanceVariableNames:''
|
49
|
15 |
classVariableNames:'DefaultDisplayRadix'
|
1
|
16 |
poolDictionaries:''
|
|
17 |
category:'Magnitude-Numbers'
|
|
18 |
!
|
|
19 |
|
|
20 |
Integer comment:'
|
88
|
21 |
COPYRIGHT (c) 1988 by Claus Gittinger
|
159
|
22 |
All Rights Reserved
|
92
|
23 |
|
213
|
24 |
$Header: /cvs/stx/stx/libbasic/Integer.st,v 1.19 1994-11-28 20:32:56 claus Exp $
|
88
|
25 |
'!
|
1
|
26 |
|
88
|
27 |
!Integer class methodsFor:'documentation'!
|
|
28 |
|
|
29 |
copyright
|
|
30 |
"
|
|
31 |
COPYRIGHT (c) 1988 by Claus Gittinger
|
159
|
32 |
All Rights Reserved
|
1
|
33 |
|
88
|
34 |
This software is furnished under a license and may be used
|
|
35 |
only in accordance with the terms of that license and with the
|
|
36 |
inclusion of the above copyright notice. This software may not
|
|
37 |
be provided or otherwise made available to, or used by, any
|
|
38 |
other person. No title to or ownership of the software is
|
|
39 |
hereby transferred.
|
|
40 |
"
|
|
41 |
!
|
1
|
42 |
|
88
|
43 |
version
|
|
44 |
"
|
213
|
45 |
$Header: /cvs/stx/stx/libbasic/Integer.st,v 1.19 1994-11-28 20:32:56 claus Exp $
|
88
|
46 |
"
|
|
47 |
!
|
|
48 |
|
|
49 |
documentation
|
|
50 |
"
|
|
51 |
abstract superclass for all integer numbers.
|
|
52 |
See details in concrete subclasses LargeInteger and SmallInteger.
|
212
|
53 |
|
|
54 |
Class variables:
|
|
55 |
|
|
56 |
DefaultDisplayRadix the radix in which integers present their
|
|
57 |
displayString (which is used in inspectors)
|
|
58 |
If you are to look at many hex numbers, bitmasks
|
|
59 |
etc. you may set this to 2 or 16.
|
|
60 |
(avoids typing printStringRadix:.. all the time
|
|
61 |
- I know - I am lazy ;-). Default is 10.
|
|
62 |
|
88
|
63 |
"
|
|
64 |
! !
|
1
|
65 |
|
|
66 |
!Integer class methodsFor:'constants'!
|
|
67 |
|
|
68 |
zero
|
44
|
69 |
"return the neutral element for addition"
|
|
70 |
|
1
|
71 |
^ 0
|
|
72 |
!
|
|
73 |
|
|
74 |
unity
|
44
|
75 |
"return the neutral element for multiplication"
|
|
76 |
|
1
|
77 |
^ 1
|
|
78 |
! !
|
|
79 |
|
92
|
80 |
!Integer class methodsFor:'instance creation'!
|
|
81 |
|
|
82 |
new:numberOfBytes neg:negative
|
|
83 |
"for ST-80 compatibility:
|
|
84 |
Return an empty Integer (uninitialized value) with space for
|
|
85 |
numberOfBytes bytes (= digitLength). The additional argument
|
132
|
86 |
negative specifies if the result should be a negative number.
|
|
87 |
The digits can be stored byte-wise into the result, using digitAt:put:"
|
92
|
88 |
|
132
|
89 |
^ (LargeInteger basicNew numberOfDigits:numberOfBytes)
|
159
|
90 |
sign:(negative ifTrue:[-1] ifFalse:[1])
|
92
|
91 |
! !
|
|
92 |
|
49
|
93 |
!Integer class methodsFor:'misc'!
|
|
94 |
|
|
95 |
displayRadix:aNumber
|
54
|
96 |
"being tired of always sending #printStringRadix: in the inspectors,
|
49
|
97 |
this allows you to change the default print radix for the displayString
|
|
98 |
method."
|
|
99 |
|
|
100 |
DefaultDisplayRadix := aNumber
|
132
|
101 |
|
|
102 |
"
|
|
103 |
Integer displayRadix:16. 123456 inspect
|
|
104 |
Integer displayRadix:10. 123456 inspect
|
|
105 |
"
|
49
|
106 |
! !
|
|
107 |
|
1
|
108 |
!Integer methodsFor:'arithmetic'!
|
|
109 |
|
|
110 |
quo:aNumber
|
|
111 |
"Return the integer quotient of dividing the receiver by aNumber with
|
54
|
112 |
truncation towards zero. For Integers this is same as //"
|
1
|
113 |
|
|
114 |
^ self // aNumber
|
|
115 |
|
|
116 |
! !
|
|
117 |
|
|
118 |
!Integer methodsFor:'double dispatching'!
|
|
119 |
|
|
120 |
sumFromFraction:aFraction
|
213
|
121 |
"sent when a fraction does not know how to add the receiver, an integer"
|
1
|
122 |
|
16
|
123 |
|d|
|
|
124 |
|
|
125 |
d := aFraction denominator.
|
|
126 |
^ (Fraction numerator:(aFraction numerator + (self * d))
|
159
|
127 |
denominator:d) reduced
|
1
|
128 |
!
|
|
129 |
|
|
130 |
differenceFromFraction:aFraction
|
213
|
131 |
"sent when a fraction does not know how to subtract the receiver, an integer"
|
1
|
132 |
|
16
|
133 |
|d|
|
|
134 |
|
|
135 |
d := aFraction denominator.
|
|
136 |
^ (Fraction numerator:(aFraction numerator - (self * d))
|
159
|
137 |
denominator:d) reduced
|
1
|
138 |
!
|
|
139 |
|
|
140 |
productFromFraction:aFraction
|
213
|
141 |
"sent when a fraction does not know how to multiply the receiver, an integer"
|
1
|
142 |
|
|
143 |
^ (Fraction numerator:(self * aFraction numerator)
|
159
|
144 |
denominator:aFraction denominator) reduced
|
69
|
145 |
!
|
|
146 |
|
|
147 |
quotientFromFraction:aFraction
|
213
|
148 |
"sent when a fraction does not know how to divide the receiver, an integer"
|
69
|
149 |
|
|
150 |
^ (Fraction numerator:aFraction numerator
|
159
|
151 |
denominator:(self * aFraction denominator)) reduced
|
69
|
152 |
! !
|
|
153 |
|
|
154 |
!Integer methodsFor:'bit operators'!
|
|
155 |
|
|
156 |
bitAnd:anInteger
|
|
157 |
"return the bitwise-and of the receiver and the argument, anInteger"
|
|
158 |
|
92
|
159 |
|result n byte|
|
69
|
160 |
|
92
|
161 |
n := (anInteger digitLength) min:(self digitLength).
|
|
162 |
result := self class basicNew numberOfDigits:n.
|
69
|
163 |
|
|
164 |
1 to:n do:[:index |
|
159
|
165 |
byte := (anInteger digitAt:index) bitAnd:(self digitAt:index).
|
|
166 |
result digitAt:index put:byte.
|
69
|
167 |
].
|
92
|
168 |
byte == 0 ifTrue:[
|
159
|
169 |
^ result normalize
|
92
|
170 |
].
|
|
171 |
^ result
|
69
|
172 |
!
|
|
173 |
|
|
174 |
bitOr:anInteger
|
|
175 |
"return the bitwise-or of the receiver and the argument, anInteger"
|
|
176 |
|
92
|
177 |
|result n byte|
|
69
|
178 |
|
92
|
179 |
n := (anInteger digitLength) max:(self digitLength).
|
|
180 |
result := self class basicNew numberOfDigits:n.
|
69
|
181 |
|
|
182 |
1 to:n do:[:index |
|
159
|
183 |
byte := (anInteger digitAt:index) bitOr:(self digitAt:index).
|
|
184 |
result digitAt:index put:byte.
|
69
|
185 |
].
|
92
|
186 |
byte == 0 ifTrue:[
|
159
|
187 |
^ result normalize
|
92
|
188 |
].
|
|
189 |
^ result
|
69
|
190 |
!
|
|
191 |
|
|
192 |
bitXor:anInteger
|
|
193 |
"return the bitwise-or of the receiver and the argument, anInteger"
|
|
194 |
|
92
|
195 |
|result n byte|
|
69
|
196 |
|
92
|
197 |
n := (anInteger digitLength) max:(self digitLength).
|
|
198 |
result := self class basicNew numberOfDigits:n.
|
69
|
199 |
|
|
200 |
1 to:n do:[:index |
|
159
|
201 |
byte := (anInteger digitAt:index) bitXor:(self digitAt:index).
|
|
202 |
result digitAt:index put:byte.
|
92
|
203 |
].
|
|
204 |
byte == 0 ifTrue:[
|
159
|
205 |
^ result normalize
|
69
|
206 |
].
|
92
|
207 |
^ result
|
|
208 |
!
|
|
209 |
|
|
210 |
highBit
|
|
211 |
"return the bitIndex of the highest bit set. The returned bitIndex
|
|
212 |
starts at 1 for the least significant bit. Returns -1 if no bit is set."
|
|
213 |
|
|
214 |
|byteNr highByte|
|
|
215 |
|
|
216 |
byteNr := self digitLength.
|
|
217 |
highByte := self digitAt:byteNr.
|
|
218 |
^ (byteNr - 1) * 8 + highByte highBit
|
|
219 |
|
|
220 |
"
|
|
221 |
1 highBit
|
|
222 |
(1 bitShift:1) highBit
|
|
223 |
(1 bitShift:30) highBit
|
|
224 |
(1 bitShift:31) highBit
|
|
225 |
(1 bitShift:32) highBit
|
|
226 |
(1 bitShift:33) highBit
|
|
227 |
(1 bitShift:64) highBit
|
|
228 |
(1 bitShift:1000) highBit
|
|
229 |
((1 bitShift:64)-1) highBit
|
|
230 |
"
|
85
|
231 |
!
|
|
232 |
|
|
233 |
bitAt:index
|
92
|
234 |
"return the value of the index's bit (index starts at 1).
|
|
235 |
Notice: the result of bitShift: on negative receivers is not
|
159
|
236 |
defined in the language standard (since the implementation
|
|
237 |
is free to choose any internal representation for integers)"
|
85
|
238 |
|
|
239 |
|i "{Class: SmallInteger}"|
|
|
240 |
|
|
241 |
i := index - 1.
|
|
242 |
^ (self digitAt:(i // 8 + 1)) bitAt:(i \\ 8 + 1)
|
92
|
243 |
|
|
244 |
"
|
|
245 |
1 bitAt:1
|
|
246 |
(1 bitShift:30) bitAt:30
|
|
247 |
(1 bitShift:30) bitAt:31
|
|
248 |
(1 bitShift:30) bitAt:32
|
|
249 |
(1 bitShift:31) bitAt:31
|
|
250 |
(1 bitShift:31) bitAt:32
|
|
251 |
(1 bitShift:31) bitAt:33
|
|
252 |
(1 bitShift:32) bitAt:32
|
|
253 |
(1 bitShift:32) bitAt:33
|
|
254 |
(1 bitShift:32) bitAt:34
|
|
255 |
(1 bitShift:64) bitAt:64
|
|
256 |
(1 bitShift:64) bitAt:65
|
|
257 |
(1 bitShift:64) bitAt:66
|
|
258 |
(1 bitShift:1000) bitAt:1000
|
|
259 |
(1 bitShift:1000) bitAt:1001
|
|
260 |
(1 bitShift:1000) bitAt:1002
|
|
261 |
(1 bitShift:1000) bitAt:2000
|
|
262 |
"
|
85
|
263 |
!
|
|
264 |
|
|
265 |
bitShift:shiftCount
|
|
266 |
"return the value of the receiver shifted by shiftCount bits;
|
|
267 |
leftShift if shiftCount > 0; rightShift otherwise.
|
92
|
268 |
|
|
269 |
Notice: the result of bitShift: on negative receivers is not
|
159
|
270 |
defined in the language standard (since the implementation
|
|
271 |
is free to choose any internal representation for integers)"
|
85
|
272 |
|
92
|
273 |
|result
|
|
274 |
prev "{ Class: SmallInteger }"
|
|
275 |
next "{ Class: SmallInteger }"
|
|
276 |
byte "{ Class: SmallInteger }"
|
|
277 |
bitShift "{ Class: SmallInteger }"
|
|
278 |
revShift "{ Class: SmallInteger }"
|
|
279 |
digitShift "{ Class: SmallInteger }"
|
|
280 |
n "{ Class: SmallInteger }"
|
|
281 |
nn "{ Class: SmallInteger }"|
|
85
|
282 |
|
|
283 |
shiftCount > 0 ifTrue:[
|
159
|
284 |
"left shift"
|
85
|
285 |
|
159
|
286 |
digitShift := shiftCount // 8.
|
|
287 |
bitShift := shiftCount \\ 8.
|
|
288 |
n := self digitLength.
|
92
|
289 |
|
159
|
290 |
"
|
|
291 |
modulu 8 shifts can be done faster ...
|
|
292 |
"
|
|
293 |
bitShift == 0 ifTrue:[
|
|
294 |
result := self class basicNew numberOfDigits:n + digitShift.
|
|
295 |
result sign:self sign.
|
|
296 |
result digits replaceFrom:(digitShift + 1) with:self digits.
|
|
297 |
"
|
|
298 |
no normalize needed, since receiver was already normalized
|
|
299 |
"
|
|
300 |
^ result
|
|
301 |
].
|
92
|
302 |
|
159
|
303 |
"
|
|
304 |
less-than-8 shifts can be done faster ...
|
|
305 |
"
|
|
306 |
digitShift == 0 ifTrue:[
|
|
307 |
n := n + 1.
|
|
308 |
result := self class basicNew numberOfDigits:n.
|
|
309 |
result sign:self sign.
|
|
310 |
prev := 0.
|
|
311 |
1 to:n-1 do:[:index |
|
|
312 |
byte := self digitAt:index.
|
|
313 |
byte := (byte bitShift:bitShift) bitOr:prev.
|
|
314 |
result digitAt:index put:(byte bitAnd:16rFF).
|
|
315 |
prev := byte bitShift:-8.
|
|
316 |
].
|
|
317 |
result digitAt:n put:prev.
|
|
318 |
"
|
|
319 |
might have stored a 0-byte ...
|
|
320 |
"
|
|
321 |
prev == 0 ifTrue:[
|
|
322 |
^ result normalize
|
|
323 |
].
|
|
324 |
^ result.
|
|
325 |
].
|
92
|
326 |
|
159
|
327 |
"
|
|
328 |
slow case ...
|
|
329 |
"
|
|
330 |
n := n + digitShift + 1.
|
|
331 |
result := self class basicNew numberOfDigits:n.
|
|
332 |
result sign:self sign.
|
92
|
333 |
|
159
|
334 |
byte := ((self digitAt:1) bitShift:bitShift) bitAnd:16rFF.
|
|
335 |
result digitAt:(digitShift + 1) put:byte.
|
|
336 |
revShift := -8 + bitShift.
|
|
337 |
2 to:(self digitLength) do:[:index |
|
|
338 |
byte := (self digitAt:index) bitShift:bitShift.
|
|
339 |
byte := byte bitOr:((self digitAt:index-1) bitShift:revShift).
|
|
340 |
byte := byte bitAnd:16rFF.
|
|
341 |
result digitAt:(index + digitShift) put:byte
|
|
342 |
].
|
|
343 |
"
|
|
344 |
might have stored a 0-byte ...
|
|
345 |
"
|
|
346 |
byte == 0 ifTrue:[
|
|
347 |
^ result normalize
|
|
348 |
].
|
|
349 |
^ result
|
92
|
350 |
].
|
|
351 |
|
|
352 |
shiftCount < 0 ifTrue:[
|
159
|
353 |
"right shift"
|
85
|
354 |
|
159
|
355 |
digitShift := shiftCount negated // 8.
|
|
356 |
bitShift := shiftCount negated \\ 8.
|
|
357 |
n := self digitLength.
|
92
|
358 |
|
159
|
359 |
digitShift >= n ifTrue:[
|
|
360 |
^ 0
|
|
361 |
].
|
92
|
362 |
|
159
|
363 |
"
|
|
364 |
modulu 8 shifts can be done faster ...
|
|
365 |
"
|
|
366 |
bitShift == 0 ifTrue:[
|
|
367 |
n := n-digitShift.
|
|
368 |
result := self class basicNew numberOfDigits:n.
|
|
369 |
result sign:self sign.
|
|
370 |
result digits replaceFrom:1 with:self digits startingAt:(digitShift + 1) .
|
|
371 |
n <= 4 ifTrue:[
|
|
372 |
^ result normalize
|
|
373 |
].
|
|
374 |
^ result
|
|
375 |
].
|
85
|
376 |
|
159
|
377 |
"
|
|
378 |
less-than-8 shifts can be done faster ...
|
|
379 |
"
|
|
380 |
digitShift == 0 ifTrue:[
|
|
381 |
result := self class basicNew numberOfDigits:n.
|
|
382 |
result sign:self sign.
|
|
383 |
prev := 0.
|
|
384 |
bitShift := bitShift negated.
|
|
385 |
revShift := 8 + bitShift.
|
|
386 |
n to:1 by:-1 do:[:index |
|
|
387 |
byte := self digitAt:index.
|
|
388 |
next := (byte bitShift:revShift) bitAnd:16rFF.
|
|
389 |
byte := (byte bitShift:bitShift) bitOr:prev.
|
|
390 |
result digitAt:index put:(byte bitAnd:16rFF).
|
|
391 |
prev := next.
|
|
392 |
].
|
|
393 |
(n <= 5) ifTrue:[
|
|
394 |
^ result normalize
|
|
395 |
].
|
|
396 |
^ result
|
|
397 |
].
|
92
|
398 |
|
159
|
399 |
"
|
|
400 |
slow case ...
|
|
401 |
"
|
|
402 |
nn := n-digitShift.
|
|
403 |
result := self class basicNew numberOfDigits:nn.
|
|
404 |
result sign:self sign.
|
85
|
405 |
|
159
|
406 |
prev := 0.
|
|
407 |
bitShift := bitShift negated.
|
|
408 |
revShift := 8 + bitShift.
|
|
409 |
n to:(1 + digitShift) by:-1 do:[:index |
|
|
410 |
byte := self digitAt:index.
|
|
411 |
next := (byte bitShift:revShift) bitAnd:16rFF.
|
|
412 |
byte := (byte bitShift:bitShift) bitOr:prev.
|
|
413 |
result digitAt:(index - digitShift) put:byte.
|
|
414 |
prev := next.
|
|
415 |
].
|
|
416 |
"the last stored byte ..."
|
|
417 |
^ result normalize
|
85
|
418 |
].
|
92
|
419 |
|
|
420 |
^ self "no shift"
|
1
|
421 |
! !
|
|
422 |
|
|
423 |
!Integer methodsFor:'truncation & rounding'!
|
|
424 |
|
|
425 |
ceiling
|
|
426 |
"I am my ceiling"
|
|
427 |
|
|
428 |
^ self
|
|
429 |
!
|
|
430 |
|
|
431 |
floor
|
|
432 |
"I am my floor"
|
|
433 |
|
|
434 |
^ self
|
|
435 |
!
|
|
436 |
|
|
437 |
rounded
|
|
438 |
"return the receiver rounded toward the next Integer -
|
|
439 |
for integers this is self"
|
|
440 |
|
|
441 |
^ self
|
|
442 |
!
|
|
443 |
|
|
444 |
truncated
|
|
445 |
"return the receiver truncated towards zero -
|
|
446 |
for integers this is self"
|
|
447 |
|
|
448 |
^ self
|
92
|
449 |
!
|
|
450 |
|
|
451 |
normalize
|
|
452 |
"this is ST/X's name for ST-80's compressed.
|
|
453 |
ST/X may be changed for full compatibility."
|
|
454 |
|
|
455 |
^ self
|
|
456 |
!
|
|
457 |
|
|
458 |
compressed
|
|
459 |
"this is ST-80's name for ST/X's normalize.
|
|
460 |
ST/X may be changed for full compatibility."
|
|
461 |
|
|
462 |
^ self
|
1
|
463 |
! !
|
|
464 |
|
|
465 |
!Integer methodsFor:'queries'!
|
|
466 |
|
|
467 |
digitLength
|
|
468 |
"return the number of bytes needed for the binary representation
|
|
469 |
of the receiver"
|
|
470 |
|
|
471 |
^ (self log:256) ceiling asInteger
|
|
472 |
!
|
|
473 |
|
|
474 |
digitAt:n
|
92
|
475 |
"return the n-th byte of the binary representation.
|
|
476 |
This is a very stupid implementation, and should be redefined in
|
|
477 |
concrete subclasses."
|
1
|
478 |
|
|
479 |
|num count|
|
|
480 |
|
|
481 |
num := self.
|
|
482 |
count := n.
|
|
483 |
[count > 1] whileTrue:[
|
159
|
484 |
num := num // 256.
|
|
485 |
count := count - 1
|
1
|
486 |
].
|
|
487 |
^ num \\ 256
|
77
|
488 |
|
|
489 |
"
|
|
490 |
16r44332211 digitAt:1
|
|
491 |
16r44332211 digitAt:2
|
|
492 |
16r44332211 digitAt:3
|
|
493 |
16r44332211 digitAt:4
|
|
494 |
16r44332211 digitAt:5
|
|
495 |
16r00332211 digitAt:4
|
|
496 |
16r00332211 digitAt:5
|
|
497 |
"
|
1
|
498 |
!
|
|
499 |
|
|
500 |
isInteger
|
|
501 |
"return true, if the receiver is some kind of integer number"
|
|
502 |
|
|
503 |
^ true
|
|
504 |
! !
|
|
505 |
|
|
506 |
!Integer methodsFor:'misc math'!
|
|
507 |
|
|
508 |
factorial
|
|
509 |
"return 1*2*3...*self"
|
|
510 |
|
52
|
511 |
(self > 2) ifTrue:[
|
159
|
512 |
^ self * (self - 1) factorial
|
1
|
513 |
].
|
|
514 |
^ self
|
212
|
515 |
|
|
516 |
"
|
|
517 |
10 factorial
|
|
518 |
1000 factorial
|
|
519 |
"
|
1
|
520 |
!
|
|
521 |
|
|
522 |
gcd:anInteger
|
|
523 |
"return the greatest common divisor (Euclid's algorithm)"
|
|
524 |
|
|
525 |
|ttt selfInteger temp|
|
|
526 |
|
|
527 |
ttt := anInteger.
|
|
528 |
selfInteger := self.
|
|
529 |
[ttt ~~ 0] whileTrue:[
|
159
|
530 |
temp := selfInteger \\ ttt.
|
|
531 |
selfInteger := ttt.
|
|
532 |
ttt := temp
|
1
|
533 |
].
|
|
534 |
^ selfInteger
|
52
|
535 |
|
92
|
536 |
"
|
|
537 |
65 gcd:15
|
|
538 |
3 gcd:15
|
|
539 |
132 gcd:55
|
|
540 |
"
|
1
|
541 |
!
|
|
542 |
|
|
543 |
lcm:anInteger
|
52
|
544 |
"return the least common multiple (using gcd:)"
|
|
545 |
|
|
546 |
^ (self * anInteger) abs // (self gcd: anInteger)
|
|
547 |
|
92
|
548 |
"
|
|
549 |
65 lcm:15
|
|
550 |
3 lcm:15
|
|
551 |
"
|
1
|
552 |
!
|
|
553 |
|
|
554 |
fib
|
|
555 |
"dont use this method if you need fibionacci numbers -
|
|
556 |
this method is for benchmarking purposes only.
|
|
557 |
(use fastFib instead and dont ever try 60 fib ...)"
|
|
558 |
|
|
559 |
(self > 1) ifTrue:[
|
159
|
560 |
^ (self - 1) fib + (self - 2) fib
|
1
|
561 |
].
|
|
562 |
^ 1
|
|
563 |
|
85
|
564 |
"
|
212
|
565 |
10 fib
|
|
566 |
20 fib
|
|
567 |
Transcript showCr:(Time millisecondsToRun:[30 fib])
|
85
|
568 |
"
|
1
|
569 |
!
|
|
570 |
|
|
571 |
fastFib
|
|
572 |
"this method just to show how a changed algorithm can
|
|
573 |
change things much more drastic than tuning ...
|
|
574 |
(compare 30 fib with 30 fastFib / dont even try 60 fib)"
|
|
575 |
|
92
|
576 |
^ self fibUsingCache:(OrderedCollection new)
|
|
577 |
|
|
578 |
"
|
212
|
579 |
Transcript showCr:(Time millisecondsToRun:[30 fib])
|
92
|
580 |
Transcript showCr:(Time millisecondsToRun:[30 fastFib])
|
|
581 |
Transcript showCr:(Time millisecondsToRun:[60 fastFib])
|
|
582 |
Transcript showCr:(Time millisecondsToRun:[200 fastFib])
|
|
583 |
"
|
|
584 |
!
|
|
585 |
|
|
586 |
fibUsingCache:fibCache
|
|
587 |
"actual fastfib worker; keep track of already computed fib
|
|
588 |
numbers in fibCache, which is passed around. Take value from
|
|
589 |
the cache if already present. Doing so, the double recursive
|
|
590 |
fib actually becomes a single recursive algorithm."
|
|
591 |
|
1
|
592 |
|fib|
|
|
593 |
|
|
594 |
self <= 1 ifTrue:[^ 1].
|
|
595 |
|
92
|
596 |
fibCache size >= self ifTrue:[
|
159
|
597 |
^ fibCache at:self
|
1
|
598 |
].
|
92
|
599 |
fib := ((self - 2) fibUsingCache:fibCache) + ((self - 1) fibUsingCache:fibCache).
|
1
|
600 |
|
92
|
601 |
fibCache grow:self.
|
|
602 |
fibCache at:self put:fib.
|
1
|
603 |
^ fib
|
|
604 |
!
|
|
605 |
|
|
606 |
acker:n
|
|
607 |
"return the value of acker(self, n)"
|
|
608 |
|
|
609 |
(self == 0) ifTrue:[^ n + 1].
|
|
610 |
(n == 0) ifTrue:[^ (self - 1) acker: 1].
|
|
611 |
^ (self - 1) acker:(self acker:(n - 1))
|
|
612 |
|
92
|
613 |
"
|
|
614 |
3 acker:2
|
|
615 |
"
|
1
|
616 |
! !
|
|
617 |
|
|
618 |
!Integer methodsFor:'coercing and converting'!
|
|
619 |
|
|
620 |
asFraction
|
|
621 |
"return a Fraction with same value as receiver"
|
|
622 |
|
|
623 |
^ Fraction numerator:self denominator:1
|
|
624 |
!
|
|
625 |
|
|
626 |
asInteger
|
|
627 |
"return the receiver truncated towards zero -
|
|
628 |
for integers this is self"
|
|
629 |
|
|
630 |
^ self
|
|
631 |
! !
|
|
632 |
|
|
633 |
!Integer methodsFor:'printing & storing'!
|
|
634 |
|
54
|
635 |
printOn:aStream
|
|
636 |
"append a printed description of the receiver to aStream"
|
1
|
637 |
|
54
|
638 |
aStream nextPutAll:(self printStringRadix:10)
|
1
|
639 |
!
|
|
640 |
|
202
|
641 |
printOn:aStream radix:base
|
|
642 |
"append a printed description of the receiver to aStream"
|
|
643 |
|
|
644 |
aStream nextPutAll:(self printStringRadix:base)
|
|
645 |
!
|
|
646 |
|
1
|
647 |
printString
|
|
648 |
"return a string representation of the receiver"
|
|
649 |
|
|
650 |
^ self printStringRadix:10
|
|
651 |
!
|
|
652 |
|
49
|
653 |
displayString
|
|
654 |
"return a string for displaying in a view (as in inspector).
|
|
655 |
The output radix is usually 10, but can be changed by setting
|
|
656 |
DefaultDisplayRadix (see Integer>>displayRadix:)"
|
|
657 |
|
54
|
658 |
(DefaultDisplayRadix isNil or:[DefaultDisplayRadix == 10]) ifTrue:[
|
159
|
659 |
^ self printString
|
54
|
660 |
].
|
49
|
661 |
^ self radixPrintStringRadix:DefaultDisplayRadix
|
|
662 |
|
92
|
663 |
"
|
|
664 |
Integer displayRadix:16. 12345
|
|
665 |
Integer displayRadix:2. 12345
|
|
666 |
Integer displayRadix:10. 12345
|
|
667 |
"
|
49
|
668 |
!
|
|
669 |
|
1
|
670 |
radixPrintStringRadix:aRadix
|
|
671 |
"return a string representation of the receiver in the specified
|
|
672 |
radix; prepend XXr to the string"
|
|
673 |
|
|
674 |
^ (aRadix printString) , 'r', (self printStringRadix:aRadix)
|
|
675 |
|
92
|
676 |
"
|
|
677 |
31 radixPrintStringRadix:2
|
|
678 |
31 radixPrintStringRadix:3
|
|
679 |
31 radixPrintStringRadix:36
|
|
680 |
"
|
1
|
681 |
!
|
|
682 |
|
|
683 |
printStringRadix:aRadix
|
|
684 |
"return a string representation of the receiver in the specified
|
|
685 |
radix (without the initial XXr)"
|
|
686 |
|
92
|
687 |
|num s "rx leftPart"|
|
1
|
688 |
|
54
|
689 |
(aRadix between:2 and:36) ifFalse:[
|
159
|
690 |
self error:'invalid radix'.
|
|
691 |
^ self printStringRadix:10
|
54
|
692 |
].
|
|
693 |
|
1
|
694 |
(self = 0) ifTrue:[^ '0'].
|
|
695 |
(self < 0) ifTrue:[
|
159
|
696 |
^ '-' , (self negated printStringRadix:aRadix)
|
1
|
697 |
].
|
92
|
698 |
|
|
699 |
"
|
|
700 |
claus: changed it from a recursive algorithm;
|
|
701 |
(it used to trigger stack-overflow exceptions when printing
|
|
702 |
3000 factorial ...)
|
|
703 |
"
|
|
704 |
"/ leftPart := self // aRadix.
|
|
705 |
"/ (leftPart ~= 0) ifTrue:[
|
|
706 |
"/ ^ (leftPart printStringRadix:aRadix) copyWith:(Character digitValue:(self \\ aRadix))
|
|
707 |
"/ ].
|
|
708 |
"/ ^ (Character digitValue:self) asString
|
|
709 |
|
|
710 |
num := self.
|
|
711 |
s := (Character digitValue:(num \\ aRadix)) asString.
|
|
712 |
num := num // aRadix.
|
|
713 |
[num ~= 0] whileTrue:[
|
159
|
714 |
s := (Character digitValue:(num \\ aRadix)) asString , s.
|
|
715 |
num := num // aRadix.
|
1
|
716 |
].
|
92
|
717 |
^ s
|
1
|
718 |
!
|
|
719 |
|
|
720 |
printStringRadix:aRadix size:sz fill:fillCharacter
|
|
721 |
"return a string representation of the receiver in the specified
|
|
722 |
radix. The string is padded on the left with fillCharacter to make
|
|
723 |
its size as specified in sz."
|
|
724 |
|
92
|
725 |
|s|
|
1
|
726 |
|
|
727 |
s := self printStringRadix:aRadix.
|
|
728 |
s size < sz ifTrue:[
|
159
|
729 |
s := ((String new:(sz - s size)) atAllPut:fillCharacter) , s
|
1
|
730 |
].
|
|
731 |
^ s
|
|
732 |
|
92
|
733 |
"
|
|
734 |
1024 printStringRadix:16 size:4 fill:$0
|
|
735 |
1024 printStringRadix:2 size:16 fill:$.
|
|
736 |
1024 printStringRadix:16 size:8 fill:(Character space)
|
|
737 |
"
|
1
|
738 |
! !
|
|
739 |
|
|
740 |
!Integer class methodsFor:'instance creation'!
|
|
741 |
|
|
742 |
readFrom:aStream radix:radix
|
159
|
743 |
"return the next unsigned Integer from the (character-)stream aStream
|
|
744 |
in radix; (assumes that the initial XXr has already been read).
|
|
745 |
No whitespace-skipping is done.
|
202
|
746 |
Returns 0 if no number available."
|
|
747 |
|
|
748 |
^ self readFrom:aStream radix:radix onError:[0]
|
|
749 |
!
|
|
750 |
|
|
751 |
readFrom:aStream radix:radix onError:exceptionBlock
|
|
752 |
"return the next unsigned Integer from the (character-)stream aStream
|
|
753 |
in radix; (assumes that the initial XXr has already been read).
|
|
754 |
No whitespace-skipping is done.
|
|
755 |
Returns the value of exceptionBlock, if no number is available."
|
1
|
756 |
|
|
757 |
|nextChar value|
|
|
758 |
|
|
759 |
nextChar := aStream peek.
|
202
|
760 |
(nextChar notNil and:[nextChar isDigitRadix:radix]) ifFalse:[
|
|
761 |
^ exceptionBlock value
|
|
762 |
].
|
|
763 |
|
|
764 |
value := nextChar digitValue.
|
|
765 |
nextChar := aStream nextPeek.
|
1
|
766 |
[nextChar notNil and:[nextChar isDigitRadix:radix]] whileTrue:[
|
159
|
767 |
value := value * radix + nextChar digitValue.
|
|
768 |
nextChar := aStream nextPeek
|
1
|
769 |
].
|
|
770 |
^ value
|
159
|
771 |
|
|
772 |
"
|
|
773 |
Integer readFrom:(ReadStream on:'12345') radix:10
|
|
774 |
Integer readFrom:(ReadStream on:'FFFF') radix:16
|
|
775 |
Integer readFrom:(ReadStream on:'1010') radix:2
|
|
776 |
"
|
1
|
777 |
!
|
|
778 |
|
202
|
779 |
readFrom:aStream
|
|
780 |
"return the next unsigned Integer from the (character-)stream aStream
|
|
781 |
as decimal number. This is redefined from the default readFrom:, to return
|
|
782 |
0 (instead of raising an error) in case no number can be read.
|
|
783 |
It is unclear, if this is the correct behavior - depending on
|
|
784 |
the upcoming ANSI standard, this may change."
|
|
785 |
|
|
786 |
^ self readFrom:aStream onError:[0]
|
|
787 |
!
|
|
788 |
|
|
789 |
readFrom:aStringOrStream onError:exceptionBlock
|
1
|
790 |
"return the next Integer from the (character-)stream aStream,
|
202
|
791 |
handling initial XXr for arbitrary radix numbers and initial sign.
|
159
|
792 |
Also, all initial whitespace is skipped.
|
202
|
793 |
If the string does not represent a valid integer number,
|
|
794 |
return the value of exceptionBlock."
|
|
795 |
|
|
796 |
|str nextChar value negative|
|
1
|
797 |
|
202
|
798 |
aStringOrStream isString ifTrue:[
|
|
799 |
str := aStringOrStream readStream.
|
|
800 |
] ifFalse:[
|
|
801 |
str := aStringOrStream
|
|
802 |
].
|
1
|
803 |
|
202
|
804 |
nextChar := str skipSeparators.
|
1
|
805 |
(nextChar == $-) ifTrue:[
|
159
|
806 |
negative := true.
|
202
|
807 |
nextChar := str nextPeek
|
1
|
808 |
] ifFalse:[
|
159
|
809 |
negative := false
|
1
|
810 |
].
|
159
|
811 |
nextChar isDigit ifFalse:[
|
|
812 |
"
|
|
813 |
the string does not represent an integer
|
|
814 |
"
|
202
|
815 |
^ exceptionBlock value
|
159
|
816 |
].
|
202
|
817 |
value := self readFrom:str radix:10 onError:[^ exceptionBlock value].
|
|
818 |
nextChar := str peek.
|
1
|
819 |
((nextChar == $r) or:[ nextChar == $R]) ifTrue:[
|
159
|
820 |
"-xxr<number> is invalid; should be xxr-<val>"
|
69
|
821 |
|
159
|
822 |
negative ifTrue:[
|
|
823 |
'invalid (negative) radix ignored' errorPrintNL.
|
|
824 |
negative := false
|
|
825 |
].
|
202
|
826 |
str next.
|
|
827 |
value := self readFrom:str radix:value onError:[^ exceptionBlock value]
|
1
|
828 |
].
|
|
829 |
negative ifTrue:[
|
159
|
830 |
^ value negated
|
1
|
831 |
].
|
|
832 |
^ value
|
159
|
833 |
|
|
834 |
"
|
202
|
835 |
Integer readFrom:'12345'
|
|
836 |
Integer readFrom:'16rFFFF'
|
|
837 |
Integer readFrom:'12345.1234'
|
|
838 |
Integer readFrom:'foo'
|
|
839 |
Integer readFrom:'foo' onError:[0]
|
159
|
840 |
"
|
1
|
841 |
! !
|
|
842 |
|
|
843 |
!Integer methodsFor:'benchmarking'!
|
|
844 |
|
|
845 |
sieve
|
|
846 |
"sieve the primes self times"
|
|
847 |
|
|
848 |
|num i k prime count flags time|
|
|
849 |
|
|
850 |
num := 8191.
|
|
851 |
flags := Array new:num.
|
|
852 |
|
|
853 |
Transcript show:'Sieve running ...'.
|
|
854 |
Transcript cr.
|
|
855 |
|
|
856 |
time := Time millisecondsToRun:[
|
159
|
857 |
self timesRepeat:[
|
|
858 |
count := 0.
|
|
859 |
flags atAllPut:1.
|
|
860 |
i := 1.
|
|
861 |
num timesRepeat:[
|
|
862 |
(flags at:i) == 1 ifTrue:[
|
|
863 |
prime := i + i + 3.
|
|
864 |
k := i + prime.
|
|
865 |
[k <= num] whileTrue:[
|
|
866 |
flags at:k put:0.
|
|
867 |
k := k + prime
|
|
868 |
].
|
|
869 |
count := count + 1
|
|
870 |
].
|
|
871 |
i := i + 1
|
|
872 |
].
|
|
873 |
].
|
1
|
874 |
].
|
|
875 |
Transcript show:'Sieve in Smalltalk: '.
|
|
876 |
Transcript show:self printString.
|
|
877 |
Transcript showCr:' iteration(s).'.
|
|
878 |
Transcript show:'found '.
|
|
879 |
Transcript show:count printString.
|
|
880 |
Transcript showCr:' primes.' .
|
|
881 |
Transcript show:'time per run: '.
|
|
882 |
Transcript show:(time / self) printString.
|
|
883 |
Transcript showCr:' ms.'
|
|
884 |
|
|
885 |
"1 sieve"
|
|
886 |
!
|
|
887 |
|
|
888 |
sieveWithIntegers
|
|
889 |
"sieve the primes self times"
|
|
890 |
|
202
|
891 |
|num "{ Class: SmallInteger }"
|
|
892 |
i "{ Class: SmallInteger }"
|
|
893 |
k "{ Class: SmallInteger }"
|
|
894 |
prime "{ Class: SmallInteger }"
|
|
895 |
count "{ Class: SmallInteger }"
|
1
|
896 |
flags time|
|
|
897 |
|
|
898 |
num := 8191.
|
|
899 |
flags := Array new:num.
|
|
900 |
|
|
901 |
Transcript show:'Sieve running ...'.
|
|
902 |
Transcript cr.
|
|
903 |
|
|
904 |
time := Time millisecondsToRun:[
|
159
|
905 |
self timesRepeat:[
|
|
906 |
count := 0.
|
|
907 |
flags atAllPut:1.
|
|
908 |
i := 1.
|
|
909 |
num timesRepeat:[
|
|
910 |
(flags at:i) == 1 ifTrue:[
|
|
911 |
prime := i + i + 3.
|
|
912 |
k := i + prime.
|
|
913 |
[k <= num] whileTrue:[
|
|
914 |
flags at:k put:0.
|
|
915 |
k := k + prime
|
|
916 |
].
|
|
917 |
count := count + 1
|
|
918 |
].
|
|
919 |
i := i + 1
|
|
920 |
].
|
|
921 |
].
|
1
|
922 |
].
|
|
923 |
Transcript show:'Sieve in Smalltalk: '.
|
|
924 |
Transcript show:self printString.
|
|
925 |
Transcript showCr:' iteration(s).'.
|
|
926 |
Transcript show:'found '.
|
|
927 |
Transcript show:count printString.
|
|
928 |
Transcript showCr:' primes.' .
|
|
929 |
Transcript show:'time per run: '.
|
|
930 |
Transcript show:(time / self) printString.
|
|
931 |
Transcript showCr:' ms.'
|
|
932 |
|
|
933 |
"1 sieveWithIntegers"
|
|
934 |
!
|
|
935 |
|
|
936 |
recur1:num
|
|
937 |
"actual recursion method for recur1"
|
|
938 |
|
|
939 |
(num = 0) ifTrue:[^ self].
|
|
940 |
self recur1:(num - 1).
|
|
941 |
^ self recur1:(num - 1)
|
|
942 |
!
|
|
943 |
|
|
944 |
recur1
|
|
945 |
"lots of recursion for testing send with arg"
|
|
946 |
|
|
947 |
|t|
|
|
948 |
|
|
949 |
t := Time millisecondsToRun:[
|
159
|
950 |
1 recur1:15
|
1
|
951 |
].
|
|
952 |
Transcript showCr:(t printString)
|
|
953 |
|
|
954 |
"1 recur1"
|
|
955 |
!
|
|
956 |
|
49
|
957 |
doRecur2
|
1
|
958 |
"lots of recursion for testing send without arg"
|
|
959 |
|
|
960 |
(self > 0) ifTrue:[
|
159
|
961 |
(self - 1) doRecur2.
|
|
962 |
^ (self - 1) doRecur2
|
1
|
963 |
]
|
49
|
964 |
!
|
|
965 |
|
|
966 |
recur2
|
|
967 |
"lots of recursion for testing send without arg"
|
1
|
968 |
|
49
|
969 |
|t|
|
|
970 |
|
|
971 |
t := Time millisecondsToRun:[
|
159
|
972 |
15 doRecur2
|
49
|
973 |
].
|
|
974 |
Transcript showCr:(t printString)
|
|
975 |
|
|
976 |
"1 recur2"
|
1
|
977 |
!
|
|
978 |
|
85
|
979 |
blockEvaluation
|
|
980 |
"evaluating a simple block"
|
|
981 |
|
|
982 |
|t|
|
|
983 |
|
|
984 |
t := Time millisecondsToRun:[
|
159
|
985 |
|b|
|
85
|
986 |
|
159
|
987 |
b := [99].
|
|
988 |
1000000 timesRepeat:[b value]
|
85
|
989 |
].
|
|
990 |
Transcript showCr:(t printString)
|
|
991 |
|
|
992 |
"1 blockEvaluation"
|
|
993 |
!
|
|
994 |
|
1
|
995 |
countDown
|
49
|
996 |
"count down - notice, that index is a method var"
|
|
997 |
|
1
|
998 |
|t index|
|
|
999 |
|
|
1000 |
t := Time millisecondsToRun:[
|
159
|
1001 |
index := 100000.
|
|
1002 |
[index > 0] whileTrue:[
|
|
1003 |
index := index - 1
|
|
1004 |
].
|
1
|
1005 |
].
|
|
1006 |
Transcript showCr:(t printString)
|
|
1007 |
|
|
1008 |
"1 countDown"
|
|
1009 |
!
|
|
1010 |
|
|
1011 |
countDown2
|
49
|
1012 |
"count down - notice, that index is a block var"
|
|
1013 |
|
1
|
1014 |
|t|
|
|
1015 |
|
|
1016 |
t := Time millisecondsToRun:[
|
159
|
1017 |
|index|
|
1
|
1018 |
|
159
|
1019 |
index := 100000.
|
|
1020 |
[index > 0] whileTrue:[
|
|
1021 |
index := index - 1
|
|
1022 |
].
|
1
|
1023 |
].
|
|
1024 |
Transcript showCr:(t printString)
|
|
1025 |
|
|
1026 |
"1 countDown2"
|
|
1027 |
!
|
|
1028 |
|
92
|
1029 |
noopSelf
|
1
|
1030 |
^ self
|
|
1031 |
!
|
|
1032 |
|
92
|
1033 |
noopNil
|
|
1034 |
^ nil
|
|
1035 |
!
|
|
1036 |
|
|
1037 |
noopTrue
|
|
1038 |
^ true
|
|
1039 |
!
|
|
1040 |
|
77
|
1041 |
send
|
92
|
1042 |
"lots of dummy message sends.
|
|
1043 |
About half of the time is spent in block evaluation
|
|
1044 |
(timesRepeat is not inlined), another half in the send itself."
|
1
|
1045 |
|
|
1046 |
|t|
|
|
1047 |
|
|
1048 |
t := Time millisecondsToRun:[
|
159
|
1049 |
self timesRepeat:[
|
|
1050 |
self noopSelf
|
|
1051 |
].
|
1
|
1052 |
].
|
|
1053 |
Transcript showCr:(t printString)
|
|
1054 |
|
77
|
1055 |
"1000000 send"
|
1
|
1056 |
!
|
|
1057 |
|
85
|
1058 |
send2
|
92
|
1059 |
"lots of dummy message sends. This tests the speed of a ^self method.
|
|
1060 |
Almost all time is spent in the send (since timesRepeat is inlined here)."
|
85
|
1061 |
|
|
1062 |
|t|
|
|
1063 |
|
|
1064 |
t := Time millisecondsToRun:[
|
159
|
1065 |
1000000 timesRepeat:[
|
|
1066 |
self noopSelf
|
|
1067 |
].
|
85
|
1068 |
].
|
|
1069 |
Transcript showCr:(t printString)
|
|
1070 |
|
|
1071 |
"1 send2"
|
|
1072 |
!
|
|
1073 |
|
92
|
1074 |
send3
|
|
1075 |
"lots of dummy message sends. This tests the speed of a ^nil method.
|
|
1076 |
Almost all time is spent in the send (since timesRepeat is inlined here)."
|
|
1077 |
|
|
1078 |
|t|
|
|
1079 |
|
|
1080 |
t := Time millisecondsToRun:[
|
159
|
1081 |
1000000 timesRepeat:[
|
|
1082 |
self noopNil
|
|
1083 |
].
|
92
|
1084 |
].
|
|
1085 |
Transcript showCr:(t printString)
|
|
1086 |
|
|
1087 |
"1 send3"
|
|
1088 |
!
|
|
1089 |
|
|
1090 |
send4
|
|
1091 |
"lots of dummy message sends. This tests the speed of a ^true method.
|
|
1092 |
Almost all time is spent in the send (since timesRepeat is inlined here)."
|
|
1093 |
|
|
1094 |
|t|
|
|
1095 |
|
|
1096 |
t := Time millisecondsToRun:[
|
159
|
1097 |
1000000 timesRepeat:[
|
|
1098 |
self noopTrue
|
|
1099 |
].
|
92
|
1100 |
].
|
|
1101 |
Transcript showCr:(t printString)
|
|
1102 |
|
|
1103 |
"1 send4"
|
|
1104 |
!
|
|
1105 |
|
77
|
1106 |
instAccess1
|
92
|
1107 |
"check simple send & instvar access time.
|
|
1108 |
About half of the time is spent in block evaluation
|
|
1109 |
(timesRepeat is not inlined), another half in the send itself."
|
49
|
1110 |
|
|
1111 |
|t|
|
|
1112 |
|
|
1113 |
t := Time millisecondsToRun:[
|
159
|
1114 |
|a|
|
49
|
1115 |
|
159
|
1116 |
a := 1->2.
|
|
1117 |
self timesRepeat:[a key].
|
49
|
1118 |
].
|
|
1119 |
Transcript showCr:(t printString)
|
|
1120 |
|
77
|
1121 |
"1000000 instAccess1"
|
|
1122 |
!
|
|
1123 |
|
|
1124 |
instAccess2
|
92
|
1125 |
"check simple send & instvar access time.
|
|
1126 |
This tests the speed of a ^instVar method.
|
|
1127 |
Almost all time is spent in the send (since timesRepeat is inlined here)."
|
77
|
1128 |
|
|
1129 |
|t|
|
|
1130 |
|
|
1131 |
t := Time millisecondsToRun:[
|
159
|
1132 |
|a|
|
77
|
1133 |
|
159
|
1134 |
a := 1->2.
|
|
1135 |
1000000 timesRepeat:[a key].
|
77
|
1136 |
].
|
|
1137 |
Transcript showCr:(t printString)
|
|
1138 |
|
|
1139 |
"1 instAccess2"
|
49
|
1140 |
!
|
|
1141 |
|
1
|
1142 |
memory
|
85
|
1143 |
"lots of memory allocation
|
92
|
1144 |
(GC benchmark; allocates, nils & collects about 200Mb).
|
85
|
1145 |
Run this benchmark a few times - its outcome depends on
|
92
|
1146 |
newSpace fill-grade & cache patterns ....
|
|
1147 |
Here a block evaluation is performed, but its time is only
|
|
1148 |
a very small fraction compared to allocation & gc-time."
|
1
|
1149 |
|
|
1150 |
|t|
|
|
1151 |
|
|
1152 |
t := Time millisecondsToRun:[
|
159
|
1153 |
self timesRepeat:[
|
|
1154 |
Array new:500
|
|
1155 |
].
|
1
|
1156 |
].
|
|
1157 |
Transcript showCr:(t printString)
|
|
1158 |
|
92
|
1159 |
"100000 * 500 * (4 bytes/object) -> 200000000"
|
77
|
1160 |
"100000 memory"
|
1
|
1161 |
!
|
|
1162 |
|
85
|
1163 |
memory2
|
|
1164 |
"lots of memory allocation
|
92
|
1165 |
(GC benchmark; allocates, nils & collects about 200Mb).
|
85
|
1166 |
Run this benchmark a few times - its outcome depends on
|
92
|
1167 |
newSpace fill-grade & cache patterns ...
|
|
1168 |
Since timesRepeat-block is inlined here, all time is spent in
|
|
1169 |
allocation & gc."
|
85
|
1170 |
|
|
1171 |
|t|
|
|
1172 |
|
|
1173 |
t := Time millisecondsToRun:[
|
159
|
1174 |
100000 timesRepeat:[
|
|
1175 |
Array new:500
|
|
1176 |
].
|
85
|
1177 |
].
|
|
1178 |
Transcript showCr:(t printString)
|
|
1179 |
|
|
1180 |
"1 memory2"
|
|
1181 |
!
|
|
1182 |
|
1
|
1183 |
benchArithmetic
|
85
|
1184 |
"arithmetic speed bench
|
|
1185 |
(actually, this is a GC, and block evaluation benchmark.
|
|
1186 |
it allocates & collects about 40Mb during its run).
|
|
1187 |
Run this benchmark a few times - its outcome depends on
|
|
1188 |
newSpace fill-grade & cache patterns ..."
|
1
|
1189 |
|
|
1190 |
|p n m t|
|
|
1191 |
|
|
1192 |
n := 3.0.
|
|
1193 |
m := 5.5.
|
|
1194 |
|
|
1195 |
t := Time millisecondsToRun:[
|
159
|
1196 |
self timesRepeat:[
|
|
1197 |
p := 5 / n + m
|
|
1198 |
]
|
1
|
1199 |
].
|
|
1200 |
Transcript showCr:(t printString)
|
|
1201 |
|
92
|
1202 |
"creates 2 new floats (about 20bytes each) per iteration"
|
77
|
1203 |
"1000000 benchArithmetic"
|
|
1204 |
!
|
|
1205 |
|
|
1206 |
benchArithmetic2
|
85
|
1207 |
"arithmetic speed bench (comp.lang.smalltalk)
|
|
1208 |
(actually, this is a GC benchmark,
|
|
1209 |
it allocates & collects about 20Mb during its run).
|
92
|
1210 |
Since timesRepeat-block is inlined, all time is spent in
|
|
1211 |
float-addition, allocation and gc. Compare the time with
|
|
1212 |
the benchNew/benchBasicNew times.
|
85
|
1213 |
Run this benchmark a few times - its outcome depends on
|
|
1214 |
newSpace fill-grade & cache patterns ..."
|
77
|
1215 |
|
85
|
1216 |
|t|
|
77
|
1217 |
|
|
1218 |
t := Time millisecondsToRun:[
|
159
|
1219 |
|x|
|
|
1220 |
x := 0.0.
|
|
1221 |
1000000 timesRepeat:[x := x + 1.0]
|
77
|
1222 |
].
|
|
1223 |
Transcript showCr:(t printString)
|
|
1224 |
|
92
|
1225 |
"creates 1 new float (about 20bytes) per iteration"
|
77
|
1226 |
"1 benchArithmetic2"
|
|
1227 |
!
|
|
1228 |
|
|
1229 |
benchArithmetic3
|
85
|
1230 |
"arithmetic speed bench
|
|
1231 |
(actually, this is a GC, and block evaluation benchmark.
|
|
1232 |
it allocates & collects about 20Mb during its run;
|
|
1233 |
compare the time to benchBasicNew-time).
|
|
1234 |
Run this benchmark a few times - its outcome depends on
|
|
1235 |
newSpace fill-grade & cache patterns ..."
|
77
|
1236 |
|
85
|
1237 |
|t|
|
77
|
1238 |
|
|
1239 |
t := Time millisecondsToRun:[
|
159
|
1240 |
|x|
|
|
1241 |
x := 0.0.
|
|
1242 |
self timesRepeat:[x := x + 1.0]
|
77
|
1243 |
].
|
|
1244 |
Transcript showCr:(t printString)
|
|
1245 |
|
92
|
1246 |
"creates 1 new float (about 20bytes) per iteration"
|
77
|
1247 |
"1000000 benchArithmetic3"
|
1
|
1248 |
!
|
|
1249 |
|
85
|
1250 |
benchArithmetic4
|
92
|
1251 |
"arithmetic speed bench
|
|
1252 |
currently, the compiler does not use the Float-hint, except that an
|
|
1253 |
added float-check is done in the store operation. Newer versions of
|
|
1254 |
stc will show much better performance here."
|
|
1255 |
|
85
|
1256 |
|t|
|
|
1257 |
|
|
1258 |
t := Time millisecondsToRun:[
|
159
|
1259 |
|x "{ Class: Float }" |
|
|
1260 |
x := 0.0.
|
|
1261 |
1000000 timesRepeat:[x := x + 1.0]
|
85
|
1262 |
].
|
|
1263 |
Transcript showCr:(t printString)
|
|
1264 |
|
92
|
1265 |
"creates 1 new float (about 20bytes) per iteration"
|
213
|
1266 |
"1 benchArithmetic4"
|
85
|
1267 |
!
|
|
1268 |
|
49
|
1269 |
benchNew
|
85
|
1270 |
"instance creation speed bench
|
|
1271 |
(GC benchmark; allocating & collecting about 12Mb).
|
|
1272 |
Run this benchmark a few times - its outcome depends on
|
|
1273 |
newSpace fill-grade & cache patterns ..."
|
49
|
1274 |
|
|
1275 |
|t|
|
|
1276 |
|
|
1277 |
t := Time millisecondsToRun:[
|
159
|
1278 |
self timesRepeat:[
|
|
1279 |
Object new
|
|
1280 |
]
|
49
|
1281 |
].
|
|
1282 |
Transcript showCr:(t printString)
|
|
1283 |
|
77
|
1284 |
"1000000 benchNew"
|
49
|
1285 |
!
|
|
1286 |
|
|
1287 |
benchBasicNew
|
85
|
1288 |
"instance creation speed bench
|
|
1289 |
(GC benchmark; allocating & collecting about 12Mb).
|
|
1290 |
Run this benchmark a few times - its outcome depends on
|
|
1291 |
newSpace fill-grade & cache patterns ..."
|
49
|
1292 |
|
|
1293 |
|t|
|
|
1294 |
|
|
1295 |
t := Time millisecondsToRun:[
|
159
|
1296 |
self timesRepeat:[
|
|
1297 |
Object basicNew
|
|
1298 |
]
|
49
|
1299 |
].
|
|
1300 |
Transcript showCr:(t printString)
|
|
1301 |
|
77
|
1302 |
"1000000 benchBasicNew"
|
49
|
1303 |
!
|
|
1304 |
|
|
1305 |
benchArrayBasicNew
|
85
|
1306 |
"instance creation speed bench
|
|
1307 |
(GC benchmark; allocating & collecting about 12Mb).
|
|
1308 |
Run this benchmark a few times - its outcome depends on
|
|
1309 |
newSpace fill-grade & cache patterns ..."
|
|
1310 |
|
49
|
1311 |
|
|
1312 |
|t|
|
|
1313 |
|
|
1314 |
t := Time millisecondsToRun:[
|
159
|
1315 |
self timesRepeat:[
|
|
1316 |
Array basicNew
|
|
1317 |
]
|
49
|
1318 |
].
|
|
1319 |
Transcript showCr:(t printString)
|
|
1320 |
|
77
|
1321 |
"1000000 benchArrayBasicNew"
|
49
|
1322 |
!
|
|
1323 |
|
|
1324 |
benchArrayNew
|
85
|
1325 |
"instance creation speed bench
|
|
1326 |
(GC benchmark; allocating & collecting about 12Mb).
|
|
1327 |
Run this benchmark a few times - its outcome depends on
|
|
1328 |
newSpace fill-grade & cache patterns ..."
|
49
|
1329 |
|
|
1330 |
|t|
|
|
1331 |
|
|
1332 |
t := Time millisecondsToRun:[
|
159
|
1333 |
self timesRepeat:[
|
|
1334 |
Array new
|
|
1335 |
]
|
49
|
1336 |
].
|
|
1337 |
Transcript showCr:(t printString)
|
|
1338 |
|
77
|
1339 |
"1000000 benchArrayNew"
|
49
|
1340 |
!
|
|
1341 |
|
85
|
1342 |
benchSetCreation
|
92
|
1343 |
"benchmark set grow (from comp.lang.smalltalk).
|
|
1344 |
The loop is inlined, but access to the set is via the
|
|
1345 |
method local (s), which is outside the time-block."
|
85
|
1346 |
|
|
1347 |
|s t|
|
|
1348 |
|
|
1349 |
s := Set new.
|
|
1350 |
t := Time millisecondsToRun:[
|
159
|
1351 |
1 to:4500 do:[:i | s add:i]
|
85
|
1352 |
].
|
|
1353 |
|
|
1354 |
Transcript showCr:(t printString)
|
|
1355 |
|
|
1356 |
"1 benchSetCreation"
|
|
1357 |
!
|
|
1358 |
|
|
1359 |
benchSetCreation2
|
|
1360 |
"benchmark set grow (from comp.lang.smalltalk).
|
92
|
1361 |
This is slightly faster than benchSetCreation, since access to
|
|
1362 |
the set is via a block local variable (s)."
|
85
|
1363 |
|
|
1364 |
|t|
|
|
1365 |
|
|
1366 |
t := Time millisecondsToRun:[
|
159
|
1367 |
|s|
|
|
1368 |
s := Set new.
|
|
1369 |
1 to:4500 do:[:i | s add:i]
|
85
|
1370 |
].
|
|
1371 |
|
|
1372 |
Transcript showCr:(t printString)
|
|
1373 |
|
|
1374 |
"1 benchSetCreation2"
|
|
1375 |
!
|
|
1376 |
|
|
1377 |
benchSetCreation3
|
|
1378 |
"benchmark set grow (from comp.lang.smalltalk).
|
92
|
1379 |
Compare the time with benchSetCreation2 - the set is preallocated,
|
|
1380 |
thus no growing is needed in the loop."
|
85
|
1381 |
|
|
1382 |
|t|
|
|
1383 |
|
|
1384 |
t := Time millisecondsToRun:[
|
159
|
1385 |
|s|
|
|
1386 |
s := Set new:4500.
|
|
1387 |
1 to:4500 do:[:i | s add:i]
|
85
|
1388 |
].
|
|
1389 |
|
|
1390 |
Transcript showCr:(t printString)
|
|
1391 |
|
|
1392 |
"1 benchSetCreation3"
|
|
1393 |
!
|
|
1394 |
|
202
|
1395 |
benchOCCreation1
|
|
1396 |
"benchmark OrderedCollection add (from comp.lang.smalltalk).
|
|
1397 |
The collection is growing (slow)."
|
|
1398 |
|
|
1399 |
|aColl t|
|
|
1400 |
|
|
1401 |
t := Time millisecondsToRun: [
|
|
1402 |
aColl := OrderedCollection new.
|
|
1403 |
1 to: 100000 do: [ :each | aColl add: each ]
|
|
1404 |
].
|
|
1405 |
|
|
1406 |
Transcript showCr:(t printString)
|
|
1407 |
|
|
1408 |
"
|
|
1409 |
1 benchOCCreation1
|
|
1410 |
interpreted (on indy)
|
|
1411 |
1337 1227
|
|
1412 |
compiled (on indy)
|
|
1413 |
best: 295 294 294 293 (try in a workspace)
|
|
1414 |
worst:745 747 (at stack-segment boundary)
|
|
1415 |
compiled (on 486/50)
|
|
1416 |
609 610
|
|
1417 |
"
|
|
1418 |
!
|
|
1419 |
|
|
1420 |
benchOCCreation2
|
|
1421 |
"benchmark OrderedCollection add (from comp.lang.smalltalk).
|
|
1422 |
The collection is preallocated."
|
|
1423 |
|
|
1424 |
|aColl t|
|
|
1425 |
|
|
1426 |
t := Time millisecondsToRun: [
|
|
1427 |
aColl := OrderedCollection new: 100000.
|
|
1428 |
1 to: 100000 do: [ :each | aColl add: each ]
|
|
1429 |
].
|
|
1430 |
|
|
1431 |
Transcript showCr:(t printString)
|
|
1432 |
|
|
1433 |
"
|
|
1434 |
1 benchOCCreation2
|
|
1435 |
interpreted (on indy)
|
|
1436 |
2111 2112 2119 2113
|
|
1437 |
compiled (on indy)
|
|
1438 |
best: 241 240 245 239
|
|
1439 |
worst: 690 (at stack-segment boundary)
|
|
1440 |
compiled (on 486/50)
|
|
1441 |
497 500
|
|
1442 |
"
|
|
1443 |
!
|
|
1444 |
|
|
1445 |
benchOCCreation3
|
|
1446 |
"modified OrderedCollection creation. The collection variable is withing
|
|
1447 |
the block"
|
|
1448 |
|
|
1449 |
|t|
|
|
1450 |
|
|
1451 |
t := Time millisecondsToRun: [
|
|
1452 |
|aColl|
|
|
1453 |
|
|
1454 |
aColl := OrderedCollection new: 100000.
|
|
1455 |
1 to: 100000 do: [ :each | aColl add: each ]
|
|
1456 |
].
|
|
1457 |
|
|
1458 |
Transcript showCr:(t printString)
|
|
1459 |
|
|
1460 |
"
|
|
1461 |
1 benchOCCreation3
|
|
1462 |
interpreted (on indy)
|
|
1463 |
2729 2711 2747
|
|
1464 |
compiled (on indy)
|
|
1465 |
best: 237 239 239 249
|
|
1466 |
worst: 692 (at stack segment boundary)
|
|
1467 |
compiled (on 486/50)
|
|
1468 |
484 486
|
|
1469 |
"
|
|
1470 |
!
|
|
1471 |
|
49
|
1472 |
loopTimes
|
85
|
1473 |
"runs the self low-level loop benchmarks"
|
|
1474 |
|
|
1475 |
Transcript show:'fastSumTo: '; showCr:(Time millisecondsToRun:[1 fastSumTo]).
|
|
1476 |
Transcript show:'nextedLoop: '; showCr:(Time millisecondsToRun:[1 nestedLoop]).
|
|
1477 |
Transcript show:'atAllput: '; showCr:(Time millisecondsToRun:[1 atAllPut]).
|
|
1478 |
Transcript show:'sumAll: '; showCr:(Time millisecondsToRun:[1 sumAll]).
|
|
1479 |
Transcript show:'sumTo: '; showCr:(Time millisecondsToRun:[1 sumTo]).
|
49
|
1480 |
|
|
1481 |
"1 loopTimes"
|
|
1482 |
!
|
|
1483 |
|
1
|
1484 |
sumTo
|
|
1485 |
|val|
|
|
1486 |
|
|
1487 |
100 timesRepeat:[
|
159
|
1488 |
val := 0.
|
|
1489 |
1 to:10000 do:[:i |
|
|
1490 |
val := val + i
|
|
1491 |
]
|
1
|
1492 |
].
|
54
|
1493 |
"Transcript showCr:(Time millisecondsToRun:[1 sumTo])"
|
1
|
1494 |
!
|
|
1495 |
|
|
1496 |
fastSumTo
|
|
1497 |
|val i|
|
|
1498 |
|
|
1499 |
100 timesRepeat:[
|
159
|
1500 |
val := 0.
|
|
1501 |
i := 1.
|
|
1502 |
[i <= 10000] whileTrue:[
|
|
1503 |
val := val + i.
|
|
1504 |
i := i + 1
|
|
1505 |
].
|
1
|
1506 |
].
|
54
|
1507 |
"Transcript showCr:(Time millisecondsToRun:[1 fastSumTo])"
|
1
|
1508 |
!
|
|
1509 |
|
|
1510 |
nestedLoop
|
|
1511 |
|i|
|
|
1512 |
|
|
1513 |
100 timesRepeat:[
|
159
|
1514 |
i := 0.
|
|
1515 |
1 to:100 do:[:l1 |
|
|
1516 |
1 to:100 do:[:l2 |
|
|
1517 |
i := i + 1
|
|
1518 |
]
|
|
1519 |
]
|
1
|
1520 |
]
|
54
|
1521 |
"Transcript showCr:(Time millisecondsToRun:[1 nestedLoop])"
|
1
|
1522 |
!
|
|
1523 |
|
|
1524 |
atAllPut
|
|
1525 |
|vec t|
|
|
1526 |
|
|
1527 |
vec := Array new:100000.
|
|
1528 |
t := Time millisecondsToRun:[
|
159
|
1529 |
1 to:100000 do:[:i |
|
|
1530 |
vec at:i put:7
|
|
1531 |
]
|
1
|
1532 |
].
|
|
1533 |
^ t
|
|
1534 |
|
54
|
1535 |
"Transcript showCr:(1 atAllPut)"
|
1
|
1536 |
!
|
|
1537 |
|
|
1538 |
sumAll
|
|
1539 |
|vec t s|
|
|
1540 |
|
|
1541 |
vec := Array new:100000.
|
|
1542 |
1 to:100000 do:[:i |
|
159
|
1543 |
vec at:i put:7
|
1
|
1544 |
].
|
|
1545 |
s := 0.
|
|
1546 |
t := Time millisecondsToRun:[
|
159
|
1547 |
1 to:100000 do:[:i |
|
|
1548 |
s := s + (vec at:i)
|
|
1549 |
]
|
1
|
1550 |
].
|
|
1551 |
^ t
|
|
1552 |
|
54
|
1553 |
"Transcript showCr:(1 sumAll)"
|
1
|
1554 |
! !
|