Writing a large Collection of integers to a file fast

David T. Lewis lewis at mail.msen.com
Mon Jan 28 11:45:35 UTC 2008


On Mon, Jan 28, 2008 at 05:48:01AM +0000, Zulq Alam wrote:
> Lukas Renggli wrote:
> >
> >Or even smarter:
> >
> >    aStream print: int; nextPut: Character cr
> >
> >Lukas
> >
> 
> I tried this and didn't get the improvement I was expecting, < 2%. I had 
> a deeper look and noticed Integer>>#printOn:base: generates an 
> unnecessary string representation which it then reverses and prints to 
> the stream.

The #reversed is not unnecessary. Other algorithms are of course
possible, but this one is not wrong.

> I got closer to 20% with these changes to Integer as well:
> 
> printOn: aStream base: base
>   self negative ifTrue: [aStream nextPut: $-].
>   self abs normalize printDigitsOn: aStream base: base.
> 
> printDigitsOn: aStream base: base
>   (self >= base)
>     ifTrue:
>       [(self // base) printDigitsOn: aStream base: base].
> 
>   aStream nextPut: (Character digitValue: self \\ base)
> 
> I'm not sure why one needs to send #normalize but it was being send 
> before...?

The #normalize is there for large integers. If you remove it, you
will break printing for large integers, and you may not see the
problem right away since it would only apply to non-normalized
large integers. This is the worst sort of bug: Shows up later, and
nobody can remember how or why it got introduced.

> I'll create an entry in Mantis when I get a moment to check the unit 
> tests and the other printXXX methods on Integer.

It would be best not to open a new Mantis entry unless there actually
is a new issue to be resolved.

Dave




More information about the Squeak-dev mailing list