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?
There are three big problems with the first approach:
std::mapinvolves usingstd::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 .std::mapis 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
This should do exactly what you want with one logarithmic search.