Its generally considered good perl style to avoid doing
system calls unnecessarily. In your case: instead of
doung a system call to
date, use localtime (or
some other perl time function/module). Its generally less
prone to unexpected errors and in many cases significantly
faster to do it all in perl:
my ($day,$mon,$year)=(localtime(time))[3,4,5];
my $date=sprintf("%0d%02d%02d",$year+1900,$mon+1,$day);
a quick benchmark on my system shows the
localtime method is 39 times faster than the system call to date method:
localtime: 4 wallclock secs ( 3.10 usr + 0.06 sys = 3.16 CPU) @ 30622.78/s (n=96768)
sysdate: 31 wallclock secs ( 0.38 usr 3.65 sys + 11.15 cusr 2.36 csys = 17.54 CPU) @ 767.25/s (n=3092)
using
#!/usr/bin/perl -w
use strict;
use Benchmark;
timethese(0, {
sysdate => "chomp( my \$date = `date +\%Y\%m\%d` );",
localtime => 'my ($day,$mon,$year)=(localtime(time)
+)[3,4,5]; my $date=sprintf("%0d%02d%02d",$year+1900,$mon+1,$day);',
});
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.