I am using DateTime in a while loop with over 1 million (up to 300 million) lines to calculate a date & time field. Currently I had all of the code in the while loop. I moved the $formatter code outside of the while loop. But the speed is very slow. The primary reason I am using the DateTime module is to convert the data which is in UTC to New York time. The date changes since I am going past 12AM with the time zone change. Any suggestions on how I can speed this up? Should I avoid DateTime due to the number of rows?
while(<>){ chomp; $line = $_; my @line = split(';',$line); $line[4] =~ s/:/\//; ($month) = $line[4] =~ m/(\d+)\/\d+/; ($day) = $line[4] =~ m/\d+\/(\d+)/; $hour = (int($line[7]/3600000)) % 24; $minute = sprintf '%02d', (int($line[7]/60000)) % 60; $second = sprintf '%02d', (int($line[7]/1000)) % 60; $ms = sprintf '%03d', $line[7] % 1000; $formatter = new DateTime::Format::Strptime( pattern => '%Y-%m-%d %H:%M:%S.%3N'); $dhms = DateTime->new(year => $line[5], month => $month, day => $day, hour => $hour, minute => $minute, second => $second, nanosecond => $ms * 1000000, time_zone => 'Etc/UTC', formatter => $formatter, ); $dhms = $dhms->clone->set_time_zone('America/New_York'); }

In reply to DateTime speed improvement - suggestion by ler224

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.