Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- game.cpp
- --------
- #include <iostream>
- #include <cmath>
- #include <chrono>
- template<typename TimeT = std::chrono::microseconds>
- struct measure
- {
- template<typename F, typename ...Args>
- static typename TimeT::rep execution(F&& func, Args&&... args)
- {
- auto start = std::chrono::steady_clock::now();
- std::forward<decltype(func)>(func)(std::forward<Args>(args)...);
- auto duration = std::chrono::duration_cast< TimeT>
- (std::chrono::steady_clock::now() - start);
- return duration.count();
- }
- };
- using namespace std;
- class Player {
- public:
- float x;
- float y;
- float dx;
- float dy;
- };
- const int PLAYERS = 100000000;
- const int N = 1;
- Player pls[PLAYERS];
- void seq(Player pls[], int n) {
- for (int i=0;i<n;i++) {
- for (int j=0;j<N;j++) {
- pls[i].x += std::sin(2 * M_PI * i * j * pls[i].dx);
- pls[i].y += std::sin(2 * M_PI * i * j * pls[i].dy);
- }
- }
- }
- void par(Player pls[], int n) {
- #pragma omp parallel for
- for (int i=0;i<n;i++) {
- for (int j=0;j<N;j++) {
- pls[i].x += std::sin(2 * M_PI * i * j * pls[i].dx);
- pls[i].y += std::sin(2 * M_PI * i * j * pls[i].dy);
- }
- }
- }
- int main() {
- cout << "players, seq, par" << endl;
- for (int i=1000;i<PLAYERS;i<<=1) {
- cout << i << ", " << measure<>::execution(seq, pls, i) << ", " << measure<>::execution(par, pls, i) << endl;
- }
- }
- game.r
- ------
- library(ggplot2)
- g = read.csv("game.csv")
- 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')
- ggsave("game.png")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement