Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication1.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <math.h>
- #include <cstdlib>
- #include <iostream>
- #include <thread>
- void Task(double &partial, int terms)
- {
- int terms_t = terms + 250000;
- for (int i = terms; i < terms_t; i++)
- {
- partial += pow(-1.0,i)/(2.0 * i + 1);
- }
- }
- void lebizn(int terms)
- {
- double partial = 0;
- for (int i = 0; i < terms; i++)
- {
- partial += pow(-1.0,i)/(2.0 * i + 1);
- }
- printf("%1.10lf \n", 4*partial);
- }
- void withTimer(int terms)
- {
- auto start = std::chrono::high_resolution_clock::now();
- lebizn(terms);
- auto end = std::chrono::high_resolution_clock::now();
- std::chrono::duration<double, std::milli> elapsed = end-start;
- std::cout << "Waited Synch " << elapsed.count() << " ms\n";
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- double partial;
- double partial1 = 0;
- double partial2 = 0;
- double partial3 = 0;
- double partial4 = 0;
- std::thread t1(Task,std::ref(partial1), 0);
- std::thread t2(Task,std::ref(partial2), 2500000);
- std::thread t3(Task,std::ref(partial3), 5000000);
- std::thread t4(Task,std::ref(partial4), 7500000);
- auto start = std::chrono::high_resolution_clock::now();
- t1.join();
- t2.join();
- t3.join();
- t4.join();
- partial = partial1 + partial2 + partial3 + partial4;
- double newPi = 4 * partial;
- printf("%1.10lf \n", newPi);
- auto end = std::chrono::high_resolution_clock::now();
- std::chrono::duration<double, std::milli> elapsed = end-start;
- std::cout << "Waited Async " << elapsed.count() << " ms\n";
- std::cout << "Sync " <<std::endl;
- withTimer(10000000);
- system("Pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement