LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-20-2007, 12:26 PM   #1
mistersnorfles
Member
 
Registered: Aug 2007
Distribution: Gentoo 2007.0
Posts: 51

Rep: Reputation: 15
Perl: Optimising List Assignment


If I am copying the values from a function that returns a list value to a list of scalars, and I don't need all of the function's returns, do I gain any performance increase by assigning the unused values to undef? For example let's say I was using the localtime function, and all I wanted was the day, month, and year:

I could do this:
Code:
my($sec, $min, $hour, $day, $mon, $year, $wday, $yday, $isdst) 
= localtime 1180630098;
but I am wondering if this would be more efficient (speed-wise and memory-wise):

Code:
my(undef, undef, undef, $day, $mon, $year, undef, undef, undef)
= localtime 1180630098;
since the memory doesn't have to be allocated for the new variables, and the extra copying doesn't have to be done.

Is this true?

Thanks,
Mr. Snorfles

Last edited by mistersnorfles; 08-20-2007 at 12:32 PM.
 
Old 08-20-2007, 03:40 PM   #2
mistersnorfles
Member
 
Registered: Aug 2007
Distribution: Gentoo 2007.0
Posts: 51

Original Poster
Rep: Reputation: 15
I looked over my post, and realized that would create a nine variable array -- probably not helpful. Any ideas how I should go about getting only those three values?

Thanks,
Mr. snorfles
 
Old 08-20-2007, 07:41 PM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,261

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
Here ya go:

($DAY, $MONTH, $YEAR) = (localtime)[3,4,5];
 
Old 08-20-2007, 09:29 PM   #4
mistersnorfles
Member
 
Registered: Aug 2007
Distribution: Gentoo 2007.0
Posts: 51

Original Poster
Rep: Reputation: 15
Thanks... I didn't know you could do the indexing on lists like that.

--Mr. Snorfles
 
Old 08-21-2007, 05:09 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
but, i don't think you'll gain much in speed or memory.
Remember, localtime is making a slow system call anyway - so whether it return 9 or 3 will be a drop in the bucket timewise.
 
Old 08-21-2007, 09:41 PM   #6
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,261

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
As BB said, it's the overall fn elapsed time, not the num of returned values.
However, this sounds like 'premature optimization'.
If you really ALREADY have performance issues, a better (overall) algorithm will give you much larger gains.
You won't get anything out of fooling around with this stuff above.
Perhaps you could tell show us your code & mention where it seems slow?
 
  


Reply

Tags
assignment, copying, list, perl


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to list all installed Perl modules SharpyWarpy Linux - General 8 12-12-2011 04:48 AM
type of list element in Perl ? thelonius Programming 2 06-20-2006 08:49 AM
Debian optimising nefryt Debian 1 04-14-2006 05:39 PM
how to list/install perl modules lsimon4180 Linux - Software 1 10-02-2004 12:57 PM
Optimising squid dunkyb Linux - Software 2 02-21-2003 11:06 PM


All times are GMT -5. The time now is 10:34 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration