Advertisement
Guest User

Baseline performance test

a guest
Apr 11th, 2013
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.95 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using System.IO;
  4.  
  5. class App
  6. {
  7.   static ReaderWriterLockSlim rw = new ReaderWriterLockSlim();
  8.   static int reads = 0;
  9.   static int writes = 0;
  10.  
  11.   static void Main()
  12.   {
  13.     for (int i = 0; i < 3; ++i)
  14.     {
  15.       new Thread(Reader).Start();
  16.       new Thread(Writer).Start();
  17.     }
  18.  
  19.     while(true)
  20.     {
  21.       Thread.Sleep(1000);
  22.       int r = reads;
  23.       int w = writes;
  24.       Console.WriteLine("{0}\t{1}\r", r, w);
  25.     }
  26.   }
  27.  
  28.   static void Reader()
  29.   {
  30.     while(true)
  31.     {
  32.       double x = 0;
  33.       rw.EnterReadLock();
  34.       Interlocked.Increment(ref reads);
  35.       for(int i = 1; i < 10; ++i) x += Math.Sqrt(i);
  36.       rw.ExitReadLock();
  37.     }
  38.   }
  39.  
  40.   static void Writer()
  41.   {
  42.     while(true)
  43.     {
  44.       double x = 0;
  45.       rw.EnterWriteLock();
  46.       var w = Interlocked.Increment(ref writes);
  47.       for(int i = 1; i < 10; ++i) x += Math.Sqrt(i);
  48.       rw.ExitWriteLock();
  49.     }
  50.   }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement