Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

loops, function design and efficiency: two questions

I have two related questions one general and one specific to the project I'm working on.

  1. Generally if I have a loop with a lot of iterations (millions) with some parts of code being executed under certain conditions, is it better (more efficient) to have one loop with multiple conditional statements or multiple loops without them. Eg.

example 1:

while (something())
{
    // some common code
    if (condition_a)
        // some code
    if (condition_b)
        // some code
    // some more common code
}

example 2:

if (condition_a && condition_b)
{
    while (something())
    {
        // some common and specific code
    }
}
else if (condition_a)
    while (something()) //..
else if (condition_b)
    // Another loop
else //...

It seems like example 2 would result in more efficient code at the cost of redundancy because conditions are checked only once rather than million of times. If the common code is huge though or there are many possible conditions this seems extremely redundant.

  1. Now to my specific problem. I have a function which reads points from a file and inserts them into a data structure. It looks something like this:

    while (reader->read_point) { // do some stuff // insert point }

The problem is that there are several functions for reading a point which should be used based on criteria provided by the user. For example read_point_inside_circle(), read_point_inside_rectangle() etc.

Ideally I would like to use a function pointer to decide on the correct function beforehand, however I don't think it's possible since reader is an instance of a Reader class (if it is possible somehow that would solve all my problems).

In this situation is it better if I have multiple loops which differ only by the condition or should I use multiple if statements to avoid redundant code, eg.

for(;;)
{
    if (read_every_point)
        if(!reader->read_point())
            break;
    else if (read_inside_circle)
        if(!reader->read_inside_circle())
            break;
    else if // ...
}
like image 303
jaho Avatar asked Jun 13 '12 16:06

jaho


2 Answers

To answer your specific question: the time it takes to read a file is going to overwhelm the time spent in the if/else's. Write whatever is more readable. That's usually more efficient and don't optimize it until you've proven it's a bottleneck.

To answer your general question: depends on many things and modern compilers are very good at doing things efficiently independent of your intuition. So it's a theoretical discussion until you have working code on a specific compiler and architecture.

like image 115
djechlin Avatar answered Oct 23 '22 13:10

djechlin


Ideally I would like to use a function pointer to decide on the correct function beforehand, however I don't think it's possible since reader is an instance of a Reader class (if it is possible somehow that would solve all my problems).

You can use member function pointers for that, assuming all your read functions have the same signature:

typedef void (Reader::*read_fun_t)();
read_fun_t read_fun = &Reader::read_point;
// or
read_fun_t read_fun = &Reader::read_inside_circle;

...

(reader->*read_fun)();

or if you don't feel confortable with them, just create your own free functions that wrap the method calls:

void read_point( Reader* reader ){ reader->read_point(); }
void read_inside_circle( Reader* reader ){ reader->read_inside_circle(); }

and use regular function pointers instead.

like image 38
K-ballo Avatar answered Oct 23 '22 12:10

K-ballo