In which way program performance is best?

 
//code 1
int bar = 10;
 double open   = iOpen(_Symbol, PERIOD_M1, bar);
   double close  = iClose(_Symbol, PERIOD_M1, bar);
   double high   = iHigh(_Symbol, PERIOD_M1, bar);
   double low    = iLow(_Symbol, PERIOD_M1, bar);

if(open>close)
// do something
//code 2


if(iOpen(_Symbol, PERIOD_M1, 10)>iClose(_Symbol, PERIOD_M1, 10))
// do something

I want to what is good practice to write code in these 2 styles with considering "program performance" only so program runs fast and smooth consuming less memory. 

 
Arpit T:

I want to what is good practice to write code in these 2 styles with considering "program performance" only so program runs fast and smooth consuming less memory. 

I suppose, please anyone who knows better correct me, since you are calling functions, following would be the result by the compilers optimizer.

Your second code listing will be converted into your first code listing. Reason is, the values can only be compared after the function was called. Therefore the return values of these functions need to be stored. Then a comparison is performed.

It might be, though this is only guessing, if you declare the variables in first listing as const, they might be held in CPU cache longer without committing back to memory.

EDIT:
Concerning the int bar variable, there is no difference.
 
Arpit TI want to what is good practice to write code in these 2 styles with considering "program performance" only so program runs fast and smooth consuming less memory. 

In terms of memory consumption the difference between the two examples is minimal. Both examples involve the same amount of variables (open and close in the first example, and no additional variables in the second example). The main difference lies in how the code is structured and the amount of lines used. The second example is more elegant and minimalistic (these are the 2 aspects that a good coder should always be looking for).

From the point of view of memory consumption the declaration of local variables in the first example may take a little more space because of the additional variables, but I insist, in this particular case in terms of efficiency the difference is negligible.

That is my opinion. 😅

 
Miguel Angel Vico Alba #:

In terms of memory consumption the difference between the two examples is minimal. Both examples involve the same amount of variables (open and close in the first example, and no additional variables in the second example). The main difference lies in how the code is structured and the amount of lines used. The second example is more elegant and minimalistic (these are the 2 aspects that a good coder should always be looking for).

From the point of view of memory consumption the declaration of local variables in the first example may take a little more space because of the additional variables, but I insist, in this particular case in terms of efficiency the difference is negligible.

That is my opinion. 😅


//code 1
string sym = _Symbol;
ENUM_TIMEFRAMES tf = PERIOD_M1;
int bar = 10;
 double open   = iOpen(sym,tf, bar);
   double close  = iClose(sym,tf, bar);
   double high   = iHigh(sym,tf, bar);
   double low    = iLow(sym,tf, bar);

void Function1() {
if(open>close)
// do something
}
void Function2() {
if(open>close)
// do something
}
.....


void Function100() {
if(open>close)
// do something
}
//code 2

void Function1() {
if(iOpen(_Symbol, PERIOD_M1, 10)>iClose(_Symbol, PERIOD_M1, 10)){
// do something
}
}
void Function2(){
if(iOpen(_Symbol, PERIOD_M1, 10)>iClose(_Symbol, PERIOD_M1, 10)){
// do something
}
}
.....

void Function100(){
if(iOpen(_Symbol, PERIOD_M1, 10)>iClose(_Symbol, PERIOD_M1, 10)){

// do something
}
}

my use case if i have hundreds of functions which are called on a program, So as per your feedback I believe minimalistic code (code2 ) is much better for performance rather than declaring variables? and should this practice be approached by a good programmer?

 
Arpit T #my use case if i have hundreds of functions which are called on a program, So as per your feedback I believe minimalistic code (code2 ) is much better for performance rather than declaring variables? and should this practice be approached by a good programmer?

More things should be taken into account, such as how many times it will be called (if on each tick, etc), or even if open and close are worth declaring them as in example 1 because they are needed for something later on. My opinion is based on those examples being all the code, but logically it won't be like that, so you should evaluate whether to do it one way or there is a plausible explanation to do it another way.

 
Miguel Angel Vico Alba #:
More things should be taken into account, such as how many times it will be called (if on eac

My all 100+ functions are only called once at Onchartevent on press of key event but sometime they do not load all at once, in multiple press of keys it loads that is why I am asking this for performance

 
Arpit T #My all 100+ functions are only called once at Onchartevent on press of key event but sometime they do not load all at once, in multiple press of keys it loads that is why I am asking this for performance

I still think there won't be much of a difference. Nowadays, any computer can handle that (both example 1 and 2) in a matter of milliseconds. If it were complex calculations, I would understand your concern for efficiency, but although it's always good to care about that and, as I mentioned before, be elegant and minimalist, in this particular case, it really doesn't matter how you do it.

However, to the question of which is more efficient without having all the code in front of me and knowing exactly everything it does, the answer is simple; the second example wins.

 
Miguel Angel Vico Alba #:

I still think there won't be much of a difference. Nowadays, any computer can handle that (both example 1 and 2) in a matter of milliseconds. If it were complex calculations, I would understand your concern for efficiency, but although it's always good to care about that and, as I mentioned before, be elegant and minimalist, in this particular case, it really doesn't matter how you do it.

However, to the question of which is more efficient without having all the code in front of me and knowing exactly everything it does, the answer is simple; the second example wins.

Thank you.

 
Miguel Angel Vico Alba #:

I still think there won't be much of a difference. Nowadays, any computer can handle that (both example 1 and 2) in a matter of milliseconds. If it were complex calculations, I would understand your concern for efficiency, but although it's always good to care about that and, as I mentioned before, be elegant and minimalist, in this particular case, it really doesn't matter how you do it.

However, to the question of which is more efficient without having all the code in front of me and knowing exactly everything it does, the answer is simple; the second example wins.

What??

No. - Calling a function (in this case two functions, iClose and iOpen) multiple times is slower than calling a function once and storing the result.

How do you come up with such an idea?
 
I would better make sure the program runs as intended first before any optimizations (the testing stage). After that I can use the profiler to check bottlenecks in the code (the macro-optimization stage). 

If you find this piece of code you posted is slow in profiler, then you can micro-optimize its execution time (via GetTickCount or GetMicrosecondsCount). This is the micro-optimization stage.

But, frankly I do not see any difference between these 2 code snippets. 

Choosing one of them, is a matter of your coding style. Personally, I prefer clear code, not giving any consideration for how many  lines it takes. 
 
Dominik Egert #:
What??

No. - Calling a function (in this case two functions, iClose and iOpen) multiple times is slower than calling a function once and storing the result.

How do you come up with such an idea?

Relax...

The OP is not asking about what is faster; they are asking about what consumes more memory. Therefore, storing results is ruled out.

Here, the focus is on the OP, not engaging in absurd battles over why you said this or that. You gave your opinion, and I gave mine.

End of story.

Reason: