How fast are string operations in Perl? In particular, concatenation and purpose

How fast is string concatenation in Perl? Is it linear in length of the second operand? If so, what conditions must be met in order for this operation to be linear? What are examples of nonlinear concatenation times?

What about assigning strings? When and where does the actual copy of the buffer occur?

What about other operations like substring or simple regular expressions?

+3
source share
2 answers

I experienced it myself. Concatenation is linear in the length of the second argument, but the assignment is always linear in relation to the length of the string.

It seems that Perl does not consider links for strings, but it associates a buffer with each variable (link).

:

, -, , :

 248ms   my $x; $x .= "a" for 1..2_000_000
 501ms   my $x; $x .= "a" for 1..4_000_000
 967ms   my $x; $x .= "a" for 1..8_000_000

$x = $x . $y, -, $x:

 295ms   my $x; $x = $x . "a" for 1..2_000_000
 592ms   my $x; $x = $x . "a" for 1..4_000_000
1170ms   my $x; $x = $x . "a" for 1..8_000_000

, -, , , :

 233ms   my $x; ${\$x} = ${\$x} . "a" for 1..40_000
 951ms   my $x; ${\$x} = ${\$x} . "a" for 1..80_000
3811ms   my $x; ${\$x} = ${\$x} . "a" for 1..160_000

:

 186ms   my $x; for (1..50_000) { $x .= "a"; my $y = $x }
 764ms   my $x; for (1..100_000) { $x .= "a"; my $y = $x }
3029ms   my $x; for (1..200_000) { $x .= "a"; my $y = $x }

, :

 545ms   my $x; for (1..50_000) { $x .= "a"; my $y = $x; my $y2 = $x; my $y3 = $x }
2264ms   my $x; for (1..100_000) { $x .= "a"; my $y = $x; my $y2 = $x; my $y3 = $x }
8951ms   my $x; for (1..200_000) { $x .= "a"; my $y = $x; my $y2 = $x; my $y3 = $x }
+2

, (, , HW, Perl ..)

, perl, . perlguts .

, :

use Benchmark qw(:all);

my $a = "Some string";
my @b = map { "Some string to append " x $_ } (1..10);

cmpthese(-1, {
    ( map {+ "concat_$_" => sub { my $c = $a . $b[$_] } } (1..10) )
});

my $c = $a . $b . , .

+5

All Articles