in reply to Re^3: Why this code is so slow if run in thread?
in thread Why this code is so slow if run in thread?

You are right about "substr" being, unexpectedly, too slow with utf strings and threads. This program takes 12 seconds on my machine (I wanted some Greek letters, but it looks they are replaced with ugly codes. I think the idea is clear):

use utf8; use threads; threads-> create( sub { $s = 'αβγδ' x 1000_000; substr( $s, 0, 1000 ) for 1 .. 1000; })-> join; print time - $^T;

But then simple solution will be to, first, get a substring, and only then decode it. I.e. to move "decode" into loop. Then everything works as expected.

Replies are listed 'Best First'.
Re^5: Why this code is so slow if run in thread?
by BrowserUk (Patriarch) on Dec 12, 2016 at 12:01 UTC
    But then simple solution will be to, first, get a substring, and only then decode it

    Okay, great. But I don't understand how decoding utf16le works on the data returned by cc8compt() which contains 4-byte integers? 3 * width * height of them?

    Ie. a 10x10 image that looks like this (. is white * is black):

    .......... .*.******. .*........ .*.*.****. .*.*...... .*.*.****. .*.*.*.... .*.*.*.... .*.*.*.... ..........

    Comes back from cc8compt()->get_dataref as a 1200 byte string (10*10*4*3), and contains three identical copies of the 'color map' that looks like this when unpacked as 'V*'

    0:0:0:0:0:0:0:0:0:0 0:1:0:2:2:2:2:2:2:0 0:1:0:0:0:0:0:0:0:0 0:1:0:3:0:4:4:4:4:0 0:1:0:3:0:0:0:0:0:0 0:1:0:3:0:5:5:5:5:0 0:1:0:3:0:5:0:0:0:0 0:1:0:3:0:5:0:0:0:0 0:1:0:3:0:5:0:0:0:0 0:0:0:0:0:0:0:0:0:0 0:0:0:0:0:0:0:0:0:0 0:1:0:2:2:2:2:2:2:0 0:1:0:0:0:0:0:0:0:0 0:1:0:3:0:4:4:4:4:0 0:1:0:3:0:0:0:0:0:0 0:1:0:3:0:5:5:5:5:0 0:1:0:3:0:5:0:0:0:0 0:1:0:3:0:5:0:0:0:0 0:1:0:3:0:5:0:0:0:0 0:0:0:0:0:0:0:0:0:0 0:0:0:0:0:0:0:0:0:0 0:1:0:2:2:2:2:2:2:0 0:1:0:0:0:0:0:0:0:0 0:1:0:3:0:4:4:4:4:0 0:1:0:3:0:0:0:0:0:0 0:1:0:3:0:5:5:5:5:0 0:1:0:3:0:5:0:0:0:0 0:1:0:3:0:5:0:0:0:0 0:1:0:3:0:5:0:0:0:0 0:0:0:0:0:0:0:0:0:0

    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.

      It looks like data format of original piddle determines data format of return values:

      pdl> p $x = short sequence 3,4 [ [ 0 1 2] [ 3 4 5] [ 6 7 8] [ 9 10 11] ] pdl> p $y = cc8compt $x > 5 [ [0 0 0] [0 0 0] [1 1 1] [1 1 1] ] pdl> p $y-> info PDL: Short D [3,4] pdl> p length ${ $y-> get_dataref } 24
        It looks like data format of original piddle determines data format of return values:

        Okay. So the image must be returned as unsigned longs? And all the red pixels, then the green, then the blue?

        If so, how does your decoding with utf16le work?


        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.