Thread Tools Display Modes
01-17-11, 06:51 PM   #1
frogofdoom
An Aku'mai Servant
AddOn Author - Click to view addons
Join Date: Oct 2005
Posts: 31
Optimization Question: Dealing with multiple returns

Warning: this is a fine point of optimization, and I suspect that the difference is purely academic.

That said, when you only need one of the returns from a function with multiple returns (i.e. return var1, var2; ), which of these is better, memory-wise and/or CPU-wise:

This one, which seems to be the preferred way of doing it now:
Code:
local someVar = select(2, SomeFunction())
or this one, which I used to see in older code:
Code:
local _, someVar = SomeFunction()
The only reason I'm asking is that I typically figure that avoiding function calls if you don't need them is a good idea, but most good code that I've looked at lately uses select().

I'd appreciate any light anyone can shed on this. Thanks in advance.
  Reply With Quote
01-17-11, 07:59 PM   #2
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
Actually, neither of those are "older" or "current". They just are two different ways of doing it.

This:
Code:
local _, var = foo()
avoids a function call.


It totally depends on how often you are calling foo() and needing the second return. If it's not often, then using select() makes no difference. If it's in a frequent OnUpdate, using a toss away variable might be more efficient.

Ultimately, though, you're talking about fractions of a millisecond of a difference.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
01-17-11, 09:33 PM   #3
frogofdoom
An Aku'mai Servant
AddOn Author - Click to view addons
Join Date: Oct 2005
Posts: 31
Thanks for answering. That sounds reasonable.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Optimization Question: Dealing with multiple returns


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

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