File: constructors.pl

package info (click to toggle)
liblist-objects-withutils-perl 2.028003-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,292 kB
  • sloc: perl: 1,957; makefile: 17; sh: 6
file content (51 lines) | stat: -rw-r--r-- 1,123 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use strict; use warnings;
use feature 'say';

use Types::Standard 'Num';

use List::Objects::WithUtils;
use Benchmark 'cmpthese', 'timethese';

say "  >>> array types <<<";

my @values = ( 1 .. 100 );

my $basic = sub { my $arr = array @values };

my $immutable = sub { my $arr = immarray @values };

my $typed = sub { my $arr = array_of Num() => @values };

my $typed_immutable = sub { my $arr = immarray_of Num() => @values };

my $results = timethese( 100_000 =>
  +{
      array       => $basic,
      immarray    => $immutable,
      array_of    => $typed,
      immarray_of => $typed_immutable,
  }
);
cmpthese($results);


say "  >>> hash types <<<";
my $ltr = 'a';
my %hsh = map {; $ltr++ => $_ } 1 .. 100;

$basic = sub { my $hash = hash %hsh };
$immutable = sub { my $hash = immhash %hsh };
$typed = sub { my $hash = hash_of Num() => %hsh };
$typed_immutable = sub { my $hash = immhash_of Num() => %hsh };

my $hash_results = timethese( 100_000 =>
  +{
      hash       => $basic,
      immhash    => $immutable,
      hash_of    => $typed,
      immhash_of => $typed_immutable,
  }
);
cmpthese($hash_results);