Matt Sergeant writes:
> On Fri, 19 May 2000, David Larkin wrote:
> 
> > I require a large array of ints in a real application, just stripped
> > problem down to bear bones for demo.
> 
> Is your array sparse by any chance? If not your modperl daemon is going to
> get _much_ larger after you populate that array. If it's sparse, consider
> using a hash - that will give you a slight speed penalty at the benefit of
> not consuming quite so much ram. Other things to consider:
> 
> Re-write this critical bit of code in XS (not as hard as it sounds).
> Use a database.
> Use the filesystem.

Simply store them in native form in a big long string, just like C
would do. Pre-extend the string first, if you know how big you want it:
    $intsize = 4;  # alter if sizeof(int) != 4
    $intarray = "\0" x (30000 * $intsize);
Update index $i with
    substr($intarray, $i * $intsize, $intsize) = pack("I", $newval);
which assumes an unsigned int, or use "i" for signed.
To retrieve index $i use
    $val = unpack("I", substr($intarray, $i * $intsize, $intsize));
You can even retrive a bunch of them faster with
    @ten_to_thirty = unpack("I20", substr(...));
or set a bunch with
    substr(..., $intsize * 10) = pack("I20", @ten_to_thirty);
For that last bit of extra performance, do
    sub INTSIZE () { 4 };  # alter if sizeof(int) != 4

--Malcolm

-- 
Malcolm Beattie <[EMAIL PROTECTED]>
Unix Systems Programmer
Oxford University Computing Services

Reply via email to