find vs at
#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;
}
|
run
| edit
| history
| help
|
0
|
|
|