← Index
NYTProf Performance Profile   « block view • line view • sub view »
For ./testnewboardincnply
  Run on Mon Jan 12 21:52:27 2015
Reported on Mon Jan 12 22:01:19 2015

Filename/homes/dcw/lib/perl5/DCW/List.pm
StatementsExecuted 10 statements in 749µs
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
11117µs20µsList::::BEGIN@10List::BEGIN@10
11112µs38µsList::::BEGIN@13List::BEGIN@13
11110µs16µsList::::BEGIN@11List::BEGIN@11
1119µs15µsList::::BEGIN@15List::BEGIN@15
0000s0sList::::as_strList::as_str
0000s0sList::::consList::cons
0000s0sList::::deepcloneList::deepclone
0000s0sList::::foreachList::foreach
0000s0sList::::headtailList::headtail
0000s0sList::::isemptyList::isempty
0000s0sList::::itemsList::items
0000s0sList::::listList::list
0000s0sList::::newList::new
0000s0sList::::nillistList::nillist
0000s0sList::::pushList::push
0000s0sList::::randomitemList::randomitem
0000s0sList::::reverseList::reverse
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1package List;
2
3#
4# simple OO wrapper around perl arrays, giving
5# self-printing lists of anything.
6#
7# usage: use List; (mainly OO)
8# or: use List qw(list nillist); (convenience functions too)
9
10230µs224µs
# spent 20µs (17+3) within List::BEGIN@10 which was called: # once (17µs+3µs) by NewBoard::BEGIN@25 at line 10
use strict;
# spent 20µs making 1 call to List::BEGIN@10 # spent 3µs making 1 call to strict::import
11234µs222µs
# spent 16µs (10+6) within List::BEGIN@11 which was called: # once (10µs+6µs) by NewBoard::BEGIN@25 at line 11
use warnings;
# spent 16µs making 1 call to List::BEGIN@11 # spent 6µs making 1 call to warnings::import
12
13253µs264µs
# spent 38µs (12+26) within List::BEGIN@13 which was called: # once (12µs+26µs) by NewBoard::BEGIN@25 at line 13
use overload '""' => \&as_str;
# spent 38µs making 1 call to List::BEGIN@13 # spent 26µs making 1 call to overload::import
14
152626µs221µs
# spent 15µs (9+6) within List::BEGIN@15 which was called: # once (9µs+6µs) by NewBoard::BEGIN@25 at line 15
use Exporter 'import';
# spent 15µs making 1 call to List::BEGIN@15 # spent 6µs making 1 call to Exporter::import
1612µsour @EXPORT_OK = qw(list nillist);
17
18
19# exported convenience functions
20sub list (@) { return List->new(@_); }
21sub nillist { return List->new( () ); }
22
23
24#
25# my $l = List->new( @item );
26# Create a new List of items @item
27#
28sub new ($@)
29{
30 my( $class, @item ) = @_;
31 return bless [ @item ], $class;
32}
33
34
35#
36# my $l->push( @item );
37# Push @items on the end of $l
38# (destructively modifying $l)
39#
40sub push ($@)
41{
42 my( $l, @item ) = @_;
43 push @$l, @item;
44}
45
46
47#
48# my $l->cons( $item );
49# cons $item on the front of $l
50# (destructively modifying $l)
51# OR
52# my $newlist = List->cons( $head, $tail )
53# functional cons: create a new List
54# containing the $head followed by all
55# elements in the $tail
56#
57sub cons ($$;$)
58{
59 my( $thing, $head, $tail ) = @_;
60 unless( defined $tail )
61 {
62 die "List->cons: $thing not a list\n" unless
63 ref($thing) eq "List";
64 unshift @$thing, $head;
65 } else
66 {
67 return List->new( $head, @$tail );
68 }
69}
70
71
72#
73# my @item = $l->items;
74# Extract all the items from list $l
75#
76sub items ($)
77{
78 my( $self ) = @_;
79 return @$self;
80}
81
82
83#
84# my $isempty = $l->isempty;
85# Is list $l empty?
86#
87sub isempty ($)
88{
89 my( $self ) = @_;
90 return @$self == 0;
91}
92
93
94#
95# my( $head, $tail ) = $l->headtail;
96# Without altering $l, break apart
97# the head and tail (creating a new
98# List object for the tail)
99#
100sub headtail ($)
101{
102 my( $self ) = @_;
103 my @x = @$self;
104 die "list->headtail: empty list\n" if @x == 0;
105 my $head = shift @x;
106 return ( $head, List->new(@x) );
107}
108
109
110#
111# my $revlist = $l->reverse;
112# Create a new list containing the same elements
113# as in $l but in reverse order
114#
115sub reverse ($)
116{
117 my( $self ) = @_;
118 return List->new( reverse @$self );
119}
120
121
122#
123# my $item = $l->randomitem;
124# Extract a random item from list $l
125#
126sub randomitem ($)
127{
128 my( $self ) = @_;
129 return $self->[int(rand(@$self))];
130}
131
132
133#
134# my $l->foreach( $cb );
135# Foreach item in list $l, call $cb(item)
136#
137sub foreach ($$)
138{
139 my( $l, $cb ) = @_;
140 foreach (@$l)
141 {
142 $cb->($_);
143 }
144}
145
146
147#
148# my $s = $l->as_str;
149# Stringify a list nicely
150#
151sub as_str ($)
152{
153 my( $self ) = @_;
154 my @item = @$self;
155 my $str = join(",", map { "$_" } @item );
156 return "[$str]";
157}
158
159
160#
161# my $newlist = $list->deepclone;
162# deepclone the given list $list, calling each item's deepclone
163# method and building and returning a new identical list with
164# no shared items..
165# ONLY CALL THIS IF ALL ITEMS ON $list "can" deepclone!
166#
167sub deepclone ($)
168{
169 my( $self ) = @_;
170 return List->new( map { $_->deepclone } @$self );
171}
172
173
17414µs1;