[Newbies] Efficiently writing to a file

Ian Oversby oversby at googlemail.com
Tue Apr 10 21:22:36 UTC 2007


Okay, I tried this and it still seems a bit slow:

| myFile ios |

ios := ReadWriteStream on: ''.

Transcript show: 'Populate Buffer: ',
	(Time millisecondsToRun: [
		1 to: 10000 do: [
			:x | ios nextPutAll: ((x asString) , String crlf)
			]]) asString , ' millseconds' ; cr.

Transcript show: 'Position: ', (ios position) asString ; cr.

myFile := StandardFileStream fileNamed: 'c:/test.txt'.

Transcript show: 'Output Buffer: ',
                    (Time millisecondsToRun: [
	myFile nextPutAll: (ios contents)]) asString,
     ' milliseconds' ; cr.

myFile close.

# --

Populate Buffer is showing ~350 milliseconds to run on my system
whereas similar code in Perl is showing around 70 milliseconds.  Is
this the correct way to use the stream?

Thanks,

Ian

On 08/04/07, Bert Freudenberg <bert at freudenbergs.de> wrote:
>
> On Apr 8, 2007, at 21:13 , Ian Oversby wrote:
>
> > Is this a reasonable way of writing to a file?  It seems to run a
> > little slower than I would expect.
> >
> > Thanks,
> >
> > Ian
> >
> > | myFile |
> > myFile := StandardFileStream fileNamed: 'c:/test.txt'
> >
> > Transcript show: (Time millisecondsToRun: [
> >    1 to: 10000 do: [
> >           :x | myFile nextPutAll: ((x asString) , String crlf)
> >       ]]) asString , ' millseconds' ; cr.
> >
> > myFile close.
>
> It is unbuffered. Write to an in-memory stream first and then put the
> whole thing into the file.
>
> - Bert -
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at lists.squeakfoundation.org
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>


More information about the Beginners mailing list