Stream and String performances

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Stream and String performances

Damien Cassou-3
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

--
Damien Cassou

Reply | Threaded
Open this post in threaded view
|

Re: Stream and String performances

Bert Freudenberg

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 -



Reply | Threaded
Open this post in threaded view
|

RE: Stream and String performances

Alan L. Lovejoy
In reply to this post by Damien Cassou-3
<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