![]() |
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) Code:
my(undef, undef, undef, $day, $mon, $year, undef, undef, undef)Is this true? Thanks, Mr. Snorfles |
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 |
Here ya go:
($DAY, $MONTH, $YEAR) = (localtime)[3,4,5]; |
Thanks... I didn't know you could do the indexing on lists like that.
--Mr. Snorfles |
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. |
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? |
| All times are GMT -5. The time now is 02:21 PM. |