← Index
NYTProf Performance Profile   « block view • line view • sub view »
For bin/pan_genome_post_analysis
  Run on Fri Mar 27 11:43:32 2015
Reported on Fri Mar 27 11:45:28 2015

Filename/Users/ap13/perl5/lib/perl5/File/Grep.pm
StatementsExecuted 15 statements in 926µs
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
11123µs138µsFile::Grep::::BEGIN@10File::Grep::BEGIN@10
11121µs24µsFile::Grep::::BEGIN@8File::Grep::BEGIN@8
11117µs39µsFile::Grep::::BEGIN@5File::Grep::BEGIN@5
11110µs45µsFile::Grep::::BEGIN@6File::Grep::BEGIN@6
1116µs6µsFile::Grep::::BEGIN@9File::Grep::BEGIN@9
1114µs4µsFile::Grep::::CORE:matchFile::Grep::CORE:match (opcode)
0000s0sFile::Grep::::__ANON__[:100]File::Grep::__ANON__[:100]
0000s0sFile::Grep::::__ANON__[:113]File::Grep::__ANON__[:113]
0000s0sFile::Grep::::__ANON__[:129]File::Grep::__ANON__[:129]
0000s0sFile::Grep::::__ANON__[:143]File::Grep::__ANON__[:143]
0000s0sFile::Grep::::__ANON__[:157]File::Grep::__ANON__[:157]
0000s0sFile::Grep::::__ANON__[:169]File::Grep::__ANON__[:169]
0000s0sFile::Grep::::__ANON__[:88]File::Grep::__ANON__[:88]
0000s0sFile::Grep::::_fgrep_processFile::Grep::_fgrep_process
0000s0sFile::Grep::::fdoFile::Grep::fdo
0000s0sFile::Grep::::fgrepFile::Grep::fgrep
0000s0sFile::Grep::::fgrep_flatFile::Grep::fgrep_flat
0000s0sFile::Grep::::fgrep_intoFile::Grep::fgrep_into
0000s0sFile::Grep::::fmapFile::Grep::fmap
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1#!/usr/bin/perl -w
2
3package File::Grep;
4
5226µs262µs
# spent 39µs (17+23) within File::Grep::BEGIN@5 which was called: # once (17µs+23µs) by Bio::Roary::AnnotateGroups::BEGIN@25 at line 5
use strict;
# spent 39µs making 1 call to File::Grep::BEGIN@5 # spent 23µs making 1 call to strict::import
6240µs280µs
# spent 45µs (10+35) within File::Grep::BEGIN@6 which was called: # once (10µs+35µs) by Bio::Roary::AnnotateGroups::BEGIN@25 at line 6
use Carp;
# spent 45µs making 1 call to File::Grep::BEGIN@6 # spent 35µs making 1 call to Exporter::import
7
8
# spent 24µs (21+4) within File::Grep::BEGIN@8 which was called: # once (21µs+4µs) by Bio::Roary::AnnotateGroups::BEGIN@25 at line 16
BEGIN {
9237µs16µs
# spent 6µs within File::Grep::BEGIN@9 which was called: # once (6µs+0s) by Bio::Roary::AnnotateGroups::BEGIN@25 at line 9
use Exporter ();
# spent 6µs making 1 call to File::Grep::BEGIN@9
10290µs2253µs
# spent 138µs (23+115) within File::Grep::BEGIN@10 which was called: # once (23µs+115µs) by Bio::Roary::AnnotateGroups::BEGIN@25 at line 10
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
# spent 138µs making 1 call to File::Grep::BEGIN@10 # spent 115µs making 1 call to vars::import
11524µs14µs $VERSION = sprintf( "%d.%02d", q( $Revision: 0.02 $ ) =~ /\s(\d+)\.(\d+)/ );
# spent 4µs making 1 call to File::Grep::CORE:match
12 @ISA = qw(Exporter);
13 @EXPORT = qw();
14 @EXPORT_OK = qw( fgrep fmap fdo );
15 %EXPORT_TAGS = ( );
161706µs124µs}
# spent 24µs making 1 call to File::Grep::BEGIN@8
17
18# Remain silent on bad files, else shoutout.
191400nsour $SILENT = 1;
20
21# Internal function; does the actual walk through the files, and calls
22# out to the coderef to do the work for each line. This gives me a bit
23# more flexibility with the end interface
24
25sub _fgrep_process {
26 my ( $closure, @files ) = @_;
27 my $openfile = 0;
28 my $abort = 0;
29 my $i = 0;
30 foreach my $file ( @files ) {
31 my $fh;
32 if ( UNIVERSAL::isa( \$file, "SCALAR" ) ) {
33 # If it's a scalar, assume it's a file and open it
34 open FILE, "$file" or
35 ( !$SILENT and carp "Cannot open file '$file' for fgrep: $!" )
36 and next;
37 $fh = \*FILE;
38 $openfile = 1;
39 } else {
40 # Otherwise, we will assume it's a legit filehandle.
41 # If something's
42 # amiss, we'll catch it at <> below.
43 $fh = $file;
44 $openfile = 0;
45 }
46 my $line;
47 eval { $line = <$fh> };
48 # Fix for perl5.8 - thanks to Benjamin Kram
49 if ( $@ ) {
50 !$SILENT and carp "Cannot use file '$file' for fgrep: $@";
51 last;
52 } else {
53 while ( defined( $line ) ) {
54 my $state = &$closure( $i, $., $line );
55 if ( $state < 0 ) {
56 # If need to shut down whole process...
57 $abort = 1;
58 last; # while!
59 } elsif ( $state == 0 ) {
60 # If need to shut down just this file...
61 $abort = 0;
62 last; # while!
63 }
64 $line = <$fh>;
65 }
66 }
67 if ( $openfile ) { close $fh; }
68 last if ( $abort ); # Fileloop...
69 $i++; # Increment counter
70 }
71 return;
72}
73
74sub fgrep (&@) {
75 my ( $coderef, @files ) = @_;
76 if ( wantarray ) {
77 my @matches = map { { filename => $_,
78 count => 0,
79 matches => { } } } @files;
80 my $sub = sub {
81 my ( $file, $pos, $line ) = @_;
82 local $_ = $line;
83 if ( &$coderef( $file, $pos, $_ ) ) {
84 $matches[$file]->{ count }++;
85 $matches[$file]->{ matches }->{ $pos } = $line;
86 }
87 return 1;
88 };
89
90 _fgrep_process( $sub, @files );
91 return @matches;
92
93 } elsif ( defined( wantarray ) ) {
94 my $count = 0;
95 my $sub = sub {
96 my ( $file, $pos, $line ) = @_;
97 local $_ = $line;
98 if ( &$coderef( $file, $pos, $_ ) ) { $count++ };
99 return 1;
100 };
101
102 _fgrep_process( $sub, @files );
103 return $count;
104 } else {
105 my $found = 0;
106 my $sub = sub {
107 my ( $file, $pos, $line ) = @_;
108 local $_ = $line;
109 if ( &$coderef( $file, $pos, $_ ) )
110 { $found=1; return -1; }
111 else
112 { return 1; }
113 };
114 _fgrep_process( $sub, @files );
115 return $found;
116 }
117}
118
119sub fgrep_flat (&@) {
120 my ( $coderef, @files ) = @_;
121 my @matches;
122 my $sub = sub {
123 my ( $file, $pos, $line ) = @_;
124 local $_ = $line;
125 if ( &$coderef( $file, $pos, $_ ) ) {
126 push @matches, $line;
127 return 1;
128 }
129 };
130 _fgrep_process( $sub, @files );
131 return @matches;
132}
133
134sub fgrep_into ( &$@ ) {
135 my ( $coderef, $arrayref, @files ) = @_;
136 my $sub = sub {
137 my ( $file, $pos, $line ) = @_;
138 local $_ = $line;
139 if ( &$coderef( $file, $pos, $_ ) ) {
140 push @$arrayref, $line;
141 return 1;
142 }
143 };
144 _fgrep_process( $sub, @files );
145 return $arrayref;
146}
147
148sub fmap (&@) {
149 my ( $mapper, @files ) = @_;
150
151 my @mapped;
152 my $sub = sub {
153 my ( $file, $pos, $line ) = @_;
154 local $_ = $line;
155 push @mapped, &$mapper( $file, $pos, $_ );
156 return 1;
157 };
158 _fgrep_process( $sub, @files );
159 return @mapped;
160}
161
162sub fdo (&@) {
163 my ( $doer, @files ) = @_;
164 my $sub = sub {
165 my ( $file, $pos, $line ) = @_;
166 local $_ = $line;
167 &$doer( $file, $pos, $_ );
168 return 1;
169 };
170 _fgrep_process( $sub, @files );
171}
172
17313µs1;
174__END__
 
# spent 4µs within File::Grep::CORE:match which was called: # once (4µs+0s) by File::Grep::BEGIN@8 at line 11
sub File::Grep::CORE:match; # opcode