Memory allocation

 

Hello,

I am just curious what is the best way of declaring variables in terms of resources. In small ea's it means almost nothing, but if you go for big one and then you want to run optimization with it, here where the major resource problem comes in.

So what is faster for the system?

1. Declare all variables as globals and assign values on the go?

2. Declare local variables on every tick with directly assigned values?

Same question for arrays, what is the best way to work with arrays in terms of speed?

 
  1. Don't "declare all variables as global" That is bad programming.
  2. "Declare local variables with directly assigned values." Just not every tick. Don't do per tick what you can do once per bar. If you're waiting for price to reach a trigger, ignore all ticks until you reach it (or a new bar starts and you recalculate.)
  3.  Arrays are ~10x slower than variables.  Cache them in variables where possible.
       for(;;){
    
          if(!(value < arr[--iNext]))  break;
          arr[iEmpty] = arr[iNext];
          iEmpty = iNext;
       }
       for(;;){
          T  next  = arr[--iNext];
          if(!(value < next))  break;
          arr[iEmpty] = next;
          iEmpty = iNext;
       }
 
whroeder1:
  1. Don't "declare all variables as global" That is bad programming.
  2. "Declare local variables with directly assigned values." Just not every tick. Don't do per tick what you can do once per bar. If you're waiting for price to reach a trigger, ignore all ticks until you reach it (or a new bar starts and you recalculate.)
  3.  Arrays are ~10x slower than variables.  Cache them in variables where possible.
Question was not about good/bad programming. Question was about speed. Which way is faster, local or global declaration. Personally i use local, but i have doubts about speed, as every time variable is declared the memory must be allocated for it, unlike global which is declared only once.
 
Georgiy Liashchenko: Question was not about good/bad programming. Question was about speed. Which way is faster, local or global declaration. Personally i use local, but i have doubts about speed, as every time variable is declared the memory must be allocated for it, unlike global which is declared only once.

Per Tick is important. Caching array accesses in loops is important.

The rest is irrelevant. You are in the noise. Meaning less. Who cares if it finishes a microsecond faster? If you do, measure it! Do not assume.