Take into account that there is a real performance difference between the three methods, which especially shows for high column numbers:
(warning: too few iterations for a reliable count)
Rate getcolname int2LATIN col2label
getcolname 0.119/s -- -100% -100%
int2LATIN 31.4/s 26218% -- -55%
col2label 68.9/s 57742% 120% --
So if this is hot code, don't use johngg's version, however nice his solution is :)
use 5.14.2;
use warnings;
use Benchmark qw( cmpthese );
use Number::Latin qw( int2LATIN );
# johngg: [id://11114715]
sub getColName {
my ($s, $n) = ("A", @_);
while ($n-- > 1) { $s++ }
$s;
} # getColName
# tux: [id://11114673]
sub col2label {
my ($s, $n) = ("", @_);
while ($n) {
use integer;
substr $s, 0, 0, chr (--$n % 26 + ord "A");
$n /= 26;
}
$s;
} # col2label
getColName (10342) eq "OGT" or die "getColName is wrong";
col2label (10342) eq "OGT" or die "col2label is wrong";
int2LATIN (10342) eq "OGT" or die "int2LATIN is wrong";
cmpthese (-4, {
getcolname => sub { getColName ($_) for 1 .. 20000 },
col2label => sub { col2label ($_) for 1 .. 20000 },
int2LATIN => sub { int2LATIN ($_) for 1 .. 20000 },
});
Enjoy, Have FUN! H.Merijn
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|