Index of first and last non-"\xff" in a long string

Discussion in 'Perl Misc' started by w.c.humann@arcor.de, Nov 12, 2007.

Guest

I'm going through several PGM images, overlaying (i.e. ANDing) them
and would also like to determine the bounding box. For that I need to
find the first and last non-white (i.e. non-"\xff") pixel in every
line. Now I have one line in a string $data (one pixel per character),
possibly several 1000 charcters long. I tried 3 alternatives so far.
All 3 work, but there may be even faster ways to do this:

Advertisements

Quoth :
> I'm going through several PGM images, overlaying (i.e. ANDing) them
> and would also like to determine the bounding box. For that I need to
> find the first and last non-white (i.e. non-"\xff") pixel in every
> line. Now I have one line in a string $data (one pixel per character),
> possibly several 1000 charcters long. I tried 3 alternatives so far.
> All 3 work, but there may be even faster ways to do this:

so indeed 'innerm' wins, but...
> though seriously increasing the number of trailing "\xff"s causes both
> 'match' and 'innerm' to perform dramatically badly, so maybe this is an
> artefact of my test string.

typical files have blank lines at top and bottom and if I modify your
script like this:

An optimization not considered so far: Once I've found a left and
right bound in a line, I only need to check from the edges up these
bounds in all following lines, because my bounding-box can only grow
(and never shrink) while checking further lines. As a faked test I've
used this:

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!