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

  • SEARCH
  • Home
  • 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 8772279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:59:53+00:00 2026-06-13T17:59:53+00:00

Is it possible to remove the branches in the following loop. All iterators are

  • 0

Is it possible to remove the branches in the following loop. All iterators are from the container type std::map<type_name, T>

  record_iterator beginIter = lastLookup_;                                                                                                                                                                                                                                                                                                                             
  record_iterator endIter = lastLookup_;                                                                                                                                                                                                                                                                                                                               
  ++endIter;                                                                                                                                                                                                                                                                                                                                                           
  for(;endIter != end(); ++beginIter, ++endIter){                                                                                                                                                                                                                                                                                                                      
    time_type now = beginIter->first;                                                                                                                                                                                                                                                                                                                                  
    if(ts == now){                                                                                                                                                                                                                                                                                                                                                     
      lastLookup_ = beginIter;                                                                                                                                                                                                                                                                                                                                         
      return beginIter;                                                                                                                                                                                                                                                                                                                                                
    }else if(ts > now && ts <= endIter->first){                                                                                                                                                                                                                                                                                                                        
      lastLookup_ = beginIter;                                                                                                                                                                                                                                                                                                                                         
      return endIter;
    }
  }

The problem that this algo is trying to solve is to optimize the forward lookup which location is assumed to be the same or (not too far ) forward of the last looked up location. Ideally, I kept an iterator of last looked up location, and move forward linearly. But this seems to have the same performance as,

  record_iterator it= sliceMap_.find(ts);                                                                                                                                                                                                                                                                                                                              
  if(it !=end()){                                                                                                                                                                                                                                                                                                                                                      
    return it;                                                                                                                                                                                                                                                                                                                                                         
  }else{                                                                                                                                                                                                                                                                                                                                                               
    return sliceMap_.upper_bound(ts);                                                                                                                                                                                                                                                                                                                                  
  }         

I feel that the problem is the branch, so it is possible to remove the branch in this code so I can profile the different in speed?

  • 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-13T17:59:54+00:00Added an answer on June 13, 2026 at 5:59 pm

    There are three big problems with the first approach:

    • Too many comparisons inside a loop.
    • Using iterators on a std::map involves using std::map<>::iterator::operator++(), which is not exactly fast. Look at the implementation starting at line 62: http://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-html-USERS-3.4/tree_8cc-source.html .
    • Using iterators on a std::map is a linear search. Searching on a map should be logarithmic.

    There’s also a problem with your second approach. You are searching twice.

    Why don’t you just use

    return sliceMap_.lower_bound(ts);
    

    This should do exactly what you want with one logarithmic search.

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

Sidebar

Related Questions

Possible Duplicate: remove_if equivalent for std::map I have a set of strings: set <wstring>
Possible Duplicate: remove duplicate from string in PHP Have a string like: $str =
Is it possible to remove a string key value from the registry on uninstall
Is it possible to remove individual files from the list of files to be
Is it possible to remove a single (SE_NO_SERIALVERSIONID) pattern from the SerializableIdiom detector in
Is it possible to config git repository not to fetch branches from certain namespaces
Possible Duplicate: Remove item from array if it exists in a 'disallowed words' array
Possible Duplicate: Remove border from IFrame I started on a website project some weeks
Possible Duplicate: remove application icon from the taskbar using .net with c# I am
Possible Duplicate: Remove from the array elements that are repeated. in Ruby I would

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.