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?
Thank you
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 -
<Damien>Why does streaming over Strings is 10 times faster than streaming over Arrays?</Damien>
It's because Strings store Characters as bytes (one or more, depending on the encoding,) but Arrays store Characters as full Objects.
--Alan
squeak-dev@lists.squeakfoundation.org