Advertisement
Guest User

Untitled

a guest
Aug 31st, 2017
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. game.cpp
  2. --------
  3.  
  4. #include <iostream>
  5. #include <cmath>
  6. #include <chrono>
  7.  
  8. template<typename TimeT = std::chrono::microseconds>
  9. struct measure
  10. {
  11. template<typename F, typename ...Args>
  12. static typename TimeT::rep execution(F&& func, Args&&... args)
  13. {
  14. auto start = std::chrono::steady_clock::now();
  15. std::forward<decltype(func)>(func)(std::forward<Args>(args)...);
  16. auto duration = std::chrono::duration_cast< TimeT>
  17. (std::chrono::steady_clock::now() - start);
  18. return duration.count();
  19. }
  20. };
  21.  
  22. using namespace std;
  23.  
  24. class Player {
  25. public:
  26. float x;
  27. float y;
  28. float dx;
  29. float dy;
  30. };
  31.  
  32. const int PLAYERS = 100000000;
  33. const int N = 1;
  34. Player pls[PLAYERS];
  35.  
  36. void seq(Player pls[], int n) {
  37. for (int i=0;i<n;i++) {
  38. for (int j=0;j<N;j++) {
  39. pls[i].x += std::sin(2 * M_PI * i * j * pls[i].dx);
  40. pls[i].y += std::sin(2 * M_PI * i * j * pls[i].dy);
  41. }
  42. }
  43. }
  44. void par(Player pls[], int n) {
  45. #pragma omp parallel for
  46. for (int i=0;i<n;i++) {
  47. for (int j=0;j<N;j++) {
  48. pls[i].x += std::sin(2 * M_PI * i * j * pls[i].dx);
  49. pls[i].y += std::sin(2 * M_PI * i * j * pls[i].dy);
  50. }
  51. }
  52. }
  53.  
  54. int main() {
  55. cout << "players, seq, par" << endl;
  56. for (int i=1000;i<PLAYERS;i<<=1) {
  57. cout << i << ", " << measure<>::execution(seq, pls, i) << ", " << measure<>::execution(par, pls, i) << endl;
  58. }
  59. }
  60.  
  61. game.r
  62. ------
  63. library(ggplot2)
  64. g = read.csv("game.csv")
  65. ggplot(g) + geom_line(stat="identity", aes(players, par, color="par"))+ geom_line(stat="identity", aes(players, seq, color="seq")) + scale_y_continuous(name='runtime', trans='log2') + scale_x_continuous(name='players',trans='log2')
  66. ggsave("game.png")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement