Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using System.Threading;
- public class Program
- {
- const int NUM_REFS = 200;
- const int NUM_ALIVES = 200000;
- static Stopwatch st = new Stopwatch ();
- static object[] objects = new object [NUM_REFS];
- static WeakReference[] weak_refs = new WeakReference [NUM_REFS];
- public static void Time1 ()
- {
- int i, alive = 0;
- for (i = 0; i < NUM_REFS * NUM_ALIVES * 10; i++) {
- if ((alive & 0xff) == 0)
- alive++;
- }
- Console.WriteLine ("{0}", alive);
- }
- public static int Time2 ()
- {
- int alive = 0;
- for (int i = 0; i < NUM_REFS; i++) {
- for (int j = 0; j < NUM_ALIVES; j++) {
- if (weak_refs [i].IsAlive)
- alive++;
- }
- }
- return alive;
- }
- public static void Init ()
- {
- st.Reset ();
- st.Start ();
- for (int i = 0; i < NUM_REFS; i++) {
- objects [i] = i;
- weak_refs [i] = new WeakReference (objects [i]);
- }
- Time1 ();
- st.Stop ();
- Console.WriteLine ("Init {0} ms", st.ElapsedMilliseconds);
- }
- public static void Run ()
- {
- int alive;
- st.Reset ();
- st.Start ();
- alive = Time2 ();
- st.Stop ();
- Console.WriteLine ("Bench {0} ms, alive {1}", st.ElapsedMilliseconds, alive);
- }
- public static void Main ()
- {
- Init ();
- Run ();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement