Re: Manipulating binary data
- From: Chung Leong <chernyshevsky@xxxxxxxxxxx>
- Date: 30 Apr 2007 04:50:20 -0700
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.
.
- References:
- Manipulating binary data
- From: brainflakes . org
- Re: Manipulating binary data
- From: Chung Leong
- Re: Manipulating binary data
- From: brainflakes . org
- Manipulating binary data
- Prev by Date: Re: Manipulating binary data
- Next by Date: Re: Problem When Serializing Array With Multiline Text
- Previous by thread: Re: Manipulating binary data
- Next by thread: Re: Manipulating binary data
- Index(es):
Relevant Pages
|