Platinum Customer
Joined: 6/30/2014 Posts: 60
|
Hi, I have a general programming question. How can we write quick code?
One sure way is to use fewer commands! I'm clear about the code: (a*b + a*c) is better written a*(b+c). There is one less command.
What about nested IIF() statements? Is this code: IIF(a,1,IIF(b,2,IIF(c,3,0) faster that it's equivalent:
IIF(a,1,0) IIF(b,2,0) IIF(c,3,0)
Just wondering, because I'm scanning all stocks during cribbage.
Thanks
|
Worden Trainer
Joined: 10/7/2004 Posts: 65,138
|
Other than actually testing the formulas (which I have done fairly frequently), I really don't know.
I don't think the nested IIF() example is actually the same as the non-nested version. Not sure the non-nested version is actually valid as written.
That said, I am pretty sure the nested version would be faster if the second version were valid. The program definitely seems to short circuit the calculation of nested IIF() functions when it finds a true.
-Bruce Personal Criteria Formulas TC2000 Support Articles
|
Platinum Customer
Joined: 6/30/2014 Posts: 60
|
Thanks for the reply and I'm sorry for the BAD code. Here is an example of the code I'm talking about with using nested IIF statements: (CAM Indicator)
IIF(ADX10.10>=ADX10.10.1 AND MACD12.26>MACD12.26.1,4, IIF(ADX10.10<=ADX10.10.1 AND MACD12.26<MACD12.26.1,3, IIF(ADX10.10>=ADX10.10.1 AND MACD12.26<MACD12.26.1,2, IIF(ADX10.10<=ADX10.10.1 AND MACD12.26>MACD12.26.1,1,0))))
is it faster than this (yes I know that the results can not be the same, but the commands are similar; just in theory)
IIF(ADX10.10<=ADX10.10.1 AND MACD12.26<MACD12.26.1,3,0)the
IIF(ADX10.10>=ADX10.10.1 AND MACD12.26>MACD12.26.1,4,0)
IIF(ADX10.10>=ADX10.10.1 AND MACD12.26<MACD12.26.1,2, 0)
IIF(ADX10.10<=ADX10.10.1 AND MACD12.26>MACD12.26.1,1,0)
When I test the nested IIF code in edit mode, all commands are executed. Whether they all are executed during realtime running is the question.
Thanks
|
Worden Trainer
Joined: 10/7/2004 Posts: 65,138
|
Formulas definitely don't generate division by zero errors if they don't get that far in the nested version when running. So it is short circuiting the calculations.
It isn't enough to make a difference in a formula test (both come out as medium). The nested formula might be slightly quicker when sorting every single item in the system, but both are fast enough that it wouldn't make much of a difference in the real world.
-Bruce Personal Criteria Formulas TC2000 Support Articles
|