Perl: code efficiency for gmtime


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl: code efficiency for gmtime
# 1  
Old 07-27-2008
Perl: code efficiency for gmtime

I have the following Perl snippet:
Code:
# get datetime
@dt = gmtime();
$strdate = 1900 + $dt[5] . addleadingzero(++$dt[4]) . addleadingzero($dt[3]) . addleadingzero($dt[2]) . addleadingzero($dt[1]) . addleadingzero($dt[0]);

# write to file
$outfile = $strdate . ".txt";
getstore($url, $outfile) or die "Error: Unable to get page $url";

sub addleadingzero {
  return $_[0] < 10 ? "0" . $_[0] : $_[0];
}

The value of $url is not important at this stage, any webpage will do. This code downloads a webpage and the filename is the current date and time (see link removed). I am concerned about the addleadingzero function, because it is called often and I would prefer to use map(), but that is impossible because of year ($dt[5]). How can I make this code more efficient/terse?

Thanks in advance

Last edited by radoulov; 07-27-2008 at 04:18 PM.. Reason: please don't post links to illegal sites
# 2  
Old 07-27-2008
1) Write addleadingzero() to handle an array and pass all the values (except year) to it. Return the array, then use it, eg

my (@myarray) = ($dt[4], $dt[3], ... etc);
my $outfilename = addleadingzero(@myarray); #returns @result, elements all properly padded....
$outfilename = $year . $result[4] . $result[3] . $result[2] .... etc.

you call addleadingzero() only once for each web-page...

2) You could try setting the year separate from the rest (different variable) then use map on the rest of them, concatenate at the end... More steps, but you cutout addleadingzero() altogether. You'd have to benchmark both and see which is faster....

3) Instead of checking numeric value of $gmtime[...] test it's length() as string and pad with "0", etc. Same as you are doing, but I suspect this one would be slower unless perl's string parsing is more efficient than its numeric... Ya just never know!!!

4) You could use map on all of them... I think you can do stuff like...

(@dt) = ($gmtime[5], $gmtime[4], ... etc, the ones you want);
(@result) = map { if (length($_) < 3 {
#process all but year
} else {
# process year
} } @dt;

Last edited by quine; 07-27-2008 at 03:59 PM.. Reason: forgot assignment to $outfilename
# 3  
Old 07-27-2008
The function should do all the date formatting and return the string ready to use. And don't use concatenation when simple string constructors will suffice.

Code:
# get datetime
$strdate = format_date((gmtime ())[0..5]);
# write to file
getstore($url, "$strdate.txt") or die "Error: Unable to get page $url";

sub format_date {
   my @t = @_;
   ++$t[4];
   $t[5]+=1900;
   for (@t[0..4]) {
      $_ = $_ < 10 ? "0$_" : $_;
   }
   return "$t[5]$t[4]$t[3]$t[2]$t[1]$t[0]"; 
}

Note that the link in your post is to a well known pirate website. Please do not post links to that website anymore. Thank you.
# 4  
Old 07-27-2008
Thank you for your suggestions. Ran a few experiments and came up with the following, which at least meets the terseness standard:
Code:
# get datetime
$strdt = strftime "%Y%m%d%H%M%S", gmtime;

# write to file
$outfile = $strdt . ".txt";
getstore($url, $outfile) or die "Error: Unable to get page $url";

It requires use POSIX qw(strftime);

Note that this solution does not require the use of the subroutine addleadingzero.

Last edited by figaro; 07-27-2008 at 04:39 PM.. Reason: Added reasoning
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

About efficiency of parallel memory allocation

Hello, there. I'm a new beginner to Linux kernel and curious about its memory management. When multiple applications apply for memory space at the same time, how Linux kernel solve the resource contending problem for high performance? I have known that there is a buddy system for allocating and... (4 Replies)
Discussion started by: blackwall
4 Replies

2. Shell Programming and Scripting

File or Folder Efficiency?

I've got this program set up so that it creates files whose unique names specify the jobs their contents describe. In order to retrieve the information inside those files, I have to do a "grep" and awk or sed to extract it. I've just assumed that making a directory with that unique name that... (1 Reply)
Discussion started by: gmark99
1 Replies

3. Shell Programming and Scripting

The efficiency between GREP and SED???

Hello Everyone! I am a newbie. I'd like to get key lines from a big txt file by Reg Exp, The file is nearly 22MB. GREP or SED?which may be the best choice,more efficient way? or any other best practise? Thank you in advance. Ever:) (5 Replies)
Discussion started by: ever
5 Replies

4. Shell Programming and Scripting

Improve program efficiency (awk)

Hi !! I've finished an awk exercise. Here it is: #!/bin/bash function calcula { # Imprimimos el mayor tamaño de fichero ls -l $1 | awk ' BEGIN { max = $5; # Inicializamos la variable que nos guardará el máximo con el tamaño del primer archivo } { if ($5 > max){ #... (8 Replies)
Discussion started by: Phass
8 Replies

5. Shell Programming and Scripting

efficiency..

how efficient is it, and how practical is it to call outside programs in a shell script (bash) for small tasks? for example, say i have a script that might preform many tasks, one of those tasks may require root access; rather than implementing inside the script a method to use su or sudo to... (11 Replies)
Discussion started by: norsk hedensk
11 Replies
Login or Register to Ask a Question