Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Calculation of the time overlapping

by Anonymous Monk
on Mar 25, 2012 at 14:05 UTC ( #961482=note: print w/ replies, xml ) Need Help??


in reply to Calculation of the time overlapping

Your problem starts here

my @arr_b = grep { $_ % 3600 == 0 } ($start_b .. $end_b); my @arr_a = grep { $_ % 3600 == 0 } ($start_a .. $end_a);

Sure, I hate to do math too, esp the fuzzy date math, but DateTime::Span already does the math for us :) no need to generate ranges like some ruby programmer :)

With my modification I get 5 second speedup

$ perl -s timeoverlap.961234.pl Start: Sun Mar 25 06:53:28 2012 Start: 0 unit6;449 unit2;1014 End: Sun Mar 25 06:53:34 2012 End: 6 $ perl -s timeoverlap.961234.pl -withDateTime Start: Sun Mar 25 06:53:37 2012 Start: 1 unit6;447 unit2;1013 End: Sun Mar 25 06:53:37 2012 End: 1

Sure my numbers don't exactly match yours but they're close enough :) maybe even more correct; here is my modification, add it to your program after split

use vars qw/ $withDateTime /; if( $withDateTime ){ use if $withDateTime,qw/ DateTime::Span /; use if $withDateTime,qw/ DateTime::Format::Strptime /; #~ use DateTime::Span; #~ use DateTime::Format::Strptime; my $strp = DateTime::Format::Strptime->new( #~ 29.10.2008 13:00:00 #~ %d.%m.%Y %T pattern => '%d.%m.%Y %T', ); my $span_a = DateTime::Span->from_datetimes( start => $strp->parse_datetime($start_a_str), end => $strp->parse_datetime($end_a_str), ); my $span_b = DateTime::Span->from_datetimes( start => $strp->parse_datetime($start_b_str), end => $strp->parse_datetime($end_b_str), ); my $set = $span_a->intersection( $span_b ); my $dur = $set->duration; my $thours = 0; $thours += $dur ->years *365*24; $thours += $dur ->months *30*24; $thours += $dur ->weeks *7*24; $thours += $dur ->days *24; $thours += $dur ->hours ; $thours += $dur ->minutes / 60 ; $thours += $dur ->seconds / 60 / 60 ; # irrelevant $thours = sprintf '%.f', $thours; # round $hash{$wds} += $thours if $thours; next STRING; }

And sure, DateTime::Duration is annoying and could have used some kind of  total_as_hours but it sure beats ranges :)


Comment on Re: Calculation of the time overlapping
Select or Download Code
Replies are listed 'Best First'.
Re^2: Calculation of the time overlapping
by vagabonding electron (Hermit) on Mar 27, 2012 at 18:33 UTC
    Thank you very much, this sounds encouraging !
    Unfortunately my ActiveState installation bites me again.
    There is no DateTime::Span module there available, only DateTime::Span::Common. If I start with DateTime::Span::Common (just a use declaration to test) it dies "Can't locate DateTime/Span.pm in @INC ..."
    Trying to install with cpan from the command line brings an error message
    "Warning: Cannot install DataTime::span, don't know what it is. Try the command i /DataTime::span/ to find objects with matching identifiers.
    There is not DateTime::Duration module, only Datetime::Duration::Fuzzy.
    :-( Update: Sorry! There is DataTime::Duration there, sorry, I have used the false script.

        Thank you very much!

        The link to the ppm repository and the hint that DateTime::Span is in the DateTime::Set distribution did it!
        Now have run the new script - in fact your script - against the "real life" data from the first post.
        Start: Tue Mar 27 21:12:58 2012 End: Tue Mar 27 21:13:34 2012
        That means over 15 times faster!
        Many many thanks again!
        VE

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://961482]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2015-07-29 23:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (269 votes), past polls