Advertisement
pmcgee

C++Builder Test parallel increments

Dec 15th, 2019
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.94 KB | None | 0 0
  1. #pragma hdrstop
  2. #pragma argsused
  3.  
  4. #include <stdio.h>
  5. #include <iostream>
  6. #include <System.SyncObjs.hpp>
  7. #include <System.Diagnostics.hpp>
  8. #include <System.Threading.hpp>
  9.  
  10. int tot = 0;
  11.  
  12. //---------------------------------
  13.  
  14. //void proc1(int i)   {
  15. //      if (i % 2)   TInterlocked::Increment(tot);
  16. //}
  17.  
  18. //---------------------------------------------------------------------------
  19. void parallel()   {
  20.     int        max = 50000000;
  21.                tot = 0;
  22.     TStopwatch s   = TStopwatch::Create();
  23.     s.Start();
  24.  
  25.     //TParallel::For(1,max,proc1);
  26.  
  27.     TParallel::For(1, max,
  28.                       TProc1<int>( [&] (int i)  {
  29.                          if (i % 2)   TInterlocked::Increment(tot);
  30.                       } )
  31.                   );
  32.  
  33.     s.Stop();
  34.     std::cout << s.ElapsedMilliseconds << "   " << tot << "\n\n";
  35. }
  36.  
  37. //---------------------------------------------------------------------------
  38. void linear()   {
  39.     int        max = 50000000;
  40.                tot = 0;
  41.     TStopwatch s   = TStopwatch::Create();
  42.     s.Start();
  43.     for (int i=0; i<max; i++)   if (i % 2)   tot++;
  44.  
  45.     s.Stop();
  46.     std::cout << s.ElapsedMilliseconds << "   " << tot << "\n\n";
  47. }
  48.  
  49. //---------------------------------------------------------------------------
  50. void parallel2()   {
  51.     int        max     = 50000000;
  52.     const int  n       = 500;
  53.     int        tot2[n]   {0};
  54.  
  55.     TStopwatch s   = TStopwatch::Create();
  56.     s.Start();
  57.  
  58.     TParallel::For(1, max,
  59.                       TProc1<int>( [&] (int i)  {
  60.                          if (i % 2)   TInterlocked::Increment(tot2[i%n]);
  61.                       } )
  62.                   );
  63.  
  64.     s.Stop();
  65.     std::cout << s.ElapsedMilliseconds << "   ";
  66.     for (int j=1; j < n; j+=2) {
  67.          std::cout << tot2[j] << "  ";
  68.     }
  69.     std::cout << "\n\n";
  70. }
  71.  
  72.  
  73. //---------------------------------------------------------------------------
  74.  
  75.  
  76. int main()
  77. {
  78.     tot = 0;
  79.     parallel();
  80.  
  81.     tot = 0;
  82.     linear();
  83.  
  84.     tot = 0;
  85.     parallel2();
  86.  
  87.     int x; std::cin >> x;
  88.     return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement