Astatic variable declared inside a method of a class is a static variable of the class itself.
class A {
void foo() {
staticint i;
i++;
Print(i);
}
public:
A(){foo();};
} o1, o2, o3;
voidOnStart() {
}
result 1
2
3
Not that special. I checked, it's the same in C++. And it seems to make sense. But I didn't know it personally. That's why by habit I often used static variables in class methods, making hard-to-see mistakes (when there are several class objects).
I think it's justified. But, yes, it would be interesting to measure it. Maybe access to a global variable takes longer than new memory allocation.
It will depend on many factors.
The stack is more likely to be in the processor's cache than .data and .bss segments, simply based on the fact that operations are constantly happening to the stack.
If the structure allocated on the stack is not large or only separate fields are used, then during optimisations the compiler does not allocate memory on the stack at all, but bypasses registers.
Hence the conclusion: don't use static instead of local storage in the above context, it won't be better, but it will almost certainly be worse.
PS. If you remove static in that example, you should also remove the initialisation of the structure, so as not to waste CPU time, it will be initialised in SymbolInfoTick.
The probability of finding the stack in the processor's cache is higher than that of .data and .bss segments, simply on the basis of the fact that operations are constantly performed on the stack.
If the structure allocated on the stack is not large or only separate fields are used, then during optimisations the compiler does not allocate memory on the stack at all, but makes do with registers.
Hence the conclusion: don't use static instead of local storage in the above context, it won't be better, but it will almost certainly be worse.
PS. If you remove static in that example, you should also remove the initialisation of the structure, so as not to waste CPU time, it will be initialised in SymbolInfoTick.
I quite admit it.
Still, we should measure it
But it will be difficult to make an objective measurement
The stack is more likely to be in the processor's cache than .data and .bss segments, simply based on the fact that operations are constantly happening to the stack.
If the structure allocated on the stack is not large or only separate fields are used, then during optimisations the compiler does not allocate memory on the stack at all, but bypasses registers.
Hence the conclusion: don't use static instead of local storage in the above context, it won't be better, but it will almost certainly be worse.
PS. If you remove static in that example, you should also remove the initialisation of the structure, so as not to waste CPU time, it will be initialised in SymbolInfoTick.
How do you know it will be initialized? Won't it just be filled with values? Or do you mean this with "initialized".
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
How about this?
Insert your variant here and compare.
Astatic variable declared inside a method of a class is a static variable of the class itself.
result
1
2
3
Not that special. I checked, it's the same in C++. And it seems to make sense. But I didn't know it personally. That's why by habit I often used static variables in class methods, making hard-to-see mistakes (when there are several class objects).
I use it for speed, so that I don't have to allocate memory on each call. I haven't measured it, though. Probably self-delusion.
I think it's justified. But, yes, it would be interesting to measure it. Maybe accessing a global variable takes longer than allocating new memory.
I think it's justified. But, yes, it would be interesting to measure it. Maybe access to a global variable takes longer than new memory allocation.
It will depend on many factors.
Hence the conclusion: don't use static instead of local storage in the above context, it won't be better, but it will almost certainly be worse.
PS. If you remove static in that example, you should also remove the initialisation of the structure, so as not to waste CPU time, it will be initialised in SymbolInfoTick.
It's going to depend on a lot of factors.
Hence the conclusion: don't use static instead of local storage in the above context, it won't be better, but it will almost certainly be worse.
PS. If you remove static in that example, you should also remove the initialisation of the structure, so as not to waste CPU time, it will be initialised in SymbolInfoTick.
I quite admit it.
It will depend on many factors.
Hence the conclusion: don't use static instead of local storage in the above context, it won't be better, but it will almost certainly be worse.
PS. If you remove static in that example, you should also remove the initialisation of the structure, so as not to waste CPU time, it will be initialised in SymbolInfoTick.
I tried it.
Result.
Frankly speaking, I don't understand why the second line gets zero when there is no initialisation.
I tried it.
Result.
Frankly speaking, I don't understand why the second line gets zero when there is no initialisation.