Re: Constructing sequences
- Posted by Mario Steele <eumario at trilake.net> Jun 28, 2005
- 625 views
Allright, Considering someone posted something about file i/o (Including Kat), about this, and the overwhelming evidence that pre-allocation is best way to go, here should be an extreamly fast version of loading a file into Memory (AKA Sequence):
function fast_read(sequence fname) sequence file_block integer fh, size fh = open(fname,"rb") if seek(fh,-1) then end if size = where(fh) if seek(fh,0) then end if file_block = repeat(0,size) for i = 1 to size do file_block[i] = getc(fh) end for close(fh) return file_block end function
This code is untested, as I formulated it out in my head, but it should prove the point that it will be faster. I use seek() instead of dir(), cause sometimes, dir() Rely's on the Operating System to get the size, and that can prove to be slow. seek() is faster, cause you open the file allready, or have it open, seek() is easier to reset the file pointers, rewind, and such, and I have yet to see an error occur from seek() where it goes to the end of the file, then back to the first byte in the file. And that's from countless usages that I have used this method for. Mario Steele http://enchantedblade.trilake.net Attaining World Dominiation, one byte at a time...