Last active
March 22, 2017 09:53
-
-
Save utilForever/248232a7d7005196e0f4394536de5b45 to your computer and use it in GitHub Desktop.
Store std::string in std::map w/ and w/o lowercase process, and find arbitrary string using various methods (Insensitive case)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <algorithm> | |
#include <cctype> | |
#include <chrono> | |
#include <fstream> | |
#include <iostream> | |
#include <map> | |
#include <random> | |
#include <string> | |
#include <vector> | |
int main() | |
{ | |
const int NUM_ITERATIONS = 100'000'00; | |
// Prepare data to find arbitrary string | |
std::ifstream input("test_case.txt"); | |
std::vector<std::string> vTestData; | |
while (!input.eof()) | |
{ | |
std::string str; | |
input >> str; | |
vTestData.emplace_back(str); | |
} | |
input.close(); | |
// Prepare random | |
auto seed = std::chrono::high_resolution_clock::now().time_since_epoch().count(); | |
std::mt19937 mt_rand(seed); | |
// Case 1: Store std::string in std::map with lowercase process | |
input.open("sample.txt"); | |
std::map<std::string, int> map1; | |
std::cout << "Store std::string in std::map with lowercase process\n"; | |
auto start = std::chrono::high_resolution_clock::now(); | |
while (!input.eof()) | |
{ | |
std::string str; | |
input >> str; | |
std::transform(str.begin(), str.end(), str.begin(), ::tolower); | |
map1.insert(std::make_pair(str, 1)); | |
} | |
auto end = std::chrono::high_resolution_clock::now(); | |
auto time = std::chrono::duration_cast<std::chrono::milliseconds>(end - start); | |
std::cout << time.count() << "\n"; | |
input.close(); | |
// Case 1-1: Find arbitrary string using for-statement | |
// NOTE: auto is much slower than auto& because of copy cost | |
std::cout << "Find arbitrary string using for-statement\n"; | |
start = std::chrono::high_resolution_clock::now(); | |
for (int i = 0; i < NUM_ITERATIONS; ++i) | |
{ | |
std::string str = vTestData[mt_rand() % vTestData.size()]; | |
for (auto& data : map1) | |
{ | |
if (stricmp(str.c_str(), data.first.c_str()) == 0) | |
{ | |
// Found it! | |
break; | |
} | |
} | |
} | |
end = std::chrono::high_resolution_clock::now(); | |
time = std::chrono::duration_cast<std::chrono::milliseconds>(end - start); | |
std::cout << time.count() << "\n"; | |
// Case 1-2: Find arbitrary string using std::map::find | |
std::cout << "Find arbitrary string using std::map::find\n"; | |
start = std::chrono::high_resolution_clock::now(); | |
for (int i = 0; i < NUM_ITERATIONS; ++i) | |
{ | |
std::string str = vTestData[mt_rand() % vTestData.size()]; | |
std::transform(str.begin(), str.end(), str.begin(), ::tolower); | |
auto search = map1.find(str); | |
if (search != map1.end()) | |
{ | |
// Found it! | |
} | |
else | |
{ | |
// Not found | |
} | |
} | |
end = std::chrono::high_resolution_clock::now(); | |
time = std::chrono::duration_cast<std::chrono::milliseconds>(end - start); | |
std::cout << time.count() << "\n"; | |
// Case 2: Store std::string in std::map without lowercase process | |
input.open("samples.txt"); | |
std::map<std::string, int> map2; | |
std::cout << "Store std::string in std::map without lowercase process\n"; | |
start = std::chrono::high_resolution_clock::now(); | |
while (!input.eof()) | |
{ | |
std::string str; | |
input >> str; | |
map2.insert(std::make_pair(str, 1)); | |
} | |
end = std::chrono::high_resolution_clock::now(); | |
time = std::chrono::duration_cast<std::chrono::milliseconds>(end - start); | |
std::cout << time.count() << "\n"; | |
input.close(); | |
// Case 2-1: Find arbitrary string using for-statement | |
// NOTE: auto is much slower than auto& because of copy cost | |
std::cout << "Find arbitrary string using for-statement\n"; | |
start = std::chrono::high_resolution_clock::now(); | |
for (int i = 0; i < NUM_ITERATIONS; ++i) | |
{ | |
std::string str = vTestData[mt_rand() % vTestData.size()]; | |
for (auto& data : map2) | |
{ | |
if (stricmp(str.c_str(), data.first.c_str()) == 0) | |
{ | |
// Found it! | |
break; | |
} | |
} | |
} | |
end = std::chrono::high_resolution_clock::now(); | |
time = std::chrono::duration_cast<std::chrono::milliseconds>(end - start); | |
std::cout << time.count() << "\n"; | |
// Case 2-2: Find arbitrary string using std::find_if | |
std::cout << "Find arbitrary string using std::find_if\n"; | |
start = std::chrono::high_resolution_clock::now(); | |
for (int i = 0; i < NUM_ITERATIONS; ++i) | |
{ | |
std::string str = vTestData[mt_rand() % vTestData.size()]; | |
std::find_if(map2.begin(), map2.end(), [&str](const std::pair<std::string, int>& v) | |
{ | |
return stricmp(v.first.c_str(), str.c_str()) == 0; | |
}); | |
} | |
end = std::chrono::high_resolution_clock::now(); | |
time = std::chrono::duration_cast<std::chrono::milliseconds>(end - start); | |
std::cout << time.count() << "\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment