Advertisement
Guest User

test.cs

a guest
Aug 31st, 2015
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.25 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Threading;
  4.  
  5. public class Program
  6. {
  7.     const int NUM_REFS = 200;
  8.     const int NUM_ALIVES = 200000;
  9.  
  10.     static Stopwatch st = new Stopwatch ();
  11.     static object[] objects = new object [NUM_REFS];
  12.     static WeakReference[] weak_refs = new WeakReference [NUM_REFS];
  13.  
  14.     public static void Time1 ()
  15.     {
  16.         int i, alive = 0;
  17.         for (i = 0; i < NUM_REFS * NUM_ALIVES * 10; i++) {
  18.             if ((alive & 0xff) == 0)
  19.                 alive++;
  20.         }
  21.         Console.WriteLine ("{0}", alive);
  22.     }
  23.  
  24.     public static int Time2 ()
  25.     {
  26.         int alive = 0;
  27.         for (int i = 0; i < NUM_REFS; i++) {
  28.             for (int j = 0; j < NUM_ALIVES; j++) {
  29.                 if (weak_refs [i].IsAlive)
  30.                     alive++;
  31.             }
  32.         }
  33.         return alive;
  34.     }
  35.  
  36.     public static void Init ()
  37.     {
  38.         st.Reset ();
  39.         st.Start ();
  40.         for (int i = 0; i < NUM_REFS; i++) {
  41.             objects [i] = i;
  42.             weak_refs [i] = new WeakReference (objects [i]);
  43.         }
  44.  
  45.         Time1 ();
  46.         st.Stop ();
  47.         Console.WriteLine ("Init {0} ms", st.ElapsedMilliseconds);
  48.  
  49.     }
  50.  
  51.     public static void Run ()
  52.     {
  53.         int alive;
  54.  
  55.         st.Reset ();
  56.         st.Start ();
  57.         alive = Time2 ();
  58.         st.Stop ();
  59.         Console.WriteLine ("Bench {0} ms, alive {1}", st.ElapsedMilliseconds, alive);
  60.     }
  61.  
  62.     public static void Main ()
  63.     {
  64.         Init ();
  65.         Run ();
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement