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...
question about function calls [SOLVED]
- Henry MultiСharts
- Posts: 9165
- Joined: 25 Aug 2011
- Has thanked: 1264 times
- Been thanked: 2957 times
Re: question about function calls
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.
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.
Re: question about function calls
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.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.
Which way is better in terms of computer resource usage?
- TJ
- Posts: 7743
- Joined: 29 Aug 2006
- Location: Global Citizen
- Has thanked: 1033 times
- Been thanked: 2223 times
Re: question about function calls [SOLVED]
There is a way to find out -- test it yourself.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.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.
Which way is better in terms of computer resource usage?
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.
- JoshM
- Posts: 2195
- Joined: 20 May 2011
- Location: The Netherlands
- Has thanked: 1544 times
- Been thanked: 1565 times
- Contact:
Re: question about function calls
That topic is here. It also includes an example from MC support that uses a DLL to measure the milliseconds.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.)
Don't optimize for a problem you don't have.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?
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:
I'd also like to add this quote from Jeff Atwood, where he talks about optimising string concatenation (bold from original source):Optimization is "evil" if it causes:
less clear code
significantly more code
less secure code
wasted programmer time.
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.
* * *
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.