What is faster in AFL and what is best practice?

This is really the simplest way I can think of asking the question but I refer to other functions as well, especially more computational heavy ones.

Is code A faster than B? Or is it the same?
And in general is it better to assign a function to a variable/identifier if it's used multiple times?

Thanks in advance.
ps. I read the docs about how amibroker works and that's why I ask :smiley:

A

tn = TimeNum();
StartBar = tn == rangeStart;
EndBar = tn == rangeEnd;

B

StartBar = TimeNum() == rangeStart;
EndBar = TimeNum() == rangeEnd;
1 Like

"A" scenario is faster. Define one time use multiple times.

1 Like

I dont think that is how script engines work. Both are the same.

First time AFL runs, it will take time to generate some machine code. After that, it will use it and run fast.
If formula isn't changing, it wont make sense to parse on every run.

If either A or B was faster, i'm sure there would have been a KB article on it.

1 Like

I have the same understanding @nsm51

I think TimeNum() is a timestamp array by default(like O,H,L...) but in the case of other functions like RSI(14) for instance the function gets an unique identifier and gets created in the array matrix.
If you reference RSI(14) a million times in the same AFL under the hood it has the same identifier and it points to the same array in the matrix.
I have the impression tn = TimeNum(); will double the timpestamp array, but that is just a guess. If it is so code A is actually worse.
In javascript and other languages I can tell you for sure code A would be faster if TimeNum() was a function.

The reason I ask is I'm new to AFL and want to be careful what I worry about when I write code and make sure I establish good practices from the get go as I want to shorten the learning curve as much as I can.

1 Like

TimeNum() is a function and code A) is faster. It is always preferable/faster to use variables instead of function call, if possible.

In AFL Editor there is a Tools->Code Check And Profile window that provides exact timing so it answers questions "what is faster" very clearly.

Thank you all for the feedback, I guess this one is settled!

Good to know about Code Check And Profile .