Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <random>
- #include <ctime>
- #include <chrono>
- #include <string>
- #include <fstream>
- using namespace std::chrono;
- class LogDuration {
- public:
- explicit LogDuration(const std::string& msg = "")
- : message(msg + ": ")
- , start(steady_clock::now())
- {
- }
- ~LogDuration() {
- auto finish = steady_clock::now();
- auto dur = finish - start;
- std::cerr << message
- << duration_cast<milliseconds>(dur).count()
- << " ms" << std::endl;
- }
- private:
- std::string message;
- steady_clock::time_point start;
- };
- #define UNIQ_ID_IMPL(lineno) _a_local_var_##lineno
- #define UNIQ_ID(lineno) UNIQ_ID_IMPL(lineno)
- #define LOG_DURATION(message) \
- LogDuration UNIQ_ID(__LINE__){message};
- template<typename T>
- void print1(const std::vector<T> v)
- {
- std::ofstream file("text1.txt");
- for (const auto i : v)
- file << i;
- std::cout << std::endl;
- }
- template<typename T>
- void print2(const std::vector<T> v)
- {
- std::ofstream file("text2.txt");
- for (const auto& i : v)
- file << i;
- std::cout << std::endl;
- }
- template<typename T>
- void print3(const std::vector<T>& v)
- {
- std::ofstream file("text3.txt");
- for (const auto i : v)
- file << i;
- std::cout << std::endl;
- }
- template<typename T>
- void print4(const std::vector<T>& v)
- {
- std::ofstream file("text4.txt");
- for (const auto& i : v)
- file << i;
- std::cout << std::endl;
- }
- int main ()
- {
- std::mt19937 gen(time(0));
- std::uniform_int_distribution<> uid(0, 500);
- std::vector<std::string> v(100000);
- for (auto& str : v)
- {
- str.resize(uid(gen));
- for (auto& c : str)
- c = char(uid(gen));
- }
- LOG_DURATION("Output without &")
- {
- print1(v);
- }
- LOG_DURATION("Output with &i")
- {
- print2(v);
- }
- LOG_DURATION("Output with &v")
- {
- print3(v);
- }
- LOG_DURATION("Output with &v and &i")
- {
- print4(v);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement