dorian3d / DBoW2

Enhanced hierarchical bag-of-word library for C++
Other
858 stars 367 forks source link

fix load too slow bug #68

Open IronSublimate opened 2 years ago

IronSublimate commented 2 years ago

In OpenCV, cv::FileNode::operator and cv::FileNodeIterator::operator+=(int) has O(n) time complexity. So I changed fn[i] to iterator.The total time complexity is changed from O(n^2) to O(n)
The OpenCV FileNode code can find here. In OpenCV4, the FileNode::operator calls FileNodeIterator::operator+=(int):

FileNode FileNode::operator[](int i) const
{
    if(!fs)
        return FileNode();

    CV_Assert( isSeq() );

    int sz = (int)size();
    CV_Assert( 0 <= i && i < sz );

    FileNodeIterator it = begin();
    it += i; //Here OpenCV use operator+=

    return *it;
}

but the FileNodeIterator::operator+=(int) is not O(1) .It uses FOR to get i.

FileNodeIterator& FileNodeIterator::operator += (int _ofs)
{
    CV_Assert( _ofs >= 0 );
    for( ; _ofs > 0; _ofs-- )
        this->operator ++();
    return *this;
}
llschloesser commented 1 year ago

@IronSublimate and @yhabib29 This same fix can be applied to improve database loading as well, yeah?