in reply to Strange performance loss after interpolating an array and then copying to another array;

I've limited knowledge of the internals, but as I understand it, interpolating into a string will force the array elements to calculate a string value for all the numbers.

I would then expect the array copying to be duplicating the *strings* rather than only the integers, and that's a lot more bytes to copy.

  • Comment on Re: Strange performance loss after interpolating an array and then copying to another array;

Replies are listed 'Best First'.
Re^2: Strange performance loss after interpolating an array and then copying to another array;
by moritz (Cardinal) on Aug 19, 2011 at 18:09 UTC

    Let's test that hypothesis:

    use Time::HiRes qw(gettimeofday tv_interval); my @array = (1..100); # say "@array"; # 0.332776 s # say @array; # 0.052496 s # #<nothing> # 0.052361 s my $t0 = [gettimeofday]; for (1..10000) { my @array2 = @array; } say tv_interval($t0); __END__ SV = IV(0xe47dd8) at 0xe47de8 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 1 SV = PVIV(0xe512d8) at 0xe47de8 REFCNT = 1 FLAGS = (IOK,POK,pIOK,pPOK) IV = 1 PV = 0xe56050 "1"\0 CUR = 1 LEN = 16

    So yes, interpolating the array into the string adds a PV (string) representation to the scalar.

      So what causes the performance gain, though small, going from this:

      use Time::HiRes qw(gettimeofday tv_interval); my @array = (1..100); for my $element (@array) { $element .= ''; } for my $element (@array) { $element += 0; } my $t0 = [gettimeofday]; for (1..10000) { my @array2 = @array; } say tv_interval($t0);

      To this:

      use Time::HiRes qw(gettimeofday tv_interval); my @array = (1..100); my $t0 = [gettimeofday]; for (1..10000) { my @array2 = @array; } say tv_interval($t0);
Re^2: Strange performance loss after interpolating an array and then copying to another array;
by Kc12349 (Monk) on Aug 19, 2011 at 18:08 UTC

    That makes sense, though I am curious if perl bothers to copy both internal scalar representations. I ran my test again preceded by the below. I force each element into numeric context again after interpolating. I get almost all the performance back with this approach, but not quite all of it.

    for my $element (@array) { $element += 0; }