AFL engine code optimizer capability

I have a couple of doubts on how to write performant AFL code. Searched the forum but keep finding hits related to the optimization feature instead of results related to the below question.

Does the AFL engine detect unused variables and automatically not process them? I have a few debug variables that I plot when I suspect logic bugs. Wondering if I need to comment the variable declarations or just commenting out the plot statement is sufficient.

debug2= Sum(MA(c,20) >= MA(c,50), 20) ; //Plot(debug2,"Debug 2", colorAqua, styleLeftAxisScale);

Also, if I use MA(C, 20) twice in code, is the moving average array calculated twice or does AFL reuse previous results?

As Donald Knuth (author of The Art of Computer Programming) wrote:

Premature optimization is the root of all evil

Don't do that. Don't focus on micro-optimizations. This is waste of time. You would save one microsecond at best. As long as you write array code you are fine.

1 Like

Thanks for the reply @Tomasz. I understand what you're saying, I've quoted this to other engineers myself. There's a difference between active micro-optimisation and understanding the platform. For my understanding, could you please clarify?

Would Amibroker -
a) optimise away unused variables?
b) evaluate repeated operations like MA(C,10) once or multiple times?

AFL engine is not fixed in stone, each version is free to internally implement various techniques that are not affecting the final computation result, and whatever is done is a trade secret.

@tradecap, unused variables, IMO, should be removed by the programmer (or if you expect to still need them later for further debugging, at least commented out). You might also consider including variables and debug statements in conditional blocks that are ignored in production (as is commonly done with logging at various levels).
This entails a small execution penalty which however avoids having to comment/uncomment lines of code every time the debug phase has to be repeated.

Regarding the second question, running an extensive loop measuring the time elapsed in both conditions might give you a better idea of ​​what probably happens in the AB version you have.
And, at the same time, it shows you that, as @tomasz wrote, the difference is so minimal that, except in particular cases, you can safely ignore it!

To run extended loops just for testing purposes (normally not used in formulas) you may need to check your preferences:


Ok @Tomasz it's quite understandable.

@beppe thanks for the detailed suggestions. Both are great ideas. Think I'll add conditionals controlled by a boolean param right away. I did not know about GetPerformanceCounter. Will look into using this. If an operation is not measurably impacting the millisecond counter, its going to be a good amount of work to optimise anyway.

I came to these questions because of an AFL scan without for loops taking a long time to run. Wanted to better understand the basic structure of the AFL engine. I'll spend some more time on this and then post the code for feedback if I'm not able to solve it.

Thanks for the ideas and help guys.

Also, in AFL Code Editor you have Tools->Code Check And Profile that displays execution times for all functions used by your formula.

Generally speaking it is strongly encouraged to use variables to store results of function calls. Variable assignment (including array assignment) has near ZERO cost (it does NOT copy data, it just increases reference counter). Therefore feel free to use as many variables as you want as any function call (even optimized) has higher cost than just using variable.

Thanks for pointing out the "Code Check And Profile" feature @Tomasz. Profiler (+ debugger settings in preferences) was exactly what I was looking for. I did read the "Performance tuning tips" page already but failed to recollect this part.

I understand what you're saying about variables, I'll minimize function calls and use variables. It makes the code more readable as well.