I have two related questions: one general and one specific for the project I'm working on.
- As a rule, if I have a loop with a large number of iterations (millions) with some parts of the code executed under certain conditions, it is better (more efficient) to have one loop with several conditional statements or several loops without them. For instance.
example 1:
while (something())
{
if (condition_a)
if (condition_b)
}
Example 2:
if (condition_a && condition_b)
{
while (something())
{
}
}
else if (condition_a)
while (something())
else if (condition_b)
else
Example 2 seems to lead to more efficient code due to redundancy, since conditions are checked only once, not a million times. If the overall code is huge, or there are many possible conditions, this seems extremely redundant.
, , , . read_point_inside_circle(), read_point_inside_rectangle() ..
, , , , Reader ( , , ).
, , if, , .
for(;;)
{
if (read_every_point)
if(!reader->read_point())
break;
else if (read_inside_circle)
if(!reader->read_inside_circle())
break;
else if
}