Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma hdrstop
- #pragma argsused
- #include <stdio.h>
- #include <iostream>
- #include <System.SyncObjs.hpp>
- #include <System.Diagnostics.hpp>
- #include <System.Threading.hpp>
- int tot = 0;
- //---------------------------------
- //void proc1(int i) {
- // if (i % 2) TInterlocked::Increment(tot);
- //}
- //---------------------------------------------------------------------------
- void parallel() {
- int max = 50000000;
- tot = 0;
- TStopwatch s = TStopwatch::Create();
- s.Start();
- //TParallel::For(1,max,proc1);
- TParallel::For(1, max,
- TProc1<int>( [&] (int i) {
- if (i % 2) TInterlocked::Increment(tot);
- } )
- );
- s.Stop();
- std::cout << s.ElapsedMilliseconds << " " << tot << "\n\n";
- }
- //---------------------------------------------------------------------------
- void linear() {
- int max = 50000000;
- tot = 0;
- TStopwatch s = TStopwatch::Create();
- s.Start();
- for (int i=0; i<max; i++) if (i % 2) tot++;
- s.Stop();
- std::cout << s.ElapsedMilliseconds << " " << tot << "\n\n";
- }
- //---------------------------------------------------------------------------
- void parallel2() {
- int max = 50000000;
- const int n = 500;
- int tot2[n] {0};
- TStopwatch s = TStopwatch::Create();
- s.Start();
- TParallel::For(1, max,
- TProc1<int>( [&] (int i) {
- if (i % 2) TInterlocked::Increment(tot2[i%n]);
- } )
- );
- s.Stop();
- std::cout << s.ElapsedMilliseconds << " ";
- for (int j=1; j < n; j+=2) {
- std::cout << tot2[j] << " ";
- }
- std::cout << "\n\n";
- }
- //---------------------------------------------------------------------------
- int main()
- {
- tot = 0;
- parallel();
- tot = 0;
- linear();
- tot = 0;
- parallel2();
- int x; std::cin >> x;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement