Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Main()
- {
- var iterations = 10000000;
- var foos = new Foo[iterations];
- var repeats = 10;
- var factories = new Func<Foo>[]
- {
- () => new Foo(),
- () => (Foo)Activator.CreateInstance(typeof(Foo)),
- () => FooBar<Foo>.Create(),
- };
- var timestamps = new double[factories.Length + 1];
- var durations = new double[factories.Length];
- foreach (var factory in factories)
- {
- factory();
- }
- var sw = Stopwatch.StartNew();
- GC.Collect();
- GC.WaitForPendingFinalizers();
- for (int repeat = 0; repeat < repeats; repeat++)
- {
- var cc0 = GC.CollectionCount(0);
- for (int f = 0; f < factories.Length; f++)
- {
- timestamps[f] = sw.Elapsed.TotalMilliseconds;
- for (int i = 0; i < iterations; i++)
- {
- foos[i] = factories[f]();
- //factories[f]();
- }
- }
- timestamps[factories.Length] = sw.Elapsed.TotalMilliseconds;
- for (int i = 0; i < 3; i++)
- {
- durations[i] += timestamps[i + 1] - timestamps[i];
- }
- Console.WriteLine(GC.CollectionCount(0) - cc0);
- }
- for (int i = 0; i < 3; i++)
- {
- Console.WriteLine(durations[i] / repeats);
- }
- }
- public class Foo { }
- class FooBar<T> where T : new()
- {
- public static T Create()
- {
- return new T();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement