← Index
NYTProf Performance Profile   « line view »
For term-colors.pl
  Run on Thu Dec 21 13:47:32 2023
Reported on Thu Dec 21 13:55:07 2023

Filename/home/bakers/term-colors.pl
StatementsExecuted 22763 statements in 9.24ms
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
256113.25ms3.25msmain::::in_arraymain::in_array
1111.64ms1.66msmain::::BEGIN@4main::BEGIN@4
256111.12ms4.37msmain::::needs_whitemain::needs_white
51341372µs372µsmain::::set_fcolormain::set_fcolor
111306µs330µsmain::::BEGIN@3main::BEGIN@3
1110111224µs224µsmain::::CORE:printmain::CORE:print (opcode)
29831218µs218µsmain::::set_bcolormain::set_bcolor
2562191µs91µsmain::::CORE:prtfmain::CORE:prtf (opcode)
11113µs17µsmain::::BEGIN@0main::BEGIN@0 (xsub)
1115µs7µsmain::::ENDmain::END
1114µs4µsmain::::CORE:ftfilemain::CORE:ftfile (opcode)
221700ns700nsmain::::CORE:matchmain::CORE:match (opcode)
0000s0smain::::RUNTIMEmain::RUNTIME
0000s0smain::::get_color_mappingmain::get_color_mapping
0000s0smain::::get_color_namesmain::get_color_names
0000s0smain::::has_term_ansicolormain::has_term_ansicolor
0000s0smain::::term_ansicolormain::term_ansicolor
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
0328µsProfile data that couldn't be associated with a specific line:
# spent 17µs making 1 call to main::BEGIN@0 # spent 7µs making 1 call to main::END # spent 4µs making 1 call to main::CORE:ftfile
1364µs#!/usr/bin/perl
2
32195µs2333µs
# spent 330µs (306+24) within main::BEGIN@3 which was called: # once (306µs+24µs) by main::NULL at line 3
use strict;
# spent 330µs making 1 call to main::BEGIN@3 # spent 2µs making 1 call to strict::import
422.12ms21.67ms
# spent 1.66ms (1.64+14µs) within main::BEGIN@4 which was called: # once (1.64ms+14µs) by main::NULL at line 4
use warnings;
# spent 1.66ms making 1 call to main::BEGIN@4 # spent 12µs making 1 call to warnings::import
5
61800nsmy $args = join(" ",@ARGV);
714µs1600nsmy ($perl) = $args =~ /--perl/;
# spent 600ns making 1 call to main::CORE:match
811µs1100nsmy ($both) = $args =~ /--both/;
# spent 100ns making 1 call to main::CORE:match
9
10# If we want both, we set perl also
111100nsif ($both) {
12 $perl = 1;
13}
14
15# Term::ANSIColor didn't get 256 color constants until 4.0
161400nsif ($perl && has_term_ansicolor(4.0)) {
17 require Term::ANSIColor;
18 Term::ANSIColor->import(':constants','color','uncolor');
19
20 #print "TERM::ANSIColor constant names:\n";
21 term_ansicolor();
22} else {
231200ns my $section = 1;
241100ns my $grouping = 8;
25
26135µs for (my $i = 0; $i < 256; $i++) {
27256255µs512221µs print set_bcolor($i); # Set the background color
# spent 191µs making 256 calls to main::set_bcolor, avg 747ns/call # spent 30µs making 256 calls to main::CORE:print, avg 116ns/call
28
29256130µs2564.37ms if (needs_white($i)) {
# spent 4.37ms making 256 calls to main::needs_white, avg 17µs/call
306876µs13666µs print set_fcolor(15); # White
# spent 55µs making 68 calls to main::set_fcolor, avg 810ns/call # spent 10µs making 68 calls to main::CORE:print, avg 154ns/call
316869µs6824µs printf(" %03d ",$i); # Ouput the color number in white
# spent 24µs making 68 calls to main::CORE:prtf, avg 353ns/call
32 } else {
33188202µs376183µs print set_fcolor(0); # Black
# spent 153µs making 188 calls to main::set_fcolor, avg 814ns/call # spent 30µs making 188 calls to main::CORE:print, avg 160ns/call
34188189µs18867µs printf(" %03d ",$i); # Ouput the color number in black
# spent 67µs making 188 calls to main::CORE:prtf, avg 356ns/call
35 }
36
37256261µs512189µs print set_fcolor(); # Reset both colors
# spent 163µs making 256 calls to main::set_fcolor, avg 637ns/call # spent 26µs making 256 calls to main::CORE:print, avg 100ns/call
38256162µs25626µs print " "; # Seperator
# spent 26µs making 256 calls to main::CORE:print, avg 100ns/call
39
4025643µs if ($i == 15 || $i == 231) {
4122µs41µs print set_bcolor(); # Reset
# spent 1µs making 2 calls to main::set_bcolor, avg 600ns/call # spent 200ns making 2 calls to main::CORE:print, avg 100ns/call
4227µs26µs print "\n\n";
# spent 6µs making 2 calls to main::CORE:print, avg 3µs/call
432300ns $section = 0;
442200ns $grouping = 6;
45 } elsif ($section > 0 && ($section % $grouping == 0)) {
464042µs8029µs print set_bcolor(); # Reset
# spent 25µs making 40 calls to main::set_bcolor, avg 628ns/call # spent 4µs making 40 calls to main::CORE:print, avg 100ns/call
4740119µs4092µs print "\n";
# spent 92µs making 40 calls to main::CORE:print, avg 2µs/call
48 }
49
5025635µs $section++;
51 }
52}
53
54
# spent 7µs (5+2) within main::END which was called: # once (5µs+2µs) by main::RUNTIME at line 0
END {
5512µs2700ns print set_fcolor(); # Reset the colors
# spent 500ns making 1 call to main::set_fcolor # spent 200ns making 1 call to main::CORE:print
5614µs11µs print "\n";
# spent 1µs making 1 call to main::CORE:print
57}
58
59#################################################################################
60
61sub has_term_ansicolor {
62 my $version = shift();
63
64 eval {
65 # Check if we have Term::ANSIColor version 4.0
66 require Term::ANSIColor;
67 Term::ANSIColor->VERSION($version);
68 };
69
70 if ($@) {
71 return 0;
72 } else {
73 return 1;
74 }
75}
76
77
# spent 372µs within main::set_fcolor which was called 513 times, avg 724ns/call: # 256 times (163µs+0s) by main::RUNTIME at line 37, avg 637ns/call # 188 times (153µs+0s) by main::RUNTIME at line 33, avg 814ns/call # 68 times (55µs+0s) by main::RUNTIME at line 30, avg 810ns/call # once (500ns+0s) by main::END at line 55
sub set_fcolor {
7851345µs my $c = shift();
79
8051341µs my $ret = '';
8151371µs if (!defined($c)) { $ret = "\e[0m"; } # Reset the color
8225638µs else { $ret = "\e[38;5;${c}m"; }
83
84513303µs return $ret;
85}
86
87
# spent 218µs within main::set_bcolor which was called 298 times, avg 730ns/call: # 256 times (191µs+0s) by main::RUNTIME at line 27, avg 747ns/call # 40 times (25µs+0s) by main::RUNTIME at line 46, avg 628ns/call # 2 times (1µs+0s) by main::RUNTIME at line 41, avg 600ns/call
sub set_bcolor {
8829824µs my $c = shift();
89
9029821µs my $ret = '';
9129834µs if (!defined($c)) { $ret = "\e[0m"; } # Reset the color
9225642µs else { $ret .= "\e[48;5;${c}m"; }
93
94298176µs return $ret;
95}
96
97sub get_color_mapping {
98 my $map = {};
99
100 for (my $i = 0; $i < 256; $i++) {
101 my $str = "\e[38;5;${i}m";
102 my ($acc) = uncolor($str);
103
104 $map->{$acc} = int($i);
105 }
106
107 return $map;
108}
109
110sub term_ansicolor {
111 my @colors = get_color_names();
112 my $map = get_color_mapping();
113
114 my $absolute = 0;
115 my $group = 0;
116 my $grouping = 8;
117
118 print "Showing Term::ANSIColor constant names\n\n";
119
120 foreach my $name (@colors) {
121 my $bg = "on_$name";
122 my $map_num = int($map->{$name});
123 my $perl_name = sprintf("%6s",$name);
124 my $ansi_number = sprintf("#%03i",$map_num);
125
126 my $name_string = "";
127 if ($both) {
128 $name_string = "$perl_name / $ansi_number";
129 } else {
130 $name_string = "$perl_name";
131 }
132
133 if (needs_white($map_num)) {
134 print color($bg) . " " . color('bright_white') . $name_string . " ";
135 } else {
136 print color($bg) . " " . color("black") . $name_string . " ";
137 }
138 print color('reset') . " ";
139
140 $absolute++;
141 $group++;
142
143 if ($absolute == 16 || $absolute == 232) {
144 print "\n\n";
145 $group = 0;
146 $grouping = 6;
147 } elsif ($group % $grouping == 0) {
148 print "\n";
149 }
150 }
151}
152
153sub get_color_names {
154 my @colors = ();
155 my ($r,$g,$b) = 0;
156
157 for (my $i = 0; $i < 16; $i++) {
158 my $name = "ansi$i";
159 push(@colors,$name);
160 }
161
162 for ($r = 0; $r <= 5; $r++) {
163 for ($g = 0; $g <= 5; $g++) {
164 for ($b = 0; $b <= 5; $b++) {
165 my $name = "rgb$r$g$b";
166 push(@colors,$name);
167 }
168 }
169 }
170
171 for (my $i = 0; $i < 24; $i++) {
172 my $name = "grey$i";
173 push(@colors,$name);
174 }
175
176 return @colors;
177}
178
179
# spent 4.37ms (1.12+3.25) within main::needs_white which was called 256 times, avg 17µs/call: # 256 times (1.12ms+3.25ms) by main::RUNTIME at line 29, avg 17µs/call
sub needs_white {
180 # Sorta lame, but it's a hard coded list of which background colors need a white foreground
181256623µs my @needs_white = qw(0 1 4 5 8 232 233 234 235 236 237 238 239 240 241 242 243 16 17 18
182 19 20 21 22 28 52 53 54 55 25 56 57 58 59 60 88 89 90 91 92 93 124 125 29 30 31 26
183 27 61 62 64 160 196 161 126 63 94 95 100 101 127 128 129 12 130 131 23 24);
184
18525621µs my $num = shift();
186256117µs2563.25ms my $ret = in_array($num, @needs_white);
# spent 3.25ms making 256 calls to main::in_array, avg 13µs/call
187
188256343µs return $ret;
189}
190
191
# spent 3.25ms within main::in_array which was called 256 times, avg 13µs/call: # 256 times (3.25ms+0s) by main::needs_white at line 186, avg 13µs/call
sub in_array {
192256565µs my ($needle, @haystack) = @_;
193
19425627µs foreach my $l (@haystack) {
195151302.47ms if ($l == $needle) { return 1; }
196 }
197
198188264µs return 0;
199}
 
# spent 17µs (13+4) within main::BEGIN@0 which was called: # once (13µs+4µs) by main::NULL at line 0
sub main::BEGIN@0; # xsub
# spent 4µs within main::CORE:ftfile which was called: # once (4µs+0s) by main::BEGIN@0 at line 0
sub main::CORE:ftfile; # opcode
# spent 700ns within main::CORE:match which was called 2 times, avg 350ns/call: # once (600ns+0s) by main::RUNTIME at line 7 # once (100ns+0s) by main::RUNTIME at line 8
sub main::CORE:match; # opcode
# spent 224µs within main::CORE:print which was called 1110 times, avg 202ns/call: # 256 times (30µs+0s) by main::RUNTIME at line 27, avg 116ns/call # 256 times (26µs+0s) by main::RUNTIME at line 37, avg 100ns/call # 256 times (26µs+0s) by main::RUNTIME at line 38, avg 100ns/call # 188 times (30µs+0s) by main::RUNTIME at line 33, avg 160ns/call # 68 times (10µs+0s) by main::RUNTIME at line 30, avg 154ns/call # 40 times (92µs+0s) by main::RUNTIME at line 47, avg 2µs/call # 40 times (4µs+0s) by main::RUNTIME at line 46, avg 100ns/call # 2 times (6µs+0s) by main::RUNTIME at line 42, avg 3µs/call # 2 times (200ns+0s) by main::RUNTIME at line 41, avg 100ns/call # once (1µs+0s) by main::END at line 56 # once (200ns+0s) by main::END at line 55
sub main::CORE:print; # opcode
# spent 91µs within main::CORE:prtf which was called 256 times, avg 355ns/call: # 188 times (67µs+0s) by main::RUNTIME at line 34, avg 356ns/call # 68 times (24µs+0s) by main::RUNTIME at line 31, avg 353ns/call
sub main::CORE:prtf; # opcode