Advertisement
Guest User

Untitled

a guest
May 24th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.71 KB | None | 0 0
  1. template< typename T, typename Fun, typename ...Args>
  2. auto measure(Fun&& fun, Args&& ...args)
  3. {
  4.     std::chrono::high_resolution_clock timer{};
  5.     auto before = timer.now();
  6.     fun(args...);
  7.     auto after = timer.now();
  8.     return std::chrono::duration_cast<T>((after - before));
  9. }
  10.  
  11. // prosta struktura pomocnicza, pseudo tabelka
  12. class SimpleTable
  13. {
  14. public:
  15.  
  16.     std::vector<std::string> tuples;
  17.  
  18.  
  19.     void addTuple(std::string tuple)
  20.     {
  21.         tuples.emplace_back(std::forward<std::string>(tuple));
  22.     }
  23.     void print()
  24.     {
  25.         for (auto& tuple : tuples)
  26.         {
  27.             std::cout << tuple << '\n';
  28.         }
  29.     }
  30.     void toFile(std::string filename)
  31.     {
  32.         std::ofstream ofs{ "results\\" + filename };
  33.         if (!ofs.is_open()) throw 15;
  34.         for (auto& tuple : tuples)
  35.         {
  36.             ofs << tuple << '\n';
  37.         }
  38.     }
  39. };
  40.  
  41. // rowniez wykorzystane do zad1_6
  42. void zad1_3()
  43. {
  44.     SimpleTable sTable;
  45.  
  46.     sTable.print();
  47.  
  48.     sTable.addTuple("n;edgesSeq;edgesConc;");
  49.     for (int i = 50; i <= 1000; i += 50)
  50.     {
  51.         std::ostringstream oss{};
  52.  
  53.         auto graph = blankGraph(i);
  54.         gnp(graph.size(), 0.25f, graph);
  55.         // ilość powtórzeń obliczeń do uśredniania
  56.         const int STEPS = 10;
  57.         long long seq = 0;
  58.         long long conc = 0;
  59.         for (int j = 0; j < STEPS; ++j)
  60.         {
  61.             seq += measure<std::chrono::nanoseconds>(countGraphEdgesSeq, graph.size(), graph).count();
  62.             conc += measure<std::chrono::nanoseconds>(countGraphEdgesConc, graph.size(), graph).count();
  63.         }
  64.         oss
  65.             << i << ';'
  66.             // uśrednienie wyników
  67.             << seq/STEPS << ';'
  68.             << conc/STEPS << ';';
  69.         // dodanie do prostej tabelki
  70.         sTable.addTuple(oss.str());
  71.     }
  72.     // wyświetlenie w konsoli
  73.     sTable.print();
  74.     // zapisanie tabelki do pliku do obróbki w zewnętrznych programach
  75.     sTable.toFile("zad1_3.txt");
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement