changeset 159 | 514c749165c3 |
parent 132 | ab2cfccd218c |
child 202 | 40ca7cc6fb9c |
158:be947d4e7fb2 | 159:514c749165c3 |
---|---|
1 " |
1 " |
2 COPYRIGHT (c) 1988 by Claus Gittinger |
2 COPYRIGHT (c) 1988 by Claus Gittinger |
3 All Rights Reserved |
3 All Rights Reserved |
4 |
4 |
5 This software is furnished under a license and may be used |
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 |
6 only in accordance with the terms of that license and with the |
7 inclusion of the above copyright notice. This software may not |
7 inclusion of the above copyright notice. This software may not |
8 be provided or otherwise made available to, or used by, any |
8 be provided or otherwise made available to, or used by, any |
17 category:'Magnitude-Numbers' |
17 category:'Magnitude-Numbers' |
18 ! |
18 ! |
19 |
19 |
20 Integer comment:' |
20 Integer comment:' |
21 COPYRIGHT (c) 1988 by Claus Gittinger |
21 COPYRIGHT (c) 1988 by Claus Gittinger |
22 All Rights Reserved |
22 All Rights Reserved |
23 |
23 |
24 $Header: /cvs/stx/stx/libbasic/Integer.st,v 1.15 1994-08-22 12:21:15 claus Exp $ |
24 $Header: /cvs/stx/stx/libbasic/Integer.st,v 1.16 1994-10-10 00:26:19 claus Exp $ |
25 '! |
25 '! |
26 |
26 |
27 !Integer class methodsFor:'documentation'! |
27 !Integer class methodsFor:'documentation'! |
28 |
28 |
29 copyright |
29 copyright |
30 " |
30 " |
31 COPYRIGHT (c) 1988 by Claus Gittinger |
31 COPYRIGHT (c) 1988 by Claus Gittinger |
32 All Rights Reserved |
32 All Rights Reserved |
33 |
33 |
34 This software is furnished under a license and may be used |
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 |
35 only in accordance with the terms of that license and with the |
36 inclusion of the above copyright notice. This software may not |
36 inclusion of the above copyright notice. This software may not |
37 be provided or otherwise made available to, or used by, any |
37 be provided or otherwise made available to, or used by, any |
40 " |
40 " |
41 ! |
41 ! |
42 |
42 |
43 version |
43 version |
44 " |
44 " |
45 $Header: /cvs/stx/stx/libbasic/Integer.st,v 1.15 1994-08-22 12:21:15 claus Exp $ |
45 $Header: /cvs/stx/stx/libbasic/Integer.st,v 1.16 1994-10-10 00:26:19 claus Exp $ |
46 " |
46 " |
47 ! |
47 ! |
48 |
48 |
49 documentation |
49 documentation |
50 " |
50 " |
75 numberOfBytes bytes (= digitLength). The additional argument |
75 numberOfBytes bytes (= digitLength). The additional argument |
76 negative specifies if the result should be a negative number. |
76 negative specifies if the result should be a negative number. |
77 The digits can be stored byte-wise into the result, using digitAt:put:" |
77 The digits can be stored byte-wise into the result, using digitAt:put:" |
78 |
78 |
79 ^ (LargeInteger basicNew numberOfDigits:numberOfBytes) |
79 ^ (LargeInteger basicNew numberOfDigits:numberOfBytes) |
80 sign:(negative ifTrue:[-1] ifFalse:[1]) |
80 sign:(negative ifTrue:[-1] ifFalse:[1]) |
81 ! ! |
81 ! ! |
82 |
82 |
83 !Integer class methodsFor:'misc'! |
83 !Integer class methodsFor:'misc'! |
84 |
84 |
85 displayRadix:aNumber |
85 displayRadix:aNumber |
112 |
112 |
113 |d| |
113 |d| |
114 |
114 |
115 d := aFraction denominator. |
115 d := aFraction denominator. |
116 ^ (Fraction numerator:(aFraction numerator + (self * d)) |
116 ^ (Fraction numerator:(aFraction numerator + (self * d)) |
117 denominator:d) reduced |
117 denominator:d) reduced |
118 ! |
118 ! |
119 |
119 |
120 differenceFromFraction:aFraction |
120 differenceFromFraction:aFraction |
121 "sent when a fraction does not know how to subtract the recevier, an integer" |
121 "sent when a fraction does not know how to subtract the recevier, an integer" |
122 |
122 |
123 |d| |
123 |d| |
124 |
124 |
125 d := aFraction denominator. |
125 d := aFraction denominator. |
126 ^ (Fraction numerator:(aFraction numerator - (self * d)) |
126 ^ (Fraction numerator:(aFraction numerator - (self * d)) |
127 denominator:d) reduced |
127 denominator:d) reduced |
128 ! |
128 ! |
129 |
129 |
130 productFromFraction:aFraction |
130 productFromFraction:aFraction |
131 "sent when a fraction does not know how to multiply the recevier, an integer" |
131 "sent when a fraction does not know how to multiply the recevier, an integer" |
132 |
132 |
133 ^ (Fraction numerator:(self * aFraction numerator) |
133 ^ (Fraction numerator:(self * aFraction numerator) |
134 denominator:aFraction denominator) reduced |
134 denominator:aFraction denominator) reduced |
135 ! |
135 ! |
136 |
136 |
137 quotientFromFraction:aFraction |
137 quotientFromFraction:aFraction |
138 "sent when a fraction does not know how to divide the recevier, an integer" |
138 "sent when a fraction does not know how to divide the recevier, an integer" |
139 |
139 |
140 ^ (Fraction numerator:aFraction numerator |
140 ^ (Fraction numerator:aFraction numerator |
141 denominator:(self * aFraction denominator)) reduced |
141 denominator:(self * aFraction denominator)) reduced |
142 ! ! |
142 ! ! |
143 |
143 |
144 !Integer methodsFor:'bit operators'! |
144 !Integer methodsFor:'bit operators'! |
145 |
145 |
146 bitAnd:anInteger |
146 bitAnd:anInteger |
150 |
150 |
151 n := (anInteger digitLength) min:(self digitLength). |
151 n := (anInteger digitLength) min:(self digitLength). |
152 result := self class basicNew numberOfDigits:n. |
152 result := self class basicNew numberOfDigits:n. |
153 |
153 |
154 1 to:n do:[:index | |
154 1 to:n do:[:index | |
155 byte := (anInteger digitAt:index) bitAnd:(self digitAt:index). |
155 byte := (anInteger digitAt:index) bitAnd:(self digitAt:index). |
156 result digitAt:index put:byte. |
156 result digitAt:index put:byte. |
157 ]. |
157 ]. |
158 byte == 0 ifTrue:[ |
158 byte == 0 ifTrue:[ |
159 ^ result normalize |
159 ^ result normalize |
160 ]. |
160 ]. |
161 ^ result |
161 ^ result |
162 ! |
162 ! |
163 |
163 |
164 bitOr:anInteger |
164 bitOr:anInteger |
168 |
168 |
169 n := (anInteger digitLength) max:(self digitLength). |
169 n := (anInteger digitLength) max:(self digitLength). |
170 result := self class basicNew numberOfDigits:n. |
170 result := self class basicNew numberOfDigits:n. |
171 |
171 |
172 1 to:n do:[:index | |
172 1 to:n do:[:index | |
173 byte := (anInteger digitAt:index) bitOr:(self digitAt:index). |
173 byte := (anInteger digitAt:index) bitOr:(self digitAt:index). |
174 result digitAt:index put:byte. |
174 result digitAt:index put:byte. |
175 ]. |
175 ]. |
176 byte == 0 ifTrue:[ |
176 byte == 0 ifTrue:[ |
177 ^ result normalize |
177 ^ result normalize |
178 ]. |
178 ]. |
179 ^ result |
179 ^ result |
180 ! |
180 ! |
181 |
181 |
182 bitXor:anInteger |
182 bitXor:anInteger |
186 |
186 |
187 n := (anInteger digitLength) max:(self digitLength). |
187 n := (anInteger digitLength) max:(self digitLength). |
188 result := self class basicNew numberOfDigits:n. |
188 result := self class basicNew numberOfDigits:n. |
189 |
189 |
190 1 to:n do:[:index | |
190 1 to:n do:[:index | |
191 byte := (anInteger digitAt:index) bitXor:(self digitAt:index). |
191 byte := (anInteger digitAt:index) bitXor:(self digitAt:index). |
192 result digitAt:index put:byte. |
192 result digitAt:index put:byte. |
193 ]. |
193 ]. |
194 byte == 0 ifTrue:[ |
194 byte == 0 ifTrue:[ |
195 ^ result normalize |
195 ^ result normalize |
196 ]. |
196 ]. |
197 ^ result |
197 ^ result |
198 ! |
198 ! |
199 |
199 |
200 highBit |
200 highBit |
221 ! |
221 ! |
222 |
222 |
223 bitAt:index |
223 bitAt:index |
224 "return the value of the index's bit (index starts at 1). |
224 "return the value of the index's bit (index starts at 1). |
225 Notice: the result of bitShift: on negative receivers is not |
225 Notice: the result of bitShift: on negative receivers is not |
226 defined in the language standard (since the implementation |
226 defined in the language standard (since the implementation |
227 is free to choose any internal representation for integers)" |
227 is free to choose any internal representation for integers)" |
228 |
228 |
229 |i "{Class: SmallInteger}"| |
229 |i "{Class: SmallInteger}"| |
230 |
230 |
231 i := index - 1. |
231 i := index - 1. |
232 ^ (self digitAt:(i // 8 + 1)) bitAt:(i \\ 8 + 1) |
232 ^ (self digitAt:(i // 8 + 1)) bitAt:(i \\ 8 + 1) |
255 bitShift:shiftCount |
255 bitShift:shiftCount |
256 "return the value of the receiver shifted by shiftCount bits; |
256 "return the value of the receiver shifted by shiftCount bits; |
257 leftShift if shiftCount > 0; rightShift otherwise. |
257 leftShift if shiftCount > 0; rightShift otherwise. |
258 |
258 |
259 Notice: the result of bitShift: on negative receivers is not |
259 Notice: the result of bitShift: on negative receivers is not |
260 defined in the language standard (since the implementation |
260 defined in the language standard (since the implementation |
261 is free to choose any internal representation for integers)" |
261 is free to choose any internal representation for integers)" |
262 |
262 |
263 |result |
263 |result |
264 prev "{ Class: SmallInteger }" |
264 prev "{ Class: SmallInteger }" |
265 next "{ Class: SmallInteger }" |
265 next "{ Class: SmallInteger }" |
266 byte "{ Class: SmallInteger }" |
266 byte "{ Class: SmallInteger }" |
269 digitShift "{ Class: SmallInteger }" |
269 digitShift "{ Class: SmallInteger }" |
270 n "{ Class: SmallInteger }" |
270 n "{ Class: SmallInteger }" |
271 nn "{ Class: SmallInteger }"| |
271 nn "{ Class: SmallInteger }"| |
272 |
272 |
273 shiftCount > 0 ifTrue:[ |
273 shiftCount > 0 ifTrue:[ |
274 "left shift" |
274 "left shift" |
275 |
275 |
276 digitShift := shiftCount // 8. |
276 digitShift := shiftCount // 8. |
277 bitShift := shiftCount \\ 8. |
277 bitShift := shiftCount \\ 8. |
278 n := self digitLength. |
278 n := self digitLength. |
279 |
279 |
280 " |
280 " |
281 modulu 8 shifts can be done faster ... |
281 modulu 8 shifts can be done faster ... |
282 " |
282 " |
283 bitShift == 0 ifTrue:[ |
283 bitShift == 0 ifTrue:[ |
284 result := self class basicNew numberOfDigits:n + digitShift. |
284 result := self class basicNew numberOfDigits:n + digitShift. |
285 result sign:self sign. |
285 result sign:self sign. |
286 result digits replaceFrom:(digitShift + 1) with:self digits. |
286 result digits replaceFrom:(digitShift + 1) with:self digits. |
287 " |
287 " |
288 no normalize needed, since receiver was already normalized |
288 no normalize needed, since receiver was already normalized |
289 " |
289 " |
290 ^ result |
290 ^ result |
291 ]. |
291 ]. |
292 |
292 |
293 " |
293 " |
294 less-than-8 shifts can be done faster ... |
294 less-than-8 shifts can be done faster ... |
295 " |
295 " |
296 digitShift == 0 ifTrue:[ |
296 digitShift == 0 ifTrue:[ |
297 n := n + 1. |
297 n := n + 1. |
298 result := self class basicNew numberOfDigits:n. |
298 result := self class basicNew numberOfDigits:n. |
299 result sign:self sign. |
299 result sign:self sign. |
300 prev := 0. |
300 prev := 0. |
301 1 to:n-1 do:[:index | |
301 1 to:n-1 do:[:index | |
302 byte := self digitAt:index. |
302 byte := self digitAt:index. |
303 byte := (byte bitShift:bitShift) bitOr:prev. |
303 byte := (byte bitShift:bitShift) bitOr:prev. |
304 result digitAt:index put:(byte bitAnd:16rFF). |
304 result digitAt:index put:(byte bitAnd:16rFF). |
305 prev := byte bitShift:-8. |
305 prev := byte bitShift:-8. |
306 ]. |
306 ]. |
307 result digitAt:n put:prev. |
307 result digitAt:n put:prev. |
308 " |
308 " |
309 might have stored a 0-byte ... |
309 might have stored a 0-byte ... |
310 " |
310 " |
311 prev == 0 ifTrue:[ |
311 prev == 0 ifTrue:[ |
312 ^ result normalize |
312 ^ result normalize |
313 ]. |
313 ]. |
314 ^ result. |
314 ^ result. |
315 ]. |
315 ]. |
316 |
316 |
317 " |
317 " |
318 slow case ... |
318 slow case ... |
319 " |
319 " |
320 n := n + digitShift + 1. |
320 n := n + digitShift + 1. |
321 result := self class basicNew numberOfDigits:n. |
321 result := self class basicNew numberOfDigits:n. |
322 result sign:self sign. |
322 result sign:self sign. |
323 |
323 |
324 byte := ((self digitAt:1) bitShift:bitShift) bitAnd:16rFF. |
324 byte := ((self digitAt:1) bitShift:bitShift) bitAnd:16rFF. |
325 result digitAt:(digitShift + 1) put:byte. |
325 result digitAt:(digitShift + 1) put:byte. |
326 revShift := -8 + bitShift. |
326 revShift := -8 + bitShift. |
327 2 to:(self digitLength) do:[:index | |
327 2 to:(self digitLength) do:[:index | |
328 byte := (self digitAt:index) bitShift:bitShift. |
328 byte := (self digitAt:index) bitShift:bitShift. |
329 byte := byte bitOr:((self digitAt:index-1) bitShift:revShift). |
329 byte := byte bitOr:((self digitAt:index-1) bitShift:revShift). |
330 byte := byte bitAnd:16rFF. |
330 byte := byte bitAnd:16rFF. |
331 result digitAt:(index + digitShift) put:byte |
331 result digitAt:(index + digitShift) put:byte |
332 ]. |
332 ]. |
333 " |
333 " |
334 might have stored a 0-byte ... |
334 might have stored a 0-byte ... |
335 " |
335 " |
336 byte == 0 ifTrue:[ |
336 byte == 0 ifTrue:[ |
337 ^ result normalize |
337 ^ result normalize |
338 ]. |
338 ]. |
339 ^ result |
339 ^ result |
340 ]. |
340 ]. |
341 |
341 |
342 shiftCount < 0 ifTrue:[ |
342 shiftCount < 0 ifTrue:[ |
343 "right shift" |
343 "right shift" |
344 |
344 |
345 digitShift := shiftCount negated // 8. |
345 digitShift := shiftCount negated // 8. |
346 bitShift := shiftCount negated \\ 8. |
346 bitShift := shiftCount negated \\ 8. |
347 n := self digitLength. |
347 n := self digitLength. |
348 |
348 |
349 digitShift >= n ifTrue:[ |
349 digitShift >= n ifTrue:[ |
350 ^ 0 |
350 ^ 0 |
351 ]. |
351 ]. |
352 |
352 |
353 " |
353 " |
354 modulu 8 shifts can be done faster ... |
354 modulu 8 shifts can be done faster ... |
355 " |
355 " |
356 bitShift == 0 ifTrue:[ |
356 bitShift == 0 ifTrue:[ |
357 n := n-digitShift. |
357 n := n-digitShift. |
358 result := self class basicNew numberOfDigits:n. |
358 result := self class basicNew numberOfDigits:n. |
359 result sign:self sign. |
359 result sign:self sign. |
360 result digits replaceFrom:1 with:self digits startingAt:(digitShift + 1) . |
360 result digits replaceFrom:1 with:self digits startingAt:(digitShift + 1) . |
361 n <= 4 ifTrue:[ |
361 n <= 4 ifTrue:[ |
362 ^ result normalize |
362 ^ result normalize |
363 ]. |
363 ]. |
364 ^ result |
364 ^ result |
365 ]. |
365 ]. |
366 |
366 |
367 " |
367 " |
368 less-than-8 shifts can be done faster ... |
368 less-than-8 shifts can be done faster ... |
369 " |
369 " |
370 digitShift == 0 ifTrue:[ |
370 digitShift == 0 ifTrue:[ |
371 result := self class basicNew numberOfDigits:n. |
371 result := self class basicNew numberOfDigits:n. |
372 result sign:self sign. |
372 result sign:self sign. |
373 prev := 0. |
373 prev := 0. |
374 bitShift := bitShift negated. |
374 bitShift := bitShift negated. |
375 revShift := 8 + bitShift. |
375 revShift := 8 + bitShift. |
376 n to:1 by:-1 do:[:index | |
376 n to:1 by:-1 do:[:index | |
377 byte := self digitAt:index. |
377 byte := self digitAt:index. |
378 next := (byte bitShift:revShift) bitAnd:16rFF. |
378 next := (byte bitShift:revShift) bitAnd:16rFF. |
379 byte := (byte bitShift:bitShift) bitOr:prev. |
379 byte := (byte bitShift:bitShift) bitOr:prev. |
380 result digitAt:index put:(byte bitAnd:16rFF). |
380 result digitAt:index put:(byte bitAnd:16rFF). |
381 prev := next. |
381 prev := next. |
382 ]. |
382 ]. |
383 (n <= 5) ifTrue:[ |
383 (n <= 5) ifTrue:[ |
384 ^ result normalize |
384 ^ result normalize |
385 ]. |
385 ]. |
386 ^ result |
386 ^ result |
387 ]. |
387 ]. |
388 |
388 |
389 " |
389 " |
390 slow case ... |
390 slow case ... |
391 " |
391 " |
392 nn := n-digitShift. |
392 nn := n-digitShift. |
393 result := self class basicNew numberOfDigits:nn. |
393 result := self class basicNew numberOfDigits:nn. |
394 result sign:self sign. |
394 result sign:self sign. |
395 |
395 |
396 prev := 0. |
396 prev := 0. |
397 bitShift := bitShift negated. |
397 bitShift := bitShift negated. |
398 revShift := 8 + bitShift. |
398 revShift := 8 + bitShift. |
399 n to:(1 + digitShift) by:-1 do:[:index | |
399 n to:(1 + digitShift) by:-1 do:[:index | |
400 byte := self digitAt:index. |
400 byte := self digitAt:index. |
401 next := (byte bitShift:revShift) bitAnd:16rFF. |
401 next := (byte bitShift:revShift) bitAnd:16rFF. |
402 byte := (byte bitShift:bitShift) bitOr:prev. |
402 byte := (byte bitShift:bitShift) bitOr:prev. |
403 result digitAt:(index - digitShift) put:byte. |
403 result digitAt:(index - digitShift) put:byte. |
404 prev := next. |
404 prev := next. |
405 ]. |
405 ]. |
406 "the last stored byte ..." |
406 "the last stored byte ..." |
407 ^ result normalize |
407 ^ result normalize |
408 ]. |
408 ]. |
409 |
409 |
410 ^ self "no shift" |
410 ^ self "no shift" |
411 ! ! |
411 ! ! |
412 |
412 |
469 |num count| |
469 |num count| |
470 |
470 |
471 num := self. |
471 num := self. |
472 count := n. |
472 count := n. |
473 [count > 1] whileTrue:[ |
473 [count > 1] whileTrue:[ |
474 num := num // 256. |
474 num := num // 256. |
475 count := count - 1 |
475 count := count - 1 |
476 ]. |
476 ]. |
477 ^ num \\ 256 |
477 ^ num \\ 256 |
478 |
478 |
479 " |
479 " |
480 16r44332211 digitAt:1 |
480 16r44332211 digitAt:1 |
497 |
497 |
498 factorial |
498 factorial |
499 "return 1*2*3...*self" |
499 "return 1*2*3...*self" |
500 |
500 |
501 (self > 2) ifTrue:[ |
501 (self > 2) ifTrue:[ |
502 ^ self * (self - 1) factorial |
502 ^ self * (self - 1) factorial |
503 ]. |
503 ]. |
504 ^ self |
504 ^ self |
505 ! |
505 ! |
506 |
506 |
507 gcd:anInteger |
507 gcd:anInteger |
510 |ttt selfInteger temp| |
510 |ttt selfInteger temp| |
511 |
511 |
512 ttt := anInteger. |
512 ttt := anInteger. |
513 selfInteger := self. |
513 selfInteger := self. |
514 [ttt ~~ 0] whileTrue:[ |
514 [ttt ~~ 0] whileTrue:[ |
515 temp := selfInteger \\ ttt. |
515 temp := selfInteger \\ ttt. |
516 selfInteger := ttt. |
516 selfInteger := ttt. |
517 ttt := temp |
517 ttt := temp |
518 ]. |
518 ]. |
519 ^ selfInteger |
519 ^ selfInteger |
520 |
520 |
521 " |
521 " |
522 65 gcd:15 |
522 65 gcd:15 |
540 "dont use this method if you need fibionacci numbers - |
540 "dont use this method if you need fibionacci numbers - |
541 this method is for benchmarking purposes only. |
541 this method is for benchmarking purposes only. |
542 (use fastFib instead and dont ever try 60 fib ...)" |
542 (use fastFib instead and dont ever try 60 fib ...)" |
543 |
543 |
544 (self > 1) ifTrue:[ |
544 (self > 1) ifTrue:[ |
545 ^ (self - 1) fib + (self - 2) fib |
545 ^ (self - 1) fib + (self - 2) fib |
546 ]. |
546 ]. |
547 ^ 1 |
547 ^ 1 |
548 |
548 |
549 " |
549 " |
550 Transcript showCr:(Time millisecondsToRun:[30 fib]) |
550 Transcript showCr:(Time millisecondsToRun:[30 fib]) |
574 |fib| |
574 |fib| |
575 |
575 |
576 self <= 1 ifTrue:[^ 1]. |
576 self <= 1 ifTrue:[^ 1]. |
577 |
577 |
578 fibCache size >= self ifTrue:[ |
578 fibCache size >= self ifTrue:[ |
579 ^ fibCache at:self |
579 ^ fibCache at:self |
580 ]. |
580 ]. |
581 fib := ((self - 2) fibUsingCache:fibCache) + ((self - 1) fibUsingCache:fibCache). |
581 fib := ((self - 2) fibUsingCache:fibCache) + ((self - 1) fibUsingCache:fibCache). |
582 |
582 |
583 fibCache grow:self. |
583 fibCache grow:self. |
584 fibCache at:self put:fib. |
584 fibCache at:self put:fib. |
630 "return a string for displaying in a view (as in inspector). |
630 "return a string for displaying in a view (as in inspector). |
631 The output radix is usually 10, but can be changed by setting |
631 The output radix is usually 10, but can be changed by setting |
632 DefaultDisplayRadix (see Integer>>displayRadix:)" |
632 DefaultDisplayRadix (see Integer>>displayRadix:)" |
633 |
633 |
634 (DefaultDisplayRadix isNil or:[DefaultDisplayRadix == 10]) ifTrue:[ |
634 (DefaultDisplayRadix isNil or:[DefaultDisplayRadix == 10]) ifTrue:[ |
635 ^ self printString |
635 ^ self printString |
636 ]. |
636 ]. |
637 ^ self radixPrintStringRadix:DefaultDisplayRadix |
637 ^ self radixPrintStringRadix:DefaultDisplayRadix |
638 |
638 |
639 " |
639 " |
640 Integer displayRadix:16. 12345 |
640 Integer displayRadix:16. 12345 |
661 radix (without the initial XXr)" |
661 radix (without the initial XXr)" |
662 |
662 |
663 |num s "rx leftPart"| |
663 |num s "rx leftPart"| |
664 |
664 |
665 (aRadix between:2 and:36) ifFalse:[ |
665 (aRadix between:2 and:36) ifFalse:[ |
666 self error:'invalid radix'. |
666 self error:'invalid radix'. |
667 ^ self printStringRadix:10 |
667 ^ self printStringRadix:10 |
668 ]. |
668 ]. |
669 |
669 |
670 (self = 0) ifTrue:[^ '0']. |
670 (self = 0) ifTrue:[^ '0']. |
671 (self < 0) ifTrue:[ |
671 (self < 0) ifTrue:[ |
672 ^ '-' , (self negated printStringRadix:aRadix) |
672 ^ '-' , (self negated printStringRadix:aRadix) |
673 ]. |
673 ]. |
674 |
674 |
675 " |
675 " |
676 claus: changed it from a recursive algorithm; |
676 claus: changed it from a recursive algorithm; |
677 (it used to trigger stack-overflow exceptions when printing |
677 (it used to trigger stack-overflow exceptions when printing |
685 |
685 |
686 num := self. |
686 num := self. |
687 s := (Character digitValue:(num \\ aRadix)) asString. |
687 s := (Character digitValue:(num \\ aRadix)) asString. |
688 num := num // aRadix. |
688 num := num // aRadix. |
689 [num ~= 0] whileTrue:[ |
689 [num ~= 0] whileTrue:[ |
690 s := (Character digitValue:(num \\ aRadix)) asString , s. |
690 s := (Character digitValue:(num \\ aRadix)) asString , s. |
691 num := num // aRadix. |
691 num := num // aRadix. |
692 ]. |
692 ]. |
693 ^ s |
693 ^ s |
694 ! |
694 ! |
695 |
695 |
696 printStringRadix:aRadix size:sz fill:fillCharacter |
696 printStringRadix:aRadix size:sz fill:fillCharacter |
700 |
700 |
701 |s| |
701 |s| |
702 |
702 |
703 s := self printStringRadix:aRadix. |
703 s := self printStringRadix:aRadix. |
704 s size < sz ifTrue:[ |
704 s size < sz ifTrue:[ |
705 s := ((String new:(sz - s size)) atAllPut:fillCharacter) , s |
705 s := ((String new:(sz - s size)) atAllPut:fillCharacter) , s |
706 ]. |
706 ]. |
707 ^ s |
707 ^ s |
708 |
708 |
709 " |
709 " |
710 1024 printStringRadix:16 size:4 fill:$0 |
710 1024 printStringRadix:16 size:4 fill:$0 |
714 ! ! |
714 ! ! |
715 |
715 |
716 !Integer class methodsFor:'instance creation'! |
716 !Integer class methodsFor:'instance creation'! |
717 |
717 |
718 readFrom:aStream radix:radix |
718 readFrom:aStream radix:radix |
719 "return the next Integer from the (character-)stream aStream in radix; |
719 "return the next unsigned Integer from the (character-)stream aStream |
720 (assumes that the initial XXr has already been read) |
720 in radix; (assumes that the initial XXr has already been read). |
721 no whitespace-skipping; returns 0 if no number available" |
721 No whitespace-skipping is done. |
722 Returns 0 if no number available" |
|
722 |
723 |
723 |nextChar value| |
724 |nextChar value| |
724 |
725 |
725 nextChar := aStream peek. |
726 nextChar := aStream peek. |
726 value := 0. |
727 value := 0. |
727 [nextChar notNil and:[nextChar isDigitRadix:radix]] whileTrue:[ |
728 [nextChar notNil and:[nextChar isDigitRadix:radix]] whileTrue:[ |
728 value := value * radix + nextChar digitValue. |
729 value := value * radix + nextChar digitValue. |
729 nextChar := aStream nextPeek |
730 nextChar := aStream nextPeek |
730 ]. |
731 ]. |
731 ^ value |
732 ^ value |
733 |
|
734 " |
|
735 Integer readFrom:(ReadStream on:'12345') radix:10 |
|
736 Integer readFrom:(ReadStream on:'FFFF') radix:16 |
|
737 Integer readFrom:(ReadStream on:'1010') radix:2 |
|
738 " |
|
732 ! |
739 ! |
733 |
740 |
734 readFrom:aStream |
741 readFrom:aStream |
735 "return the next Integer from the (character-)stream aStream, |
742 "return the next Integer from the (character-)stream aStream, |
736 handling initial XXr for arbitrary radix numbers and initial |
743 handling initial XXr for arbitrary radix numbers and initial |
737 sign. |
744 sign. |
738 skipping all whitespace first; return nil if no number" |
745 Also, all initial whitespace is skipped. |
746 Q: If the string does not represent a valid integer number, |
|
747 should nil be returned, or an error be raised (see Object>>readFrom:) |
|
748 A: for now, return nil. |
|
749 This may be changed, once the ANSI spec defines this." |
|
739 |
750 |
740 |nextChar value negative| |
751 |nextChar value negative| |
741 |
752 |
742 nextChar := aStream skipSeparators. |
753 nextChar := aStream skipSeparators. |
743 (nextChar == $-) ifTrue:[ |
754 (nextChar == $-) ifTrue:[ |
744 negative := true. |
755 negative := true. |
745 nextChar := aStream nextPeek |
756 nextChar := aStream nextPeek |
746 ] ifFalse:[ |
757 ] ifFalse:[ |
747 negative := false |
758 negative := false |
748 ]. |
759 ]. |
749 nextChar isDigit ifFalse:[ ^ nil]. |
760 nextChar isDigit ifFalse:[ |
750 value := Integer readFrom:aStream radix:10. |
761 " |
762 the string does not represent an integer |
|
763 " |
|
764 ^ nil |
|
765 " |
|
766 ^ self error:'expected Integer'. |
|
767 " |
|
768 ]. |
|
769 value := self readFrom:aStream radix:10. |
|
751 nextChar := aStream peek. |
770 nextChar := aStream peek. |
752 ((nextChar == $r) or:[ nextChar == $R]) ifTrue:[ |
771 ((nextChar == $r) or:[ nextChar == $R]) ifTrue:[ |
753 "-xxr<number> is invalid; should be xxr-<val>" |
772 "-xxr<number> is invalid; should be xxr-<val>" |
754 |
773 |
755 negative ifTrue:[ |
774 negative ifTrue:[ |
756 'invalid (negative) radix ignored' errorPrintNewline. |
775 'invalid (negative) radix ignored' errorPrintNL. |
757 negative := false |
776 negative := false |
758 ]. |
777 ]. |
759 aStream next. |
778 aStream next. |
760 value := Integer readFrom:aStream radix:value |
779 value := self readFrom:aStream radix:value |
761 ]. |
780 ]. |
762 negative ifTrue:[ |
781 negative ifTrue:[ |
763 ^ value negated |
782 ^ value negated |
764 ]. |
783 ]. |
765 ^ value |
784 ^ value |
785 |
|
786 " |
|
787 Integer readFrom:(ReadStream on:'12345') |
|
788 Integer readFrom:(ReadStream on:'16rFFFF') |
|
789 Integer readFrom:(ReadStream on:'12345.1234') |
|
790 Integer readFrom:(ReadStream on:'foo') |
|
791 |
|
792 Object errorSignal handle:[:ex | ex returnWith:0] |
|
793 do:[Integer readFrom:(ReadStream on:'foo')] |
|
794 " |
|
766 ! ! |
795 ! ! |
767 |
796 |
768 !Integer methodsFor:'benchmarking'! |
797 !Integer methodsFor:'benchmarking'! |
769 |
798 |
770 sieve |
799 sieve |
777 |
806 |
778 Transcript show:'Sieve running ...'. |
807 Transcript show:'Sieve running ...'. |
779 Transcript cr. |
808 Transcript cr. |
780 |
809 |
781 time := Time millisecondsToRun:[ |
810 time := Time millisecondsToRun:[ |
782 self timesRepeat:[ |
811 self timesRepeat:[ |
783 count := 0. |
812 count := 0. |
784 flags atAllPut:1. |
813 flags atAllPut:1. |
785 i := 1. |
814 i := 1. |
786 num timesRepeat:[ |
815 num timesRepeat:[ |
787 (flags at:i) == 1 ifTrue:[ |
816 (flags at:i) == 1 ifTrue:[ |
788 prime := i + i + 3. |
817 prime := i + i + 3. |
789 k := i + prime. |
818 k := i + prime. |
790 [k <= num] whileTrue:[ |
819 [k <= num] whileTrue:[ |
791 flags at:k put:0. |
820 flags at:k put:0. |
792 k := k + prime |
821 k := k + prime |
793 ]. |
822 ]. |
794 count := count + 1 |
823 count := count + 1 |
795 ]. |
824 ]. |
796 i := i + 1 |
825 i := i + 1 |
797 ]. |
826 ]. |
798 ]. |
827 ]. |
799 ]. |
828 ]. |
800 Transcript show:'Sieve in Smalltalk: '. |
829 Transcript show:'Sieve in Smalltalk: '. |
801 Transcript show:self printString. |
830 Transcript show:self printString. |
802 Transcript showCr:' iteration(s).'. |
831 Transcript showCr:' iteration(s).'. |
803 Transcript show:'found '. |
832 Transcript show:'found '. |
825 |
854 |
826 Transcript show:'Sieve running ...'. |
855 Transcript show:'Sieve running ...'. |
827 Transcript cr. |
856 Transcript cr. |
828 |
857 |
829 time := Time millisecondsToRun:[ |
858 time := Time millisecondsToRun:[ |
830 self timesRepeat:[ |
859 self timesRepeat:[ |
831 count := 0. |
860 count := 0. |
832 flags atAllPut:1. |
861 flags atAllPut:1. |
833 i := 1. |
862 i := 1. |
834 num timesRepeat:[ |
863 num timesRepeat:[ |
835 (flags at:i) == 1 ifTrue:[ |
864 (flags at:i) == 1 ifTrue:[ |
836 prime := i + i + 3. |
865 prime := i + i + 3. |
837 k := i + prime. |
866 k := i + prime. |
838 [k <= num] whileTrue:[ |
867 [k <= num] whileTrue:[ |
839 flags at:k put:0. |
868 flags at:k put:0. |
840 k := k + prime |
869 k := k + prime |
841 ]. |
870 ]. |
842 count := count + 1 |
871 count := count + 1 |
843 ]. |
872 ]. |
844 i := i + 1 |
873 i := i + 1 |
845 ]. |
874 ]. |
846 ]. |
875 ]. |
847 ]. |
876 ]. |
848 Transcript show:'Sieve in Smalltalk: '. |
877 Transcript show:'Sieve in Smalltalk: '. |
849 Transcript show:self printString. |
878 Transcript show:self printString. |
850 Transcript showCr:' iteration(s).'. |
879 Transcript showCr:' iteration(s).'. |
851 Transcript show:'found '. |
880 Transcript show:'found '. |
870 "lots of recursion for testing send with arg" |
899 "lots of recursion for testing send with arg" |
871 |
900 |
872 |t| |
901 |t| |
873 |
902 |
874 t := Time millisecondsToRun:[ |
903 t := Time millisecondsToRun:[ |
875 1 recur1:15 |
904 1 recur1:15 |
876 ]. |
905 ]. |
877 Transcript showCr:(t printString) |
906 Transcript showCr:(t printString) |
878 |
907 |
879 "1 recur1" |
908 "1 recur1" |
880 ! |
909 ! |
881 |
910 |
882 doRecur2 |
911 doRecur2 |
883 "lots of recursion for testing send without arg" |
912 "lots of recursion for testing send without arg" |
884 |
913 |
885 (self > 0) ifTrue:[ |
914 (self > 0) ifTrue:[ |
886 (self - 1) doRecur2. |
915 (self - 1) doRecur2. |
887 ^ (self - 1) doRecur2 |
916 ^ (self - 1) doRecur2 |
888 ] |
917 ] |
889 ! |
918 ! |
890 |
919 |
891 recur2 |
920 recur2 |
892 "lots of recursion for testing send without arg" |
921 "lots of recursion for testing send without arg" |
893 |
922 |
894 |t| |
923 |t| |
895 |
924 |
896 t := Time millisecondsToRun:[ |
925 t := Time millisecondsToRun:[ |
897 15 doRecur2 |
926 15 doRecur2 |
898 ]. |
927 ]. |
899 Transcript showCr:(t printString) |
928 Transcript showCr:(t printString) |
900 |
929 |
901 "1 recur2" |
930 "1 recur2" |
902 ! |
931 ! |
905 "evaluating a simple block" |
934 "evaluating a simple block" |
906 |
935 |
907 |t| |
936 |t| |
908 |
937 |
909 t := Time millisecondsToRun:[ |
938 t := Time millisecondsToRun:[ |
910 |b| |
939 |b| |
911 |
940 |
912 b := [99]. |
941 b := [99]. |
913 1000000 timesRepeat:[b value] |
942 1000000 timesRepeat:[b value] |
914 ]. |
943 ]. |
915 Transcript showCr:(t printString) |
944 Transcript showCr:(t printString) |
916 |
945 |
917 "1 blockEvaluation" |
946 "1 blockEvaluation" |
918 ! |
947 ! |
921 "count down - notice, that index is a method var" |
950 "count down - notice, that index is a method var" |
922 |
951 |
923 |t index| |
952 |t index| |
924 |
953 |
925 t := Time millisecondsToRun:[ |
954 t := Time millisecondsToRun:[ |
926 index := 100000. |
955 index := 100000. |
927 [index > 0] whileTrue:[ |
956 [index > 0] whileTrue:[ |
928 index := index - 1 |
957 index := index - 1 |
929 ]. |
958 ]. |
930 ]. |
959 ]. |
931 Transcript showCr:(t printString) |
960 Transcript showCr:(t printString) |
932 |
961 |
933 "1 countDown" |
962 "1 countDown" |
934 ! |
963 ! |
937 "count down - notice, that index is a block var" |
966 "count down - notice, that index is a block var" |
938 |
967 |
939 |t| |
968 |t| |
940 |
969 |
941 t := Time millisecondsToRun:[ |
970 t := Time millisecondsToRun:[ |
942 |index| |
971 |index| |
943 |
972 |
944 index := 100000. |
973 index := 100000. |
945 [index > 0] whileTrue:[ |
974 [index > 0] whileTrue:[ |
946 index := index - 1 |
975 index := index - 1 |
947 ]. |
976 ]. |
948 ]. |
977 ]. |
949 Transcript showCr:(t printString) |
978 Transcript showCr:(t printString) |
950 |
979 |
951 "1 countDown2" |
980 "1 countDown2" |
952 ! |
981 ! |
969 (timesRepeat is not inlined), another half in the send itself." |
998 (timesRepeat is not inlined), another half in the send itself." |
970 |
999 |
971 |t| |
1000 |t| |
972 |
1001 |
973 t := Time millisecondsToRun:[ |
1002 t := Time millisecondsToRun:[ |
974 self timesRepeat:[ |
1003 self timesRepeat:[ |
975 self noopSelf |
1004 self noopSelf |
976 ]. |
1005 ]. |
977 ]. |
1006 ]. |
978 Transcript showCr:(t printString) |
1007 Transcript showCr:(t printString) |
979 |
1008 |
980 "1000000 send" |
1009 "1000000 send" |
981 ! |
1010 ! |
985 Almost all time is spent in the send (since timesRepeat is inlined here)." |
1014 Almost all time is spent in the send (since timesRepeat is inlined here)." |
986 |
1015 |
987 |t| |
1016 |t| |
988 |
1017 |
989 t := Time millisecondsToRun:[ |
1018 t := Time millisecondsToRun:[ |
990 1000000 timesRepeat:[ |
1019 1000000 timesRepeat:[ |
991 self noopSelf |
1020 self noopSelf |
992 ]. |
1021 ]. |
993 ]. |
1022 ]. |
994 Transcript showCr:(t printString) |
1023 Transcript showCr:(t printString) |
995 |
1024 |
996 "1 send2" |
1025 "1 send2" |
997 ! |
1026 ! |
1001 Almost all time is spent in the send (since timesRepeat is inlined here)." |
1030 Almost all time is spent in the send (since timesRepeat is inlined here)." |
1002 |
1031 |
1003 |t| |
1032 |t| |
1004 |
1033 |
1005 t := Time millisecondsToRun:[ |
1034 t := Time millisecondsToRun:[ |
1006 1000000 timesRepeat:[ |
1035 1000000 timesRepeat:[ |
1007 self noopNil |
1036 self noopNil |
1008 ]. |
1037 ]. |
1009 ]. |
1038 ]. |
1010 Transcript showCr:(t printString) |
1039 Transcript showCr:(t printString) |
1011 |
1040 |
1012 "1 send3" |
1041 "1 send3" |
1013 ! |
1042 ! |
1017 Almost all time is spent in the send (since timesRepeat is inlined here)." |
1046 Almost all time is spent in the send (since timesRepeat is inlined here)." |
1018 |
1047 |
1019 |t| |
1048 |t| |
1020 |
1049 |
1021 t := Time millisecondsToRun:[ |
1050 t := Time millisecondsToRun:[ |
1022 1000000 timesRepeat:[ |
1051 1000000 timesRepeat:[ |
1023 self noopTrue |
1052 self noopTrue |
1024 ]. |
1053 ]. |
1025 ]. |
1054 ]. |
1026 Transcript showCr:(t printString) |
1055 Transcript showCr:(t printString) |
1027 |
1056 |
1028 "1 send4" |
1057 "1 send4" |
1029 ! |
1058 ! |
1034 (timesRepeat is not inlined), another half in the send itself." |
1063 (timesRepeat is not inlined), another half in the send itself." |
1035 |
1064 |
1036 |t| |
1065 |t| |
1037 |
1066 |
1038 t := Time millisecondsToRun:[ |
1067 t := Time millisecondsToRun:[ |
1039 |a| |
1068 |a| |
1040 |
1069 |
1041 a := 1->2. |
1070 a := 1->2. |
1042 self timesRepeat:[a key]. |
1071 self timesRepeat:[a key]. |
1043 ]. |
1072 ]. |
1044 Transcript showCr:(t printString) |
1073 Transcript showCr:(t printString) |
1045 |
1074 |
1046 "1000000 instAccess1" |
1075 "1000000 instAccess1" |
1047 ! |
1076 ! |
1052 Almost all time is spent in the send (since timesRepeat is inlined here)." |
1081 Almost all time is spent in the send (since timesRepeat is inlined here)." |
1053 |
1082 |
1054 |t| |
1083 |t| |
1055 |
1084 |
1056 t := Time millisecondsToRun:[ |
1085 t := Time millisecondsToRun:[ |
1057 |a| |
1086 |a| |
1058 |
1087 |
1059 a := 1->2. |
1088 a := 1->2. |
1060 1000000 timesRepeat:[a key]. |
1089 1000000 timesRepeat:[a key]. |
1061 ]. |
1090 ]. |
1062 Transcript showCr:(t printString) |
1091 Transcript showCr:(t printString) |
1063 |
1092 |
1064 "1 instAccess2" |
1093 "1 instAccess2" |
1065 ! |
1094 ! |
1073 a very small fraction compared to allocation & gc-time." |
1102 a very small fraction compared to allocation & gc-time." |
1074 |
1103 |
1075 |t| |
1104 |t| |
1076 |
1105 |
1077 t := Time millisecondsToRun:[ |
1106 t := Time millisecondsToRun:[ |
1078 self timesRepeat:[ |
1107 self timesRepeat:[ |
1079 Array new:500 |
1108 Array new:500 |
1080 ]. |
1109 ]. |
1081 ]. |
1110 ]. |
1082 Transcript showCr:(t printString) |
1111 Transcript showCr:(t printString) |
1083 |
1112 |
1084 "100000 * 500 * (4 bytes/object) -> 200000000" |
1113 "100000 * 500 * (4 bytes/object) -> 200000000" |
1085 "100000 memory" |
1114 "100000 memory" |
1094 allocation & gc." |
1123 allocation & gc." |
1095 |
1124 |
1096 |t| |
1125 |t| |
1097 |
1126 |
1098 t := Time millisecondsToRun:[ |
1127 t := Time millisecondsToRun:[ |
1099 100000 timesRepeat:[ |
1128 100000 timesRepeat:[ |
1100 Array new:500 |
1129 Array new:500 |
1101 ]. |
1130 ]. |
1102 ]. |
1131 ]. |
1103 Transcript showCr:(t printString) |
1132 Transcript showCr:(t printString) |
1104 |
1133 |
1105 "1 memory2" |
1134 "1 memory2" |
1106 ! |
1135 ! |
1116 |
1145 |
1117 n := 3.0. |
1146 n := 3.0. |
1118 m := 5.5. |
1147 m := 5.5. |
1119 |
1148 |
1120 t := Time millisecondsToRun:[ |
1149 t := Time millisecondsToRun:[ |
1121 self timesRepeat:[ |
1150 self timesRepeat:[ |
1122 p := 5 / n + m |
1151 p := 5 / n + m |
1123 ] |
1152 ] |
1124 ]. |
1153 ]. |
1125 Transcript showCr:(t printString) |
1154 Transcript showCr:(t printString) |
1126 |
1155 |
1127 "creates 2 new floats (about 20bytes each) per iteration" |
1156 "creates 2 new floats (about 20bytes each) per iteration" |
1128 "1000000 benchArithmetic" |
1157 "1000000 benchArithmetic" |
1139 newSpace fill-grade & cache patterns ..." |
1168 newSpace fill-grade & cache patterns ..." |
1140 |
1169 |
1141 |t| |
1170 |t| |
1142 |
1171 |
1143 t := Time millisecondsToRun:[ |
1172 t := Time millisecondsToRun:[ |
1144 |x| |
1173 |x| |
1145 x := 0.0. |
1174 x := 0.0. |
1146 1000000 timesRepeat:[x := x + 1.0] |
1175 1000000 timesRepeat:[x := x + 1.0] |
1147 ]. |
1176 ]. |
1148 Transcript showCr:(t printString) |
1177 Transcript showCr:(t printString) |
1149 |
1178 |
1150 "creates 1 new float (about 20bytes) per iteration" |
1179 "creates 1 new float (about 20bytes) per iteration" |
1151 "1 benchArithmetic2" |
1180 "1 benchArithmetic2" |
1160 newSpace fill-grade & cache patterns ..." |
1189 newSpace fill-grade & cache patterns ..." |
1161 |
1190 |
1162 |t| |
1191 |t| |
1163 |
1192 |
1164 t := Time millisecondsToRun:[ |
1193 t := Time millisecondsToRun:[ |
1165 |x| |
1194 |x| |
1166 x := 0.0. |
1195 x := 0.0. |
1167 self timesRepeat:[x := x + 1.0] |
1196 self timesRepeat:[x := x + 1.0] |
1168 ]. |
1197 ]. |
1169 Transcript showCr:(t printString) |
1198 Transcript showCr:(t printString) |
1170 |
1199 |
1171 "creates 1 new float (about 20bytes) per iteration" |
1200 "creates 1 new float (about 20bytes) per iteration" |
1172 "1000000 benchArithmetic3" |
1201 "1000000 benchArithmetic3" |
1179 stc will show much better performance here." |
1208 stc will show much better performance here." |
1180 |
1209 |
1181 |t| |
1210 |t| |
1182 |
1211 |
1183 t := Time millisecondsToRun:[ |
1212 t := Time millisecondsToRun:[ |
1184 |x "{ Class: Float }" | |
1213 |x "{ Class: Float }" | |
1185 x := 0.0. |
1214 x := 0.0. |
1186 1000000 timesRepeat:[x := x + 1.0] |
1215 1000000 timesRepeat:[x := x + 1.0] |
1187 ]. |
1216 ]. |
1188 Transcript showCr:(t printString) |
1217 Transcript showCr:(t printString) |
1189 |
1218 |
1190 "creates 1 new float (about 20bytes) per iteration" |
1219 "creates 1 new float (about 20bytes) per iteration" |
1191 "1 benchArithmetic3" |
1220 "1 benchArithmetic3" |
1198 newSpace fill-grade & cache patterns ..." |
1227 newSpace fill-grade & cache patterns ..." |
1199 |
1228 |
1200 |t| |
1229 |t| |
1201 |
1230 |
1202 t := Time millisecondsToRun:[ |
1231 t := Time millisecondsToRun:[ |
1203 self timesRepeat:[ |
1232 self timesRepeat:[ |
1204 Object new |
1233 Object new |
1205 ] |
1234 ] |
1206 ]. |
1235 ]. |
1207 Transcript showCr:(t printString) |
1236 Transcript showCr:(t printString) |
1208 |
1237 |
1209 "1000000 benchNew" |
1238 "1000000 benchNew" |
1210 ! |
1239 ! |
1216 newSpace fill-grade & cache patterns ..." |
1245 newSpace fill-grade & cache patterns ..." |
1217 |
1246 |
1218 |t| |
1247 |t| |
1219 |
1248 |
1220 t := Time millisecondsToRun:[ |
1249 t := Time millisecondsToRun:[ |
1221 self timesRepeat:[ |
1250 self timesRepeat:[ |
1222 Object basicNew |
1251 Object basicNew |
1223 ] |
1252 ] |
1224 ]. |
1253 ]. |
1225 Transcript showCr:(t printString) |
1254 Transcript showCr:(t printString) |
1226 |
1255 |
1227 "1000000 benchBasicNew" |
1256 "1000000 benchBasicNew" |
1228 ! |
1257 ! |
1235 |
1264 |
1236 |
1265 |
1237 |t| |
1266 |t| |
1238 |
1267 |
1239 t := Time millisecondsToRun:[ |
1268 t := Time millisecondsToRun:[ |
1240 self timesRepeat:[ |
1269 self timesRepeat:[ |
1241 Array basicNew |
1270 Array basicNew |
1242 ] |
1271 ] |
1243 ]. |
1272 ]. |
1244 Transcript showCr:(t printString) |
1273 Transcript showCr:(t printString) |
1245 |
1274 |
1246 "1000000 benchArrayBasicNew" |
1275 "1000000 benchArrayBasicNew" |
1247 ! |
1276 ! |
1253 newSpace fill-grade & cache patterns ..." |
1282 newSpace fill-grade & cache patterns ..." |
1254 |
1283 |
1255 |t| |
1284 |t| |
1256 |
1285 |
1257 t := Time millisecondsToRun:[ |
1286 t := Time millisecondsToRun:[ |
1258 self timesRepeat:[ |
1287 self timesRepeat:[ |
1259 Array new |
1288 Array new |
1260 ] |
1289 ] |
1261 ]. |
1290 ]. |
1262 Transcript showCr:(t printString) |
1291 Transcript showCr:(t printString) |
1263 |
1292 |
1264 "1000000 benchArrayNew" |
1293 "1000000 benchArrayNew" |
1265 ! |
1294 ! |
1271 |
1300 |
1272 |s t| |
1301 |s t| |
1273 |
1302 |
1274 s := Set new. |
1303 s := Set new. |
1275 t := Time millisecondsToRun:[ |
1304 t := Time millisecondsToRun:[ |
1276 1 to:4500 do:[:i | s add:i] |
1305 1 to:4500 do:[:i | s add:i] |
1277 ]. |
1306 ]. |
1278 |
1307 |
1279 Transcript showCr:(t printString) |
1308 Transcript showCr:(t printString) |
1280 |
1309 |
1281 "1 benchSetCreation" |
1310 "1 benchSetCreation" |
1287 the set is via a block local variable (s)." |
1316 the set is via a block local variable (s)." |
1288 |
1317 |
1289 |t| |
1318 |t| |
1290 |
1319 |
1291 t := Time millisecondsToRun:[ |
1320 t := Time millisecondsToRun:[ |
1292 |s| |
1321 |s| |
1293 s := Set new. |
1322 s := Set new. |
1294 1 to:4500 do:[:i | s add:i] |
1323 1 to:4500 do:[:i | s add:i] |
1295 ]. |
1324 ]. |
1296 |
1325 |
1297 Transcript showCr:(t printString) |
1326 Transcript showCr:(t printString) |
1298 |
1327 |
1299 "1 benchSetCreation2" |
1328 "1 benchSetCreation2" |
1305 thus no growing is needed in the loop." |
1334 thus no growing is needed in the loop." |
1306 |
1335 |
1307 |t| |
1336 |t| |
1308 |
1337 |
1309 t := Time millisecondsToRun:[ |
1338 t := Time millisecondsToRun:[ |
1310 |s| |
1339 |s| |
1311 s := Set new:4500. |
1340 s := Set new:4500. |
1312 1 to:4500 do:[:i | s add:i] |
1341 1 to:4500 do:[:i | s add:i] |
1313 ]. |
1342 ]. |
1314 |
1343 |
1315 Transcript showCr:(t printString) |
1344 Transcript showCr:(t printString) |
1316 |
1345 |
1317 "1 benchSetCreation3" |
1346 "1 benchSetCreation3" |
1331 |
1360 |
1332 sumTo |
1361 sumTo |
1333 |val| |
1362 |val| |
1334 |
1363 |
1335 100 timesRepeat:[ |
1364 100 timesRepeat:[ |
1336 val := 0. |
1365 val := 0. |
1337 1 to:10000 do:[:i | |
1366 1 to:10000 do:[:i | |
1338 val := val + i |
1367 val := val + i |
1339 ] |
1368 ] |
1340 ]. |
1369 ]. |
1341 "Transcript showCr:(Time millisecondsToRun:[1 sumTo])" |
1370 "Transcript showCr:(Time millisecondsToRun:[1 sumTo])" |
1342 ! |
1371 ! |
1343 |
1372 |
1344 fastSumTo |
1373 fastSumTo |
1345 |val i| |
1374 |val i| |
1346 |
1375 |
1347 100 timesRepeat:[ |
1376 100 timesRepeat:[ |
1348 val := 0. |
1377 val := 0. |
1349 i := 1. |
1378 i := 1. |
1350 [i <= 10000] whileTrue:[ |
1379 [i <= 10000] whileTrue:[ |
1351 val := val + i. |
1380 val := val + i. |
1352 i := i + 1 |
1381 i := i + 1 |
1353 ]. |
1382 ]. |
1354 ]. |
1383 ]. |
1355 "Transcript showCr:(Time millisecondsToRun:[1 fastSumTo])" |
1384 "Transcript showCr:(Time millisecondsToRun:[1 fastSumTo])" |
1356 ! |
1385 ! |
1357 |
1386 |
1358 nestedLoop |
1387 nestedLoop |
1359 |i| |
1388 |i| |
1360 |
1389 |
1361 100 timesRepeat:[ |
1390 100 timesRepeat:[ |
1362 i := 0. |
1391 i := 0. |
1363 1 to:100 do:[:l1 | |
1392 1 to:100 do:[:l1 | |
1364 1 to:100 do:[:l2 | |
1393 1 to:100 do:[:l2 | |
1365 i := i + 1 |
1394 i := i + 1 |
1366 ] |
1395 ] |
1367 ] |
1396 ] |
1368 ] |
1397 ] |
1369 "Transcript showCr:(Time millisecondsToRun:[1 nestedLoop])" |
1398 "Transcript showCr:(Time millisecondsToRun:[1 nestedLoop])" |
1370 ! |
1399 ! |
1371 |
1400 |
1372 atAllPut |
1401 atAllPut |
1373 |vec t| |
1402 |vec t| |
1374 |
1403 |
1375 vec := Array new:100000. |
1404 vec := Array new:100000. |
1376 t := Time millisecondsToRun:[ |
1405 t := Time millisecondsToRun:[ |
1377 1 to:100000 do:[:i | |
1406 1 to:100000 do:[:i | |
1378 vec at:i put:7 |
1407 vec at:i put:7 |
1379 ] |
1408 ] |
1380 ]. |
1409 ]. |
1381 ^ t |
1410 ^ t |
1382 |
1411 |
1383 "Transcript showCr:(1 atAllPut)" |
1412 "Transcript showCr:(1 atAllPut)" |
1384 ! |
1413 ! |
1386 sumAll |
1415 sumAll |
1387 |vec t s| |
1416 |vec t s| |
1388 |
1417 |
1389 vec := Array new:100000. |
1418 vec := Array new:100000. |
1390 1 to:100000 do:[:i | |
1419 1 to:100000 do:[:i | |
1391 vec at:i put:7 |
1420 vec at:i put:7 |
1392 ]. |
1421 ]. |
1393 s := 0. |
1422 s := 0. |
1394 t := Time millisecondsToRun:[ |
1423 t := Time millisecondsToRun:[ |
1395 1 to:100000 do:[:i | |
1424 1 to:100000 do:[:i | |
1396 s := s + (vec at:i) |
1425 s := s + (vec at:i) |
1397 ] |
1426 ] |
1398 ]. |
1427 ]. |
1399 ^ t |
1428 ^ t |
1400 |
1429 |
1401 "Transcript showCr:(1 sumAll)" |
1430 "Transcript showCr:(1 sumAll)" |
1402 ! ! |
1431 ! ! |