Re: Manipulating binary data



On Apr 30, 11:39 am, brainflakes....@xxxxxxxxxxxxxx wrote:
I doubt that's the optimal solution. Function calls are relatively
expensive in PHP. The difference in speed you observed between the
different methods can probably be attributed to the number of calls
made per element.

Explain exactly what you're trying to do. There probably is a better
approach.

I'm creating some custom image processing filters on 8bit greyscale
images

for($x=0; $x<$width; $x++){
for($y=0; $x<$height; $y++){
check value of pixels (0-255) in a 10x10 area around given pixel
calculate new value
plot value at x,y (0-255)
}

}

I figured the best way to do it would be to just "allocate" a memory
space of $width * $height bytes and copy the image into that, which I
guess is done by creating a string of that length. However even just
plotting to $string[$n++]=chr($val) (no co-ord calculations) is slower
then imagesetpixel() on an 8-bit image.

For some reason tho I can't actually render directly from the 8-bit
image (I use imagesetcolor() to set colours in a greyscale gradient
but imagepng complains there's the wrong number of colours) so I have
to copy the image in and out of a truecolour image anyway.

I have done some further optimisations on the actual processing code
and improved the speed about 10-fold, but if there's a faster way to
read/write 8bit data I'm still interested as every little helps when
you're doing batch processing. (tho I guess I should probably be doing
it in C or .net anyway ;)

Andrew

Hmmm, that sort of processing will likely be slow in any event. Here
are some tips:

A hash look up is faster than calling chr() or ord(). So you can speed
things up by employing two tables:

$chr = array( 0 => "\x00", 1 => "\x01", 2 => "\x02" ..., 0xFF =>
"\xFF" );
$ord = array_flip($chr);


Unpacking the binary data into an array of ints with unpack() and then
repacking the result with pack() could be faster than repeated binary-
to-numeric conversions.


for() loops are slower than foreach(). Using the range() function, you
can create two arrays holding the indexes then go through them with
foreach() instead of incrementing them.


Loops have overhead. You can squeeze out some of that by unrolling
tight loops. What you would do is dynamically write out the PHP code
that'd execute by a full iteration of the loop, then create a lambda
function using create_function(). I used that technique in my
imagecreatefrombmp() routine(http://www.conradish.net/bobo/
show_source.php?filename=bmp.php).


When possible, use array_map() or array_walk() instead of looping.

.



Relevant Pages

  • Re: foreach loops are sooooo tricky.....
    ... loops are not at all like for loops, ... Translating Fortran to PHP, because hosters won't allow anything else ... I wish PHP would do array and matrix stuff like Fortran or C, ... I find foreach loops to be quite intuitive. ...
    (comp.lang.php)
  • Re: foreach loops are sooooo tricky.....
    ... loops are not at all like for loops, ... Translating Fortran to PHP, because hosters won't allow anything else ... I wish PHP would do array and matrix stuff like Fortran or C, ... the foreach loop is tricky. ...
    (comp.lang.php)
  • foreach loops are sooooo tricky.....
    ... loops are not at all like for loops, ... Translating Fortran to PHP, because hosters won't allow anything else ... the foreach loop is tricky. ...
    (comp.lang.php)
  • Re: foreach loops are sooooo tricky.....
    ... loops are not at all like for loops, ... Something for PHP 6? ... the foreach loop is tricky. ... the references. ...
    (comp.lang.php)
  • Re: PHP running exec() Windows program very slow in comparison to UNIX equivalent program
    ... So, by converting a VB6 DLL into a COM and calling the COM from PHP I am able to work around the speed limitations of swetest.exe, PHP, and Windows. ... I wrote a little program in VB5 that essentially does what this other compiled C Windows program does in just one of the loops that gets executed about 140 times. ... the other thing is how long it takes to to execute the program with those statements from a command prompt. ...
    (alt.php)