Advertisement
Guest User

Untitled

a guest
Feb 15th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <random>
  4. #include <ctime>
  5. #include <chrono>
  6. #include <string>
  7. #include <fstream>
  8.  
  9. using namespace std::chrono;
  10.  
  11.  
  12. class LogDuration {
  13. public:
  14. explicit LogDuration(const std::string& msg = "")
  15. : message(msg + ": ")
  16. , start(steady_clock::now())
  17. {
  18. }
  19.  
  20. ~LogDuration() {
  21. auto finish = steady_clock::now();
  22. auto dur = finish - start;
  23. std::cerr << message
  24. << duration_cast<milliseconds>(dur).count()
  25. << " ms" << std::endl;
  26. }
  27. private:
  28. std::string message;
  29. steady_clock::time_point start;
  30. };
  31.  
  32. #define UNIQ_ID_IMPL(lineno) _a_local_var_##lineno
  33. #define UNIQ_ID(lineno) UNIQ_ID_IMPL(lineno)
  34.  
  35. #define LOG_DURATION(message) \
  36. LogDuration UNIQ_ID(__LINE__){message};
  37.  
  38. template<typename T>
  39. void print1(const std::vector<T> v)
  40. {
  41. std::ofstream file("text1.txt");
  42. for (const auto i : v)
  43. file << i;
  44. std::cout << std::endl;
  45. }
  46.  
  47. template<typename T>
  48. void print2(const std::vector<T> v)
  49. {
  50. std::ofstream file("text2.txt");
  51. for (const auto& i : v)
  52. file << i;
  53. std::cout << std::endl;
  54. }
  55.  
  56. template<typename T>
  57. void print3(const std::vector<T>& v)
  58. {
  59. std::ofstream file("text3.txt");
  60. for (const auto i : v)
  61. file << i;
  62. std::cout << std::endl;
  63. }
  64.  
  65. template<typename T>
  66. void print4(const std::vector<T>& v)
  67. {
  68. std::ofstream file("text4.txt");
  69. for (const auto& i : v)
  70. file << i;
  71. std::cout << std::endl;
  72. }
  73.  
  74.  
  75. int main ()
  76. {
  77. std::mt19937 gen(time(0));
  78. std::uniform_int_distribution<> uid(0, 500);
  79.  
  80. std::vector<std::string> v(100000);
  81.  
  82. for (auto& str : v)
  83. {
  84. str.resize(uid(gen));
  85. for (auto& c : str)
  86. c = char(uid(gen));
  87. }
  88.  
  89.  
  90. LOG_DURATION("Output without &")
  91. {
  92. print1(v);
  93. }
  94.  
  95. LOG_DURATION("Output with &i")
  96. {
  97. print2(v);
  98. }
  99.  
  100. LOG_DURATION("Output with &v")
  101. {
  102. print3(v);
  103. }
  104.  
  105. LOG_DURATION("Output with &v and &i")
  106. {
  107. print4(v);
  108. }
  109.  
  110. return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement