| Filename | /home/bakers/term-colors.pl |
| Statements | Executed 22763 statements in 9.24ms |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 256 | 1 | 1 | 3.25ms | 3.25ms | main::in_array |
| 1 | 1 | 1 | 1.64ms | 1.66ms | main::BEGIN@4 |
| 256 | 1 | 1 | 1.12ms | 4.37ms | main::needs_white |
| 513 | 4 | 1 | 372µs | 372µs | main::set_fcolor |
| 1 | 1 | 1 | 306µs | 330µs | main::BEGIN@3 |
| 1110 | 11 | 1 | 224µs | 224µs | main::CORE:print (opcode) |
| 298 | 3 | 1 | 218µs | 218µs | main::set_bcolor |
| 256 | 2 | 1 | 91µs | 91µs | main::CORE:prtf (opcode) |
| 1 | 1 | 1 | 13µs | 17µs | main::BEGIN@0 (xsub) |
| 1 | 1 | 1 | 5µs | 7µs | main::END |
| 1 | 1 | 1 | 4µs | 4µs | main::CORE:ftfile (opcode) |
| 2 | 2 | 1 | 700ns | 700ns | main::CORE:match (opcode) |
| 0 | 0 | 0 | 0s | 0s | main::RUNTIME |
| 0 | 0 | 0 | 0s | 0s | main::get_color_mapping |
| 0 | 0 | 0 | 0s | 0s | main::get_color_names |
| 0 | 0 | 0 | 0s | 0s | main::has_term_ansicolor |
| 0 | 0 | 0 | 0s | 0s | main::term_ansicolor |
| Line | State ments |
Time on line |
Calls | Time in subs |
Code |
|---|---|---|---|---|---|
| 0 | 3 | 28µs | Profile 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 | ||
| 1 | 3 | 64µs | #!/usr/bin/perl | ||
| 2 | |||||
| 3 | 2 | 195µs | 2 | 333µs | # spent 330µs (306+24) within main::BEGIN@3 which was called:
# once (306µs+24µs) by main::NULL at line 3 # spent 330µs making 1 call to main::BEGIN@3
# spent 2µs making 1 call to strict::import |
| 4 | 2 | 2.12ms | 2 | 1.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 # spent 1.66ms making 1 call to main::BEGIN@4
# spent 12µs making 1 call to warnings::import |
| 5 | |||||
| 6 | 1 | 800ns | my $args = join(" ",@ARGV); | ||
| 7 | 1 | 4µs | 1 | 600ns | my ($perl) = $args =~ /--perl/; # spent 600ns making 1 call to main::CORE:match |
| 8 | 1 | 1µs | 1 | 100ns | my ($both) = $args =~ /--both/; # spent 100ns making 1 call to main::CORE:match |
| 9 | |||||
| 10 | # If we want both, we set perl also | ||||
| 11 | 1 | 100ns | if ($both) { | ||
| 12 | $perl = 1; | ||||
| 13 | } | ||||
| 14 | |||||
| 15 | # Term::ANSIColor didn't get 256 color constants until 4.0 | ||||
| 16 | 1 | 400ns | if ($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 { | ||||
| 23 | 1 | 200ns | my $section = 1; | ||
| 24 | 1 | 100ns | my $grouping = 8; | ||
| 25 | |||||
| 26 | 1 | 35µs | for (my $i = 0; $i < 256; $i++) { | ||
| 27 | 256 | 255µs | 512 | 221µ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 | |||||
| 29 | 256 | 130µs | 256 | 4.37ms | if (needs_white($i)) { # spent 4.37ms making 256 calls to main::needs_white, avg 17µs/call |
| 30 | 68 | 76µs | 136 | 66µ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 |
| 31 | 68 | 69µs | 68 | 24µ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 { | ||||
| 33 | 188 | 202µs | 376 | 183µ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 |
| 34 | 188 | 189µs | 188 | 67µ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 | |||||
| 37 | 256 | 261µs | 512 | 189µ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 |
| 38 | 256 | 162µs | 256 | 26µs | print " "; # Seperator # spent 26µs making 256 calls to main::CORE:print, avg 100ns/call |
| 39 | |||||
| 40 | 256 | 43µs | if ($i == 15 || $i == 231) { | ||
| 41 | 2 | 2µs | 4 | 1µ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 |
| 42 | 2 | 7µs | 2 | 6µs | print "\n\n"; # spent 6µs making 2 calls to main::CORE:print, avg 3µs/call |
| 43 | 2 | 300ns | $section = 0; | ||
| 44 | 2 | 200ns | $grouping = 6; | ||
| 45 | } elsif ($section > 0 && ($section % $grouping == 0)) { | ||||
| 46 | 40 | 42µs | 80 | 29µ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 |
| 47 | 40 | 119µs | 40 | 92µs | print "\n"; # spent 92µs making 40 calls to main::CORE:print, avg 2µs/call |
| 48 | } | ||||
| 49 | |||||
| 50 | 256 | 35µ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 | ||||
| 55 | 1 | 2µs | 2 | 700ns | print set_fcolor(); # Reset the colors # spent 500ns making 1 call to main::set_fcolor
# spent 200ns making 1 call to main::CORE:print |
| 56 | 1 | 4µs | 1 | 1µs | print "\n"; # spent 1µs making 1 call to main::CORE:print |
| 57 | } | ||||
| 58 | |||||
| 59 | ################################################################################# | ||||
| 60 | |||||
| 61 | sub 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 | ||||
| 78 | 513 | 45µs | my $c = shift(); | ||
| 79 | |||||
| 80 | 513 | 41µs | my $ret = ''; | ||
| 81 | 513 | 71µs | if (!defined($c)) { $ret = "\e[0m"; } # Reset the color | ||
| 82 | 256 | 38µs | else { $ret = "\e[38;5;${c}m"; } | ||
| 83 | |||||
| 84 | 513 | 303µs | return $ret; | ||
| 85 | } | ||||
| 86 | |||||
| 87 | sub set_bcolor { | ||||
| 88 | 298 | 24µs | my $c = shift(); | ||
| 89 | |||||
| 90 | 298 | 21µs | my $ret = ''; | ||
| 91 | 298 | 34µs | if (!defined($c)) { $ret = "\e[0m"; } # Reset the color | ||
| 92 | 256 | 42µs | else { $ret .= "\e[48;5;${c}m"; } | ||
| 93 | |||||
| 94 | 298 | 176µs | return $ret; | ||
| 95 | } | ||||
| 96 | |||||
| 97 | sub 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 | |||||
| 110 | sub 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 | |||||
| 153 | sub 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 | ||||
| 180 | # Sorta lame, but it's a hard coded list of which background colors need a white foreground | ||||
| 181 | 256 | 623µ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 | |||||
| 185 | 256 | 21µs | my $num = shift(); | ||
| 186 | 256 | 117µs | 256 | 3.25ms | my $ret = in_array($num, @needs_white); # spent 3.25ms making 256 calls to main::in_array, avg 13µs/call |
| 187 | |||||
| 188 | 256 | 343µ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 | ||||
| 192 | 256 | 565µs | my ($needle, @haystack) = @_; | ||
| 193 | |||||
| 194 | 256 | 27µs | foreach my $l (@haystack) { | ||
| 195 | 15130 | 2.47ms | if ($l == $needle) { return 1; } | ||
| 196 | } | ||||
| 197 | |||||
| 198 | 188 | 264µ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 | |||||
# spent 4µs within main::CORE:ftfile which was called:
# once (4µs+0s) by main::BEGIN@0 at line 0 | |||||
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:prtf; # opcode |