Reply to Re: Manipulating binary data

Your name:

Reply:


Posted by Chung Leong on 04/30/07 11:50

On Apr 30, 11:39 am, brainflakes....@googlemail.com 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.

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация