[PL] Code optimisation

Questions about MultiCharts and user contributed studies.
duration
Posts: 179
Joined: 20 Dec 2005
Been thanked: 1 time

[PL] Code optimisation

Postby duration » 07 Apr 2010

Dear All,

I would like to know if there is any way of optimising a powerlanguage code, so that it compiles, executes and optimise faster.

I have heard for example that multiplication is faster than division: * .5 instead of /2 is faster.

I am wondering if the following would be faster as well. Let's say you have 10 conditions. You could write this way:

Code: Select all

condtion1 = close > low;
condition2 = ema >= sma;
.
.
.
condition10 = close[1] > open[1]


if condition1 and condition2 ..... and condition10 then buy etc.
Or you could write it this way

Code: Select all

condition1 = close > low;
if condition1 then
begin
condition2 = ema > sma;
if condition2 then
begin
.
.
.
if condition10 then begin buy etc.
Which way would be faster?

Many thanks,

User avatar
Bruce DeVault
Posts: 438
Joined: 19 Jan 2010
Location: Washington DC
Been thanked: 2 times
Contact:

Postby Bruce DeVault » 07 Apr 2010

The second way would be faster, but only marginally for the example you've given. The difference would only be consequential with more time consuming operations.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Postby TJ » 07 Apr 2010

in computing terms, the second one could be significantly faster; as much as 10 times faster.

if the first condition is met, then it can skip all the rest of the calculations...


in human terms, probably it does not matter,
especially with today's computer.

you might be looking at the difference between 0.1 nano second and 1 nanosecond...
(I am just pulling numbers out of the air)
what I want to illustrate is:
in computing terms, the difference is significant.
but in human terms, you won't notice it.


Open your Task Manager,
you should see the CPU's utilization.

make 2 test indicators and see the CPU usage for yourself.
do this test:
1. on the 1st code: duplicate the first code 10 times and force the calculation to take condition10,
2. on the 2nd code: force the second code to take condition1,

I bet... you won't see a noticeable difference.

:-)

User avatar
geizer
Posts: 375
Joined: 16 Jun 2008
Has thanked: 40 times
Been thanked: 38 times

Postby geizer » 07 Apr 2010

In order to answer this question you need to know how compiler interprets these structures, as well as have some experience programming in X86 Assembler. The documentation on Power Language compiler does not provide enough information to draw a conclusion. There are different ways the code can be translated into machine instructions by the compiler. I would not be surprised if these examples result in identical machine code if you select "optimize for execution" option in your Power Language compiler.


I would just stick to whatever method is less error-prone and takes less of your time to troubleshoot. Once it's working you may revisit your code if performance becomes a concern.

janus
Posts: 835
Joined: 25 May 2009
Has thanked: 63 times
Been thanked: 104 times

Re: [PL] Code optimisation

Postby janus » 08 Apr 2010

If you have an "if" statement like this:

if high > close[1] and close >= close[2] then begin

and if the first test fails the code doesn't even bother to check for the second test. So, it's not necessary to have indented if ... then begin blocks, apart for making the code more readable if there's a long list of tests. Otherwise, put the more complex tests last.

duration
Posts: 179
Joined: 20 Dec 2005
Been thanked: 1 time

Postby duration » 08 Apr 2010

Thank you for all you reply!

I have also heard that avoiding the use of functions and programming them in the code help speedin up.

Is that true?

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Postby TJ » 08 Apr 2010

Thank you for all you reply!

I have also heard that avoiding the use of functions and programming them in the code help speedin up.

Is that true?
no.

janus
Posts: 835
Joined: 25 May 2009
Has thanked: 63 times
Been thanked: 104 times

Postby janus » 08 Apr 2010

Thank you for all you reply!

I have also heard that avoiding the use of functions and programming them in the code help speedin up.
Is that true?
It depends if the function storage is Simple or Series. Simple makes negligible difference but Series can make a significant difference. 99% of my functions use Simple. In any case I much prefer to use functions as I'm a great believer in modular programming. It makes the coding more readable and far easier to debug.

User avatar
Bruce DeVault
Posts: 438
Joined: 19 Jan 2010
Location: Washington DC
Been thanked: 2 times
Contact:

Postby Bruce DeVault » 09 Apr 2010

No less a sage than Donald Knuth made two balancing statements about optimizing computer code for speed:

"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil"

and

"In established engineering disciplines a 12 % improvement, easily obtained, is never considered marginal and I believe the same viewpoint should prevail in software engineering".

(See Wikipedia "program optimization" for more background on this long-running discussion.)

In general, you should build your code in a way that's maintainable, understandable, and that helps you do good work. While there are certainly cases where it's important to code something in the way that runs the absolute fastest possible, it's really true less often than might be supposed, and it's generally more important in trading to be sure you have the right answer, than to have what you think may be the right answer faster.

This is especially true given the "facts of life" regarding the speed of retail internet connections, and the transaction costs at common retail brokers e.g. IB, Mirus, etc. - true high speed market participants have bought seats on the exchange and colocated their servers at the exchange, so trying to out-speed them in real-time execution from a home office is a losing proposition in general - however, profitable niches can be found and are found all the time if you are willing to specialize and "out-smart" rather than try to simply "out-run". (Sometimes, you can still find a small pond in which to fish in which you can "out-run" the other participants even from a home internet connection, but the thing is... that's a transient opportunity - one only there until it attracts the attention of someone who can out-run you which is sometimes an amazingly short period of time.)

The one place where code optimization for speed comes up most frequently is when dealing with optimization of trading system input parameters, where a great many iterations must be performed - in this case, saving 10% in time here and another 10% there can add up. However, even so, it generally makes sense to address these kinds of improvements (e.g. inlining functions if/where warranted) after the concept has been proven and you're finally settling into a routine e.g. every weekend or weeknight optimization and wanting to shave a few hours off the run, rather than always developing code that way from the start, because of the strong advantages of modular construction in helping you debug and make sure your concepts are correct long before you get to the point of trying thousands of parameter combinations.

These are long-running debates about program optimization, going back literally decades. There's no one right answer. But, the comments above may give you some perspective and hopefully shine some light on what the considerations may be for you to decide for yourself where the best balance is.

janus
Posts: 835
Joined: 25 May 2009
Has thanked: 63 times
Been thanked: 104 times

Postby janus » 09 Apr 2010

All so true. May I add that as computers get faster and faster, the optimizations of today become redundant tomorrow.

duration
Posts: 179
Joined: 20 Dec 2005
Been thanked: 1 time

Postby duration » 11 Apr 2010

Dear Janus,

How can you spot functions using simple or series storage?

Many thanks,

User avatar
geizer
Posts: 375
Joined: 16 Jun 2008
Has thanked: 40 times
Been thanked: 38 times

Postby geizer » 12 Apr 2010

How can you spot functions using simple or series storage?
A nice article on the subject.
Attachments
EL - Simple and Series Functions in EL.pdf
originally downloaded from: http://www.jurikres.com/
(116.89 KiB) Downloaded 147 times

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Postby TJ » 12 Apr 2010

How can you spot functions using simple or series storage?
A nice article on the subject.
Good stuff.

Thanks for posting.

janus
Posts: 835
Joined: 25 May 2009
Has thanked: 63 times
Been thanked: 104 times

Postby janus » 13 Apr 2010



Return to “MultiCharts”