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

 
Alexey Volchanskiy:

Just met a bunch of people who think that the compiler zeroes in on local variables just like it does on global variables.

And the compiler doesn't give out varnings.

It does, string and print are no indicator of working with variables

int a;
int b;

void OnStart()
  {
   b=a+100;
   int c;
   int d=c+5;
   for(int i=0;i<10;i++)
     {
      int e;
      int f=i+e;
     }
  }

'tst.mq5' tst.mq5 1 1

possible use of uninitialized variable 'c' tst.mq5 16 10

possible use of uninitialized variable 'e' tst.mq5 20 17

code generated 1 1

0 error(s), 2 warning(s), 526 msec elapsed 1 3

 
//c++
 int main() {
        int count= (int)10 e6;
        {
                auto t1 = chrono::high_resolution_clock::now();
                int sum=0;
                for (int i=0; i<count; i++) {
                        string st;
                        st = "12345678qwertyuioasdfgh";
                        sum += st[i%23];
                }
                auto t2 = chrono::high_resolution_clock::now();
                auto duration = chrono::duration_cast<chrono::milliseconds>( t2 - t1 ).count();
                cout << "time = " << duration << endl;
                cout << "sum = " << sum << endl;
        }

  
        {
                auto t1 = chrono::high_resolution_clock::now();
                int sum=0;
                string st = "";
                for (int i=0; i<count; i++) {
                        st = "12345678qwertyuioasdfgh";
                        sum += st[i%23];
                }
                auto t2 = chrono::high_resolution_clock::now();
                auto duration = chrono::duration_cast<chrono::milliseconds>( t2 - t1 ).count();
                cout << "time = " << duration << endl;
                cout << "sum = " << sum << endl;
        }
}

time = 1018
sum = 894782460
time = 371
sum = 894782460

I don't know why, but μl overtakes strongly (and the more intricate rand() variants).

And for me it's obvious - take it out of the loop.

 
Alexey Volchanskiy:

Just met a bunch of people who think that the compiler zeroes in on local variables just like it does on global variables.

And the compiler doesn't give out any warrants.

Or .... I don't know. I get it to swear at every chance I get.... ))))))

 
Сергей Таболин:

Or .... I don't know. I swear like that every chance I get.... ))))))

You have an obscene version of the compiler)
 
Vict:

time = 1018
sum = 894782460
time = 371
sum = 894782460

I don't know why, but μl is way ahead of the curve (and the more intricate rand() variants).

And for me it's obvious - take it out of the loop.

I'm no guru, but here, in my humble opinion, declaring variables in a loop is a HUGE deal (and it's no longer a HUGE deal) !!!

One thing is a function, even a local piece of code, but loops....

I may be wrong ))))))))

 
Dmitriy Skub:
You have an obscene version of the compiler)

And I'm happy about that ))))))))))) Although sometimes .... I'd rather he didn't say anything )))))))))))))))))))))))))))))))

 

If this is a debate - I want to ask the Guru.

Which is "correct" (optimal and readable):

for (int i=0;i<Bars();i++)
{
// code
}

Or

int i,MaxBars=Bars()-1;
for (i=MaxBars;i>=0;i--;)
{
// code
}

If the order of i is indifferent?

 
Сергей Таболин:

I'm no guru, but here, in my humble opinion, declaring variables in a loop is a HUGE deal (and it's no longer a HUGE deal) !!!

One thing is a function, even a local piece of code, but loops....

I may be wrong ))))))))

Not possible, but definitely wrong. Just one example: You loop through the open positions. You get a position ticket, use it to get other position properties. There are two variants, either in each function of getting position property insert PositionGetTicket(i) or write it in a variable once and use it. But after exiting the loop nobody needs this ticket... Why declare this variable in the body of the OnTick() function or even more interesting at global level?
 
Mikhail Dovbakh:

If this is a debate - I want to ask the Guru.

Which is "correct" (optimal and readable):

Or

If the order of i is indifferent?

I think the first option, because there is no extra variables, they not only use memory, but for me, clutter the code - read more, but it depends on the functions you call imho, if the function is rarely used, then sometimes it will be more readable, if you declare a variable with a name in the sense of the task.... all in all, this is a creative problem ))))

but generally, don't listen to them, write as you like - use the language for your own convenience

SZZ: look at examples from mikrosoft, the style mostly - declarations at once when using variables, basically get local scope, but without fanaticism ))

void SimpleImage::CalculateDrawingRect()
{
    // Load our bitmap if necessary
    if (nullptr == m_bitmap)
    {
        if (FAILED(LoadBitmapFromShellItem()))
        {
            return;
        }
    }

    // Calculate bitmap rectangle
    float  boundingWidth = m_boundingRect.right - m_boundingRect.left;
    float b oundingHeight = m_boundingRect.bottom - m_boundingRect.top;

    float w idth = Direct2DUtility::GetRectWidth(m_clipRect);
    float h eight = Direct2DUtility::GetRectHeight(m_clipRect);

    if (!m_isHorizontal)
    {
        // Swap width and height to calculate boundaries
        float widthTemp = width;
        width = height;
        height = widthTemp;
    }

    if (width > boundingWidth)
    {
        // Width is larger than bounding box. Scale width to fit
        float scale = boundingWidth / width;
        width *= scale;
        height *= scale;
    }
https://github.com/microsoft
Microsoft
Microsoft
  • github.com
This repo is the official home of .NET on GitHub. It's a great starting point to find many .NET OSS projects from Microsoft and the community, including many that are part of the .NET Foundation.
 
Mikhail Dovbakh:

If this is a debate - I want to ask the Guru.

Which is "correct" (optimal and readable):

Or

If the order of i is indifferent?

If indifferent then.

int i = Bars();

while(i-- > 0) 

{

// code

}

.