Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Sorry for my earlier misdirection. By way of recompense I have what I believe (though it is essentially untested for lack of a suitable image), that addresses both the slowness of substr on utf strings within threads (which is just weird) and the problem I thought was the cause, that of cloning the returned array.

It avoids the former by doing away with the encoding, searching instead for runs of pairs of non-null characters in the unencoded pdl; and the latter by accumulating the counts in a packed binary array stored in a scalar.

sub test { my $fn = shift; my $img = PDL::IO::Image-> new_from_file( $fn ) or die "Failed to +load image"; my $pdl = $img->pixels_to_pdl->short; my $s = cc8compt( $pdl != 0 ); my $str = ${ $s-> get_dataref }; my ( $w, $h ) = $s-> dims; my $bounds = pack 'n4', $w, 0, $h, 0; $bounds x= $s->max; for my $y ( 0 .. $h - 1 ) { my $s = substr( $str, 2 * $y * $w, 2 * $w ); while( $s =~ m[(?:[^\0][^\0])+]g ) { my( $l, $r ) = ( $-[0]/2, (($+[0])-1)/2 ); my $c = ord( $& ); vec( $bounds, 4*$c+0, 16 ) = $l if $l < vec( $bounds, 4*$c ++0, 16 ); vec( $bounds, 4*$c+1, 16 ) = $r if $r > vec( $bounds, 4*$c ++1, 16 ); vec( $bounds, 4*$c+2, 16 ) = $y if $y < vec( $bounds, 4*$c ++2, 16 ); vec( $bounds, 4*$c+3, 16 ) = $y if $y > vec( $bounds, 4*$c ++3, 16 ); } } return $bounds; }

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: Why this code is so slow if run in thread? by BrowserUk
in thread Why this code is so slow if run in thread? by vr

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (8)
As of 2024-04-19 13:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found