Stream and String performances

Bert Freudenberg bert at freudenbergs.de
Mon Apr 2 15:36:53 UTC 2007


On Apr 2, 2007, at 17:25 , Damien Cassou wrote:

> Hi,
>
> can somebody explain me the following result:
>
> [
> 	stream := WriteStream on: String new.
> 	stream nextPutAll: 'something to be put'.
> 	stream nextPutAll: 'something else to be put too'.
> 	] bench.                                ===> '206796.4407118576  
> per second.'
> .
> [
> 	stream := WriteStream on: Array new.
> 	stream nextPutAll: 'something to be put'.
> 	stream nextPutAll: 'something else to be put too'.
> 	] bench.                                ===>  '26177.36452709458  
> per second.'
>
>
> Why does streaming over Strings is 10 times faster than streaming  
> over Arrays?

Because it's just a copying primitive as long as the collection type  
matches. In the second case you are actually extracting Characters  
one-by-one from the String. It'd be much faster to convert your  
strings to an Array only once, and then put those arrays onto the  
stream.

- Bert -





More information about the Squeak-dev mailing list