question about function calls  [SOLVED]

Questions about MultiCharts and user contributed studies.
dennisc
Posts: 32
Joined: 25 Feb 2014
Has thanked: 16 times
Been thanked: 9 times

question about function calls

Postby dennisc » 11 Sep 2014

First of all, I want to thank everyone who's helped me begin to understand how to use Power Language.

Will using function calls in an indicator's coding generally require more processing resources than if I included the function coding within the indicator coding?

Is it faster to use the function calls?

Just wondering...

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: question about function calls

Postby Henry MultiСharts » 11 Sep 2014

Hello dennisc,

Function calls insignificantly increase the calculation time but make the code more readable.
Each line of code (which is adding additional operations) increases the calculation time, that is expected.

dennisc
Posts: 32
Joined: 25 Feb 2014
Has thanked: 16 times
Been thanked: 9 times

Re: question about function calls

Postby dennisc » 11 Sep 2014

Hello dennisc,

Function calls insignificantly increase the calculation time but make the code more readable.
Each line of code (which is adding additional operations) increases the calculation time, that is expected.
So let's say I create an indicator by coding it in two ways: with and without function calls. Let's assume that each way has been coded as efficiently as possible and that both ways produce the same result.

Which way is better in terms of computer resource usage?

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

Re: question about function calls  [SOLVED]

Postby TJ » 11 Sep 2014

Hello dennisc,

Function calls insignificantly increase the calculation time but make the code more readable.
Each line of code (which is adding additional operations) increases the calculation time, that is expected.
So let's say I create an indicator by coding it in two ways: with and without function calls. Let's assume that each way has been coded as efficiently as possible and that both ways produce the same result.

Which way is better in terms of computer resource usage?
There is a way to find out -- test it yourself.

1. Go look for an indicator by JoshM (sorry I don't have the name nor the link), it calculates the times it takes to process a block of code. (it is accurate down to the milliseconds.)

2. Write 2 indicators using the methods you described above, then add a loop to cycle the code 1,000,000 times... at the end of the run, you will be able to see which method is better.


ps. this is assuming you have sufficient RAM to run a normal MultiCharts operation.

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: question about function calls

Postby JoshM » 13 Sep 2014

1. Go look for an indicator by JoshM (sorry I don't have the name nor the link), it calculates the times it takes to process a block of code. (it is accurate down to the milliseconds.)
That topic is here. It also includes an example from MC support that uses a DLL to measure the milliseconds.
So let's say I create an indicator by coding it in two ways: with and without function calls. Let's assume that each way has been coded as efficiently as possible and that both ways produce the same result.

Which way is better in terms of computer resource usage?
Don't optimize for a problem you don't have.

I don't think it's wrong to write better and/or more efficient code (in general), but I don't see you saying that your current code runs very slow or has high resource usage. So don't micro-optimise your code if you don't need to.

* * *

Here's what others say:

This is an interesting discussion, especially the following quote:
Optimization is "evil" if it causes:

less clear code
significantly more code
less secure code
wasted programmer time.
I'd also like to add this quote from Jeff Atwood, where he talks about optimising string concatenation (bold from original source):
Yes, you should avoid the obvious beginner mistakes of string concatenation, the stuff every programmer learns their first year on the job. But after that, you should be more worried about the maintainability and readability of your code than its performance. And that is perhaps the most tragic thing about letting yourself get sucked into micro-optimization theater -- it distracts you from your real goal: writing better code.
Source.

* * *

In the context of MultiCharts, it's just good practice to use functions when you have repeating code because:
- This reduces the amount of code in your indicator or strategy,
- Makes the code easier to manage: just change the function as opposed to changing an x number of script that have similar code,
- Reduces programming errors: once your function works correctly, you can just call it instead of having to copy/paste or retype the code,
- Since a function is nicely contained, if you experience an odd result, you know where to look and debug.


Return to “MultiCharts”