Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. // ConsoleApplication1.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <math.h>
  6. #include <cstdlib>
  7. #include <iostream>
  8. #include <thread>
  9.  
  10. void Task(double &partial, int terms)
  11. {
  12. int terms_t = terms + 250000;
  13. for (int i = terms; i < terms_t; i++)
  14. {
  15. partial += pow(-1.0,i)/(2.0 * i + 1);
  16. }
  17. }
  18. void lebizn(int terms)
  19. {
  20. double partial = 0;
  21. for (int i = 0; i < terms; i++)
  22. {
  23. partial += pow(-1.0,i)/(2.0 * i + 1);
  24. }
  25. printf("%1.10lf \n", 4*partial);
  26. }
  27.  
  28. void withTimer(int terms)
  29. {
  30. auto start = std::chrono::high_resolution_clock::now();
  31. lebizn(terms);
  32. auto end = std::chrono::high_resolution_clock::now();
  33. std::chrono::duration<double, std::milli> elapsed = end-start;
  34. std::cout << "Waited Synch " << elapsed.count() << " ms\n";
  35. }
  36.  
  37. int _tmain(int argc, _TCHAR* argv[])
  38. {
  39. double partial;
  40. double partial1 = 0;
  41. double partial2 = 0;
  42. double partial3 = 0;
  43. double partial4 = 0;
  44. std::thread t1(Task,std::ref(partial1), 0);
  45. std::thread t2(Task,std::ref(partial2), 2500000);
  46. std::thread t3(Task,std::ref(partial3), 5000000);
  47. std::thread t4(Task,std::ref(partial4), 7500000);
  48. auto start = std::chrono::high_resolution_clock::now();
  49. t1.join();
  50. t2.join();
  51. t3.join();
  52. t4.join();
  53. partial = partial1 + partial2 + partial3 + partial4;
  54. double newPi = 4 * partial;
  55. printf("%1.10lf \n", newPi);
  56. auto end = std::chrono::high_resolution_clock::now();
  57. std::chrono::duration<double, std::milli> elapsed = end-start;
  58. std::cout << "Waited Async " << elapsed.count() << " ms\n";
  59.  
  60.  
  61. std::cout << "Sync " <<std::endl;
  62. withTimer(10000000);
  63. system("Pause");
  64. return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement