Perl 6 Native Arrays

Exploring native array behaviour and performance.

Perl6

The Perl 6 syntax for shaped arrays is really rather nice.

my int @arr[10;10];
say @arr;
[[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 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] [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 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]]

The default behaviour for printing shaped arrays – not so much.

Python

By comparison, Numpy in Python does a better default job of printing shaped arrays.

  import numpy
  print numpy.zeros((10,10), dtype=int)
[[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 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]
 [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 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]]

Numpy also handles big shaped arrays well by printing an elided summary of the array. Don't bother trying this in Perl 6.

  import numpy
  print numpy.zeros((1000,1000), dtype=int)
[[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 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]]

Some Performance Measurements

my int @arr[1000;1000];
say "Initialization took $(now - ENTER now) seconds";
{
   my $str = @arr.gist;
   say "Stringification took $(now - ENTER now) seconds";
   say "String is {$str.chars} chars long";
   say ?($str ~~ /\n/);
}
Initialization took 0.00951239 seconds
Stringification took 9.15976749 seconds
String is 2002001 chars long
False

It's inevitable that stringification of the whole array is going to be slow. It's also a fair assumption that the current implementation has room for improvement. Printing a 2 million character string with no newlines is no help to anyone though.

Native Array Access

This year's AoC day 3 problem provided a nice test-case for measuring native array performance.

perl6  raku