Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8086705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:32:12+00:00 2026-06-05T18:32:12+00:00

I have two related questions one general and one specific to the project I’m

  • 0

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 // ...
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T18:32:13+00:00Added an answer on June 5, 2026 at 6:32 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two questions about creating thread safe types in python, and one related
I have two questions related to the performance of Dozer using its Java API:
I have two related tables in my database: Page and Tag. One Page can
I have two tables related: DataEntered and Model DataEntered -currentModel Model One DataEntered can
I have two models related one-to-many: a Post and a Comment : class Post(models.Model):
I saw two related questions about this topic - one has an old link
I have two related questions about Web services: (1) I'm currently writing a set
I have two related questions. First, i have a some information such as: (732,
I have two questions related to the vector class of the standard library in
I working in a poll script, i have two tables {hs_questions_q}{hs_answers_ans} the first one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.