Run Code  | API  | Code Wall  | Misc  | Feedback  | Login  | Theme  | Privacy  | Patreon 

find vs at

//Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x64


/*
    iterator find(const key_type& _Keyval)
        {   // find an element in mutable sequence that matches _Keyval
        iterator _Where = lower_bound(_Keyval);
        return (_Where == end()
            || _DEBUG_LT_PRED(this->_Getcomp(),
                _Keyval, this->_Key(_Where._Mynode()))
                    ? end() : _Where);
        }
        

    mapped_type& at(const key_type& _Keyval)
        {   // find element matching _Keyval
        iterator _Where = _Mybase::lower_bound(_Keyval);
        if (_Where == _Mybase::end()
            || _Mybase::_Getcomp()(_Keyval, _Mybase::_Key(_Where._Mynode())))
            _Xout_of_range("invalid map<K, T> key");
        return (_Where->second);
        }

*/        

#include <vector>
#include <iostream>
#include <chrono>
#include <map>

void f(const std::map<int, int>& v)
{
    const auto& it = v.find(0);
}

void g(const std::map<int, int>& v)
try
{
    v.at(0);
}
catch (...)
{
    return;
}

void test1(const std::map<int, int>& a)
{
    {
        auto start = std::chrono::system_clock::now();
        for (int i = 0; i < 100000; i++)
        {
            f(a);
        }
        auto seconds = std::chrono::duration<double>(std::chrono::system_clock::now() - start);
        std::cout << seconds.count() << std::endl;
    }

    {
        auto start = std::chrono::system_clock::now();
        for (int i = 0; i < 100000; i++)
        {
            g(a);
        }
        auto seconds = std::chrono::duration<double>(std::chrono::system_clock::now() - start);
        std::cout << seconds.count() << std::endl;
    }
}

int main()
{
    std::map<int, int> a;
    test1(a);

    a[0] = 1;
    test1(a);



    std::cout << "a";
    return 0;
}

/*
Coll
*/
 run  | edit  | history  | help 0