(Initially i was somewhat surprised that Tanktalus's version did not result in the right value for 2^31. I think it may be for you might not have perl compiled w/ USE_64_BIT_INT.)

A problem in the above benchmark is that not all the subroutines are returning the same type of result. I modified your above code ...

use warnings; use strict; use Date::Calc qw(Delta_DHMS Time_to_Date Delta_Days); use Date::Manip; use Benchmark qw(:all); use Data::Dumper::Simple; my @seconds = qw(-2147483648 -1 0 1 86399 86400 2147483648); my %contenders = ( cw_silly => { code => \&sec_to_dhms_silly }, cw_sensible => { code => \&sec_to_dhms_sensible }, Tanktalus => { code => \&sec_to_dhms_Tanktalus }, gam3 => { code => \&sec_to_dhms_gam3 }, gloryhack => { code => \&sec_to_dhms_gloryhack }, GrandFather => { code => \&sec_to_dhms_GrandFather }, #davidrw => { code => \&sec_to_dhms_davidrw }, #snowhare => { code => \&sec_to_dhms_snowhare }, ); for my $s (@seconds) { foreach my $c (keys %contenders) { my @res; eval { @res = $contenders{$c}{code}->($s); }; $contenders{$c}{$s} = defined @_ ? "@_" : "@res"; } } print Dumper(%contenders); our $data = 1000; cmpthese ( -10 , { cw_silly => sub { sec_to_dhms_silly($data) } , cw_sensible => sub { sec_to_dhms_sensible($data) } , Tanktalus => sub { sec_to_dhms_Tanktalus($data) } , gam3 => sub { sec_to_dhms_gam3($data) } , gloryhack => sub { sec_to_dhms_gloryhack($data) } , GrandFather => sub { sec_to_dhms_GrandFather($data) } , #davidrw => sub { sec_to_dhms_davidrw($data) } , #snowhare => sub { sec_to_dhms_snowhare($data) } } ); sub sec_to_dhms_gam3 { my $t = shift; return join ':' , int($t / 86400), (gmtime($t))[2, 1, 0]; } sub sec_to_dhms_GrandFather { my $t = shift; my @fields = ($t % 60, ($t /= 60) % 60, ($t /= 60) % 24, int ($t / 24)); return join ':', reverse @fields; } sub sec_to_dhms_Tanktalus { use integer; local $_ = shift; my ($d, $h, $m, $s); $s = $_ % 60; $_ /= 60; $m = $_ % 60; $_ /= 60; $h = $_ % 24; $_ /= 24; $d = $_; return join ':' , ($d, $h, $m, $s); } sub sec_to_dhms_sensible { shift; my ($d, $h, $m, $s); $s = $_ % 60; $_ = ($_ - $s) / 60; $m = $_ % 60; $_ = ($_ - $m) / 60; $h = $_ % 24; $_ = ($_ - $h) / 24; $d = $_; return join ':' , ($d, $h, $m, $s); } # uses Date::Calc sub sec_to_dhms_silly { my @epoch = (1970, 1, 1, 0,0,0); my @mytime = gmtime(shift); splice (@mytime, 6); # discard fields after 6th element (yea +r) $mytime[5] += 1900; # gmtime returns year - 1900 $mytime[4]++; # gmtime has zero based month @mytime = reverse @mytime; # Delta_DHMS expects args in reverse to + gmtime return join ':' , Delta_DHMS(@epoch, @mytime); } # uses Date::Calc sub sec_to_dhms_gloryhack { my $t = shift; return join ':' , $t < 0 ? '-' : '' , Delta_DHMS(Time_to_Date(0), Time_to_Date(abs($t))); } # uses Date::Calc sub sec_to_dhms_davidrw { my $t = shift; return join ':' , map { sprintf "%02d", $t } do{ my @d = Time_to_Date($t); ( Delta_Days((Time_to_Date(0))[0..2], @d[0..2]), @d[3..5 +] ) }; } # uses Date::Manip sub sec_to_dhms_snowhare { my $t = shift; return join ':' , Delta_Format("+${t}seconds",'exact','%dh','%hv','%mv','%sv'); }

Last three versions still place in the same order as in your benchmark, and snowhare version is still the slowest. Big difference is that gloryhack version becomes quite faster than cw_silly, as opposed to gloryhack version being marginally slower in your benchmark.

Below, comparison is shown, date calculation results hidden ...

%contenders = ( 'Tanktalus' => { '86399' => '0:23:59:59', '86400' => '1:0:0:0', '1' => '0:0:0:1', '2147483648' => '24855:3:14:8', '0' => '0:0:0:0', '-2147483648' => '-24855:-3:-14:-8', '-1' => '0:0:0:-1', 'code' => sub { "DUMMY" } }, 'cw_sensible' => { '86399' => '0:0:0:0', '86400' => '0:0:0:0', '1' => '0:0:0:0', '2147483648' => '0:0:0:0', '0' => '0:0:0:0', '-2147483648' => '0:0:0:0', '-1' => '0:0:0:0', 'code' => sub { "DUMMY" } }, 'cw_silly' => { '86399' => '0:23:59:59', '86400' => '1:0:0:0', '1' => '0:0:0:1', '2147483648' => '-24855:-3:-14:-8', '0' => '0:0:0:0', '-2147483648' => '-24855:-3:-14:-8', '-1' => '0:0:0:-1', 'code' => sub { "DUMMY" } }, 'gam3' => { '86399' => '0:23:59:59', '86400' => '1:0:0:0', '1' => '0:0:0:1', '2147483648' => '24855:20:45:52', '0' => '0:0:0:0', '-2147483648' => '-24855:20:45:52', '-1' => '0:23:59:59', 'code' => sub { "DUMMY" } }, 'GrandFather' => { '86399' => '0:23:59:59', '86400' => '1:0:0:0', '1' => '0:0:0:1', '2147483648' => '24855:3:14:8', '0' => '0:0:0:0', '-2147483648' => '-24855:21:46:52', '-1' => '0:0:0:59', 'code' => sub { "DUMMY" } }, 'gloryhack' => { '86399' => ':0:23:59:59', '86400' => ':1:0:0:0', '1' => ':0:0:0:1', '2147483648' => '', '0' => ':0:0:0:0', '-2147483648' => '', '-1' => '-:0:0:0:1', 'code' => sub { "DUMMY" } } );
Rate cw_sensible cw_silly gloryhack GrandFather gam3 + Tanktalus cw_sensible 27248/s -- -46% -67% -73% -75% + -83% cw_silly 50161/s 84% -- -40% -49% -55% + -68% gloryhack 83435/s 206% 66% -- -16% -25% + -47% GrandFather 99258/s 264% 98% 19% -- -10% + -37% gam3 110623/s 306% 121% 33% 11% -- + -30% Tanktalus 157554/s 478% 214% 89% 59% 42% + --

In reply to Re^2: Converting seconds to DD:HH:MM:SS by parv
in thread Converting seconds to DD:HH:MM:SS by McDarren

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.