All Languages ​​- Procedural Efficiency

Let's consider them with parameters

if(successful)
{
    if(condition)
    {
       //do something
    }
    if(condition)
    {
       //do something
    }
    ...
}

or

if(successful)&&(condition)
{
   //do something
}
if(successful)&&(condition)
{
   //do something
}
...

Imagine 100 claims.

Is there a difference in performance?

Thanks in advance.

+3
source share
4 answers

It all depends on how expensive the expression is successful.

It should also be noted that the two versions are not semantically equivalent, since evaluating an if-expression can have side effects 1 .

If you really run into performance issues, then measure, don't guess . Measurement will be the only way to see what the actual performance is.

1 , , -:

CreateProcess , true:

bool CreateProcess(string filename, out handle) { ... } 

if (CreateProcess("program.exe", out handle))
{   
    if (someCondition)
    {
         handle.SomeMethod(...);
    }
    if (someOtherCondition)
    {
         handle.SomeOtherMethod(...);
    }
}

:

if (CreateProcess("program.exe", out handle) && someCondition)
{
    handle.SomeMethod(...);
}
if (CreateProcess("program.exe", out handle) && someOtherCondition)
{
    handle.SomeOtherMethod(...);
}
+8

. - .

  • -, , . , , , . (: , , .)
  • , , . , , , .

. . . - .

+10

O (1). //.

+1

, JMcO

, . (, , , () , / )

, , .

  • && & (, lhs ) &&
  • , ?
  • ? , , ( , ).
  • What types of optimization are performed by the compiler? Can some of them radically change binary files (for example, to skip a condition if it can be found as constant or, at least, is known earlier?)

the list may be oblong, but my point is the event, although it may be interesting to think about what can affect performance without knowing much about the build and runtime environment, which in principle is impossible to predict performance

0
source

Source: https://habr.com/ru/post/1760743/


All Articles