[Pharo-project] Preallocation behavior

Stéphane Ducasse stephane.ducasse at inria.fr
Thu Apr 28 10:49:56 CEST 2011


Henrik 

you lost me 

> In the first example, you are making a single string with all A's of size 9000 repeated 500 times.
> In the second example, you are making 9000 strings with all A's of size 10000  repeated 500 times.

Why?

>>> An optimization is to use a Stream. Here is my source code:
>>> ===
>>> MessageTally spyOn:
>>> [ 500 timesRepeat: [
>>> | str |
>>> *str := WriteStream on: (String new)*.
>>> 9000 timesRepeat: [ str nextPut: $A ]]].
>>> ===
>>> 
>>> The result appears after *812 ms*, which is a large improvement.
>>> Now, we could optimize again using the preallocation. Here is my source
>>> code:
>>> 
>>> ====
>>> MessageTally spyOn:
>>> [ 500 timesRepeat: [
>>> | str |
>>> *str := WriteStream on: (String new: 10000)*.
>>> 9000 timesRepeat: [ str nextPutAll: 'A' ]]].
>>> ====
>>> 
>>> And the result is strange: it makes 2 times slower to display the result.
>>> The result appears after 1656 ms.
> 
> In the first example, you are making a single string with all A's of size 9000 repeated 500 times.
> In the second example, you are making 9000 strings with all A's of size 10000  repeated 500 times.




More information about the Pharo-project mailing list