// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; namespace E { public class GCTest { private const int MIN_LVL = 0; private const int MAX_LVL = 5; private const int MIN_CHLD = 2; private const int MAX_CHLD = 10; private const int NREPL = 100; private static int objcount; private static readonly Random rng = new Random(); private static int GetRanInt(int low, int high) { return rng.Next(low, high + 1); } private class AnyObject { private Object o; public AnyObject(int lvl, int siz) { if(lvl > 0) { AnyObject[] o2 = new AnyObject[GetRanInt(MIN_CHLD, MAX_CHLD)]; for(int i = 0; i < o2.Length; i++) { o2[i] = new AnyObject(lvl - 1, siz); } o = o2; } else { o = new byte[GetRanInt(0, siz)]; } objcount += 2; } public object Object { get { return o; } } } public static void Test(int siz) { objcount = 0; int topobj = 1000000000 / (int)Math.Pow((MAX_CHLD + MIN_CHLD)/2, MAX_LVL) / siz; DateTime t1 = DateTime.Now; AnyObject[] a = new AnyObject[topobj]; for(int i = 0; i < NREPL * a.Length; i++) { a[GetRanInt(0, a.Length-1)] = new AnyObject(GetRanInt(MIN_LVL, MAX_LVL), siz); } DateTime t2 = DateTime.Now; Console.WriteLine("Objects size 0-{0} bytes : {1} top objects {2:#0.0} million objects in {3:#0.0} seconds = {4:#0.00} million objects/second", siz, topobj, objcount/1000000.0, (t2-t1).TotalSeconds, objcount/(t2-t1).TotalSeconds/1000000.0); } public static void Main(string[] args) { Console.WriteLine(Environment.Version); Test(100); Test(10000); } } }