Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <map>
- #include <string>
- #include <chrono>
- #include <random>
- #include <iostream>
- __attribute__((noinline))
- void test_slow(std::map<std::string, std::string> data); // copy
- __attribute__((noinline))
- void test_fast(const std::map<std::string, std::string>& data); // const reference
- int main()
- {
- // Just create a huge set of dummy data
- std::map<std::string, std::string> some_data;
- std::cout << "Initializing...\n";
- std::random_device e;
- std::mt19937_64 g(e());
- std::uniform_int_distribution d(' ','~');
- while(some_data.size() < 1000000)
- {
- std::string s1; s1.reserve(64); for(std::size_t n=0; n<64; ++n) s1 += d(g);
- std::string s2; s2.reserve(64); for(std::size_t n=0; n<64; ++n) s2 += d(g);
- some_data.emplace(std::move(s1), std::move(s2));
- }
- auto begin = std::chrono::system_clock::now();
- for(std::size_t n=0; n<32; ++n) test_slow(some_data);
- auto end = std::chrono::system_clock::now();
- std::cout << std::fixed; std::cout.precision(12);
- std::cout << "Slow method: " << std::chrono::duration<double>(end - begin).count() << " s\n";
- auto begin2 = std::chrono::system_clock::now();
- for(std::size_t n=0; n<32; ++n) test_fast(some_data);
- auto end2 = std::chrono::system_clock::now();
- std::cout << "Fast method: " << std::chrono::duration<double>(end2 - begin2).count() << " s\n";
- }
- void test_slow(std::map<std::string, std::string> data) // copy
- {
- }
- void test_fast(const std::map<std::string, std::string>& data) // const reference
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement