[Newbies] Binary file I/O performance problems
David T. Lewis
lewis at mail.msen.com
Thu Sep 4 00:27:09 UTC 2008
On Tue, Sep 02, 2008 at 11:00:54PM -0700, David Finlayson wrote:
> I find it troubling that I am having to write code below the
> abstraction level of C to read and write data from a file. I thought
> Smalltalk was supposed to free me from this kind of drudgery?
You're quite right about that. The good news is that you have already
figured out how to profile, and you already know where the performance
problem is. Setting aside for the moment the issue of Squeak's awfile
file I/O performance, the quickest solution to your problem may also
be the easiest. As long as the data sets are not too large, just load
the whole file into Squeak first (use FileStream>>contentsOfEntireFile)
and *then* operate on the data.
For example, if you have data in MYDATA.BIN, and you want to load it
into Squeak and read the first 100 bytes, you can do something like this:
| myFile dataStream |
myFile := FileStream fileNamed: 'MYDATA.BIN'.
[dataStream := ReadStream on: myFile contentsOfEntireFile]
ensure: [myFile ifNotNilDo: [:f | f close]].
dataStream next: 100.
Once you have the data in memory, things are quite fast. I know this
sounds like an odd way to handle data loading, but it actually works
very well, and buying some memory is a whole lot easier than fixing
Squeak's I/O performance ;)
More information about the Beginners