develooper Front page | perl.perl5.porters | Postings from October 2016

[perl #129802]

Thread Next
From:
Leszek Dubiel
Date:
October 30, 2016 01:47
Subject:
[perl #129802]
Message ID:
5451dd33-c876-a6af-9c36-2f848f25b46f@dubielvitrum.pl

I have tested program below for $ARGV[0] in range of 0 to 2000 (two 
twousand).


#!/usr/bin/perl

use strict;
use warnings;
use Benchmark 'cmpthese';

my $s = "A"x (2**16 + 5 - $ARGV[0]);
my $cat = 'B' . $s . 'B';
my $spr = sprintf "B%sB", $s;

cmpthese(-1, {
          "assign" => sub { my $i=0; $s =~ /./ while $i++ < 100 },
          "concat" => sub { my $i=0; $cat =~ /./ while $i++ < 100 },
         "string" => sub { my $i=0; $spr =~ /./ while $i++ < 100 }
});
########### end


During this long run:

        -- "concat" was extremely slow in 500 (five hundred) cases, 
every fourth run -- $ARGV[0] = 0, 2, 4, 8, 12, 16, ...

       -- "assing" was extremely slow for $ARGV[0] =  6,  262,  518, 
774,  1030,  1286,  1542,  1798, ...

          -- "string" was extremely slow for $ARGV[0] = 8,  264, 520,  
776,  1032,  1288,  1544,  1800, ...



My function works on reference to big string, and here is ugly 
workaround -- check if program is running extremely slow, and add one 
space to string in that case (15 seconds):

sub ... {

while () {

     my $a = ... # big text

         # matching big text ...
     use v5.10;
     state $t = time;
     if (time > $t + 15) {
         my $x = pos $$a;
         $a .= " ";
         pos $a = $x;
         $t = time;
     }
  }
}



Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About