← Index
NYTProf Performance Profile   « line view »
For -e
  Run on Thu Jun 30 16:34:56 2016
Reported on Thu Jun 30 16:35:08 2016

Filename/home/s1/perl5/perlbrew/perls/perl-5.22.1/lib/site_perl/5.22.1/Data/Graph/Util.pm
StatementsExecuted 185 statements in 595µs
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
11178µs78µsData::Graph::Util::::_toposortData::Graph::Util::_toposort
11130µs30µsData::Graph::Util::::BEGIN@6Data::Graph::Util::BEGIN@6
11110µs14µsData::Graph::Util::::BEGIN@7Data::Graph::Util::BEGIN@7
11110µs18µsData::Graph::Util::::BEGIN@8Data::Graph::Util::BEGIN@8
11110µs20µsData::Graph::Util::::BEGIN@10Data::Graph::Util::BEGIN@10
11110µs26µsData::Graph::Util::::BEGIN@42Data::Graph::Util::BEGIN@42
1119µs87µsData::Graph::Util::::toposortData::Graph::Util::toposort
0000s0sData::Graph::Util::::is_acyclicData::Graph::Util::is_acyclic
0000s0sData::Graph::Util::::is_cyclicData::Graph::Util::is_cyclic
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1package Data::Graph::Util;
2
31500nsour $DATE = '2016-06-24'; # DATE
41100nsour $VERSION = '0.002'; # VERSION
5
6254µs130µs
# spent 30µs within Data::Graph::Util::BEGIN@6 which was called: # once (30µs+0s) by DateTime::Format::Alami::new at line 6
use 5.010001;
# spent 30µs making 1 call to Data::Graph::Util::BEGIN@6
7227µs218µs
# spent 14µs (10+4) within Data::Graph::Util::BEGIN@7 which was called: # once (10µs+4µs) by DateTime::Format::Alami::new at line 7
use strict;
# spent 14µs making 1 call to Data::Graph::Util::BEGIN@7 # spent 4µs making 1 call to strict::import
8230µs227µs
# spent 18µs (10+9) within Data::Graph::Util::BEGIN@8 which was called: # once (10µs+9µs) by DateTime::Format::Alami::new at line 8
use warnings;
# spent 18µs making 1 call to Data::Graph::Util::BEGIN@8 # spent 9µs making 1 call to warnings::import
9
102166µs229µs
# spent 20µs (10+10) within Data::Graph::Util::BEGIN@10 which was called: # once (10µs+10µs) by DateTime::Format::Alami::new at line 10
use Exporter qw(import);
# spent 20µs making 1 call to Data::Graph::Util::BEGIN@10 # spent 10µs making 1 call to Exporter::import
1111µsour @EXPORT_OK = qw(toposort is_cyclic is_acyclic);
12
13
# spent 78µs within Data::Graph::Util::_toposort which was called: # once (78µs+0s) by Data::Graph::Util::toposort at line 60
sub _toposort {
141300ns my $graph = shift;
15
16 # this is the Kahn algorithm, ref:
17 # https://en.wikipedia.org/wiki/Topological_sorting#Kahn.27s_algorithm
18
191200ns my %in_degree;
2012µs for my $k (keys %$graph) {
2183µs $in_degree{$k} //= 0;
223315µs for (@{ $graph->{$k} }) { $in_degree{$_}++ }
23 }
24
25 # collect nodes with no incoming edges (in_degree = 0)
261200ns my @S;
272411µs for (keys %in_degree) { unshift @S, $_ if $in_degree{$_} == 0 }
28
291100ns my @L;
301400ns while (@S) {
31234µs my $n = pop @S;
32236µs push @L, $n;
332318µs for my $m (@{ $graph->{$n} }) {
342514µs if (--$in_degree{$m} == 0) {
35 unshift @S, $m;
36 }
37 }
38 }
39
401600ns if (@L == keys(%$graph)) {
411300ns if (@_) {
422223µs242µs
# spent 26µs (10+16) within Data::Graph::Util::BEGIN@42 which was called: # once (10µs+16µs) by DateTime::Format::Alami::new at line 42
no warnings 'uninitialized';
# spent 26µs making 1 call to Data::Graph::Util::BEGIN@42 # spent 16µs making 1 call to warnings::unimport
43 # user specifies a list to be sorted according to @L. this is like
44 # Sort::ByExample but we implement it ourselves to avoid dependency.
45 my %pos;
46 for (0..$#L) { $pos{$L[$_]} = $_+1 }
47 return (0, [
48 sort { ($pos{$a} || @L+1) <=> ($pos{$b} || @L+1) } @{$_[0]}
49 ]);
50 } else {
5116µs return (0, \@L);
52 }
53 } else {
54 # there is a cycle
55 return (1, \@L);
56 }
57}
58
59
# spent 87µs (9+78) within Data::Graph::Util::toposort which was called: # once (9µs+78µs) by DateTime::Format::Alami::new at line 84 of lib/DateTime/Format/Alami.pm
sub toposort {
6011µs178µs my ($err, $res) = _toposort(@_);
# spent 78µs making 1 call to Data::Graph::Util::_toposort
611200ns die "Can't toposort(), graph is cyclic" if $err;
6216µs @$res;
63}
64
65sub is_cyclic {
66 my ($err, $res) = _toposort(@_);
67 $err;
68}
69
70sub is_acyclic {
71 my ($err, $res) = _toposort(@_);
72 !$err;
73}
74
7514µs1;
76# ABSTRACT: Utilities related to graph data structure
77
78__END__