But then Perl's Core "Encode" is not thread safe? :(

Suppose I split my program in two. First one creates 2 files, with single-byte encoded data and double-byte encoded data (and says "Equal", of course -- this image has less than 255 "objects").

use strict; use warnings; use 5.020; use PDL; use PDL::IO::Image; use PDL::Image2D; use PDL::IO::FastRaw; use Encode qw/ decode /; my $img = PDL::IO::Image-> new_from_file( 'test.png' ); my $pdl = $img-> pixels_to_pdl-> short; my $s1 = cc8compt( $pdl == 0 )-> byte; my $s2 = cc8compt( $pdl == 0 ); say 'Equal' if decode( 'UTF16LE', ${ $s2-> get_dataref }) eq ${ $s1-> get_dataref }; writefraw( $s1, "fname-1" ); writefraw( $s2, "fname-2" );

The 2nd program doesn't use PDL modules, it reads data from disk (and uses some hard-coded numbers):

use strict; use warnings; use 5.020; use threads; use Encode qw/ decode /; say "No thread (byte)\n---------"; say 'Count: ', scalar @{ test( 1 )}; say "\nThread (byte)\n---------"; say 'Count: ', scalar @{ threads-> create( \&test, 1 )-> join }; say "\nNo thread (short)\n---------"; say 'Count: ', scalar @{ test( 2 )}; say "\nThread (short)\n---------"; say 'Count: ', scalar @{ threads-> create( \&test, 2 )-> join }; sub test { my $arg = shift; open my $fh, '<', "fname-$arg"; binmode $fh; my $str = do { local $/; <$fh> }; close $fh; $str = decode( 'UTF16LE', $str ) if $arg == 2; my ( $w, $h ) = ( 7616, 1200 ); my @b = map { [ [ $w, 0 ], [ $h, 0 ] ] } 0 .. 145; my $t = time; for my $y ( 0 .. $h - 1 ) { my $s = substr( $str, $y * $w, $w ); while( $s =~ m[[^\0]+]g ) { my $c = ord( $& ); $b[ $c ][ 0 ][ 0 ] = $-[0] if $-[0] < $b[ $c ][ 0 +][ 0 ]; $b[ $c ][ 0 ][ 1 ] = $+[0] - 1 if $+[0] - 1 > $b[ $c ][ 0 +][ 1 ]; $b[ $c ][ 1 ][ 0 ] = $y if $y < $b[ $c ][ 1 +][ 0 ]; $b[ $c ][ 1 ][ 1 ] = $y if $y > $b[ $c ][ 1 +][ 1 ]; } } say 'Time: ', time - $t; shift @b; return \@b; }

The output:

No thread (byte) --------- Time: 0 Count: 145 Thread (byte) --------- Time: 1 Count: 145 No thread (short) --------- Time: 1 Count: 145 Thread (short) --------- Time: 51 Count: 145

In reply to Re^2: Why this code is so slow if run in thread? by vr
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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.