Declaring variables behind the loop or inside the loop? - page 3

 
Aleksandr Matveev:

I have a suspicion that the author of this comment doesn't know at all how the processor, memory and compiler work...

I think the author was so inconsiderate that he didn't understand the question.

What do memory and CPU have to do with it? The question was about optimization in MQL5.

P.S. Read at your leisure entertaining little book " Technique of Attention Development, O.A. Andreev, 2007".

Техника развития внимания, Андреев О.А., 2007
Техника развития внимания, Андреев О.А., 2007
  • 2018.04.27
  • nashol.com
Смотреть, читать и скачать бесплатно pdf, djvu и купить бумажную и электронную книгу по лучшей цене со скидкой: Техника развития внимания, Андреев О.А., 2007. Настоящая книга написана по Программе интеллектуального и духовного развития личности Школы Олега Андреева. Она посвящена тренировке внимания. Можно использовать книгу как часть Программы или как самостоятельное пособие по технике развития внимания. Настоящее издание является более полным, чем предыдущие, включает новые материалы для самостоятельной работы. Информация изложена в виде восьми уроков. Каждый содержит теоретический раздел, упражнения и инструкции к
 
pivalexander:

What do memory and CPU have to do with it? The question was about optimization in MQL5.

So that code, in your opinion, uses neither memory nor CPU? And I'm answering to the statement about compilers, "You'll have to try hard to beat the optimizations of modern compilers. "

 
Aleksandr Matveev:

I have a suspicion that the author of this statement doesn't know at all how the processor, memory and compiler work... I'm willing to bet that any of your code can be accelerated at least ten times, or even hundreds of times.

So first try to prove it with respect to the code in question. Moreover, if you believe the above results cited by Igor, it's vice versa: the declaration inside the loop works a bit faster.

So all this dancing around and shuffling around trying to outsmart the compiler costs more in the end. It makes it harder for the compiler to optimize the code. When a variable is declared outside a block, it is considered external andpreserves its state between iterations. This means that the compiler has limited options for optimizing it. Clean code is always optimized better than "dirty" code (which has side effects), if you are familiar with the functional programming paradigm.

 
Alexey Navoykov:

So first try to prove it with respect to the code in question. Moreover, if you believe the above results cited by Igor, it's vice versa: the declaration inside the loop works a bit faster.

So all this dancing around and shuffling around trying to outsmart the compiler costs more in the end. It makes it harder for the compiler to optimize the code. When a variable is declared outside a block it is considered external and preserves its state between iterations, which means that the compiler has limited abilities to optimize it. Clean code is always better optimized than "dirty" code (which has some side effects) if you know the functional programming paradigm.

Read at your leisure my book, I think you will learn a lot about compilers.

 
Alexey Navoykov:

So first try to prove it with respect to the code in question. Moreover, if you believe the above results cited by Igor, it's vice versa: the declaration inside the loop works a bit faster.

So all this dancing around and shuffling around trying to outsmart the compiler costs more in the end. It makes it harder for the compiler to optimize the code. When a variable is declared outside of a block, it is considered to be an external variable that retains its state between iterations. This means that the compiler has limited options for its optimization. Clean code is always optimized better than "dirty" code (which has side effects), if you are familiar with the functional programming paradigm.

My test shows the opposite, code with a variable declaration behind the loop works faster

https://www.mql5.com/ru/forum/320307/page2#comment_12886814

Объявление переменных за циклом или внутри цикла?
Объявление переменных за циклом или внутри цикла?
  • 2019.08.18
  • www.mql5.com
или Есть ли разница? с учетом всех оптимизаций компилятора mql5...
 
pivalexander:

Run many times, result is stable, code with declaration of variable after the loop, works faster

It's strange, but earlier the person had the opposite result
 
pivalexander:

My test shows the opposite, code with a variable declaration behind the loop is faster

The result you get inside the loop is not used in any way. The results of such empty tests cannot be trusted. The compiler could have simply cut out the whole loop body in one case.
 
Alexey Navoykov:
It's strange, but a little earlier the man had the opposite results

You should learn the basics first and then prove your point. You would understand the result without any tests, if you had read at least one book on CPU and memory operation. I offered you the most ingenious one, if you want to advance a little in programming, you will read it.

 
Alexey Navoykov:
Strange, but earlier the man had the opposite results

I've already said that such a speed test on an artificial example is not indicative at all. Isn't my research under the debugger enough? Don't I have any credibility? All ignored, it's up to you, I'm out.

 
Alexey Navoykov:

Moreover, if you believe Igor's results above, it's the opposite.

I don't believe myself, I always double-check, I don't think you need to raise my self-esteem... There've been different cases in history, I'm going to find a Napoleon, and let's go! ))))


Alexey Navoykov:
It's strange, but earlier the man got the opposite results.

Why did I add srand(GetTickCount()) to my test? ?

;)