Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <unordered_map>
- #include <string>
- #include <chrono>
- constexpr bool ADD_BENCHMARK_TO_LOG_FILE = true;
- std::string GetMessageContent()
- {
- if(auto file = std::ifstream("message.txt", std::ios::ate))
- {
- size_t size = file.tellg();
- file.seekg(0);
- std::string str(size, ' ');
- file.read(str.data(), size);
- return str;
- }
- else
- {
- std::cout << "Failed to get the message content.";
- std::exit(0);
- }
- }
- int main()
- {
- const std::unordered_map<char, std::string> map
- {
- {'\\', "\\\\"}, {'`', "\\`"},
- {'*', "\\*"}, {'_', "\\_"}
- };
- const std::string content = GetMessageContent();
- auto t1 = std::chrono::high_resolution_clock::now();
- std::string result;
- result.reserve(content.size() * 1.5);
- for(const char c : content)
- {
- auto it = map.find(c);
- if(it != map.end())
- result += it->second;
- else
- result += c;
- }
- auto t2 = std::chrono::high_resolution_clock::now();
- auto time = std::chrono::duration_cast<std::chrono::nanoseconds>(t2 - t1).count();
- std::cout << "It took " << time << " nanoseconds";
- if constexpr(ADD_BENCHMARK_TO_LOG_FILE)
- {
- std::ofstream("log.txt", std::ios::app) << "C++: " << time << " nanoseconds.\n";
- }
- std::cin.get();
- std::cout << "See message content? Y or N\n";
- char c = std::cin.get();
- if(toupper(c) == 'Y')
- {
- std::cout << result << '\n';
- }
- }
Add Comment
Please, Sign In to add comment