|
Posted by Chung Leong on 04/30/07 16:38
On Apr 30, 11:49 am, Toby A Inkster <usenet200...@tobyinkster.co.uk>
wrote:
> brainflakes.org wrote:
> > Are there actually any proper binary extensions or is using gd lib the
> > way to go (as I guess it's just dealing with binary data as a 2d array
> > anyway)
>
> Assuming that each "row" of data is of the same length, and each "column"
> has the same height (which I think is a fairly safe assumption if you're
> modelling the data as a rectangular image) then, why not just store the
> data as a string. That is, instead of storing a 3x3 array of data as this:
>
> $array = array(
> array(0x61, 0x62, 0x63),
> array(0x31, 0x32, 0x33),
> array(0x78, 0x79, 0x7A)
> );
>
> you store it as:
>
> $string = 'abc123xyz';
>
> Which is roughly how a 2-dimensional array is internally stored by a C
> program.
>
> Instead of this:
>
> $cell = $array[$x][$y];
>
> You use:
>
> define(ROW_LENGTH, 3);
> $cell = $string[(ROW_LENGTH*$x) + $y];
>
> And instead of this:
>
> $array[$x][$y] = 0x64;
>
> this:
>
> $string[(ROW_LENGTH*$x) + $y] = chr(0x64);
>
> --
> Toby A Inkster BSc (Hons) ARCShttp://tobyinkster.co.uk/
> Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux
>
> * = I'm getting there!
Accessing a character within a string is going to be slower than
accessing an element in an array. It's true that PHP can very quickly
find the value given the index, but it has to create a new string
object to hold that character ($string[$i] is a one character string).
Memory allocation takes more time than a hash look-up, hence negative
saving.
The key to performance here is to reduce the amount of work in the
inner loop, as it's going to be run tens of thousands of times.
Converting the binary data to numbers and breaking them into an array
per line ahead of time will be likely be faster.
Navigation:
[Reply to this message]
|