Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- namespace ArrayAllocationTest
- {
- class Program
- {
- const int N = 5000;
- const int SmallSize = 10;
- const int MediumSize = 10000;
- const int LargeSize = 10000000;
- static void Main(string[] args)
- {
- int[] array = null;
- Console.WriteLine("Testing small ({0}) array allocation...", SmallSize);
- TimeSpan smallArrayTime = TimeAllocations(ref array, SmallSize, N);
- Console.WriteLine("Testing medium ({0}) array allocations...", MediumSize);
- TimeSpan mediumArrayTime = TimeAllocations(ref array, MediumSize, N);
- Console.WriteLine("Testing large ({0}) array allocation...", LargeSize);
- TimeSpan largeArrayTime = TimeAllocations(ref array, LargeSize, N);
- Console.WriteLine("Small: {0} ms", smallArrayTime.TotalMilliseconds);
- Console.WriteLine("Medium: {0} ms", mediumArrayTime.TotalMilliseconds);
- Console.WriteLine("Large: {0} ms", largeArrayTime.TotalMilliseconds);
- Console.ReadLine();
- }
- static TimeSpan TimeAllocations<T>(ref T[] array, int size, int count)
- {
- long ticks = 0L;
- for (int i = 0; i < count; ++i)
- {
- ticks += TimeAllocation(ref array, size).Ticks;
- }
- return TimeSpan.FromTicks(ticks);
- }
- static TimeSpan TimeAllocation<T>(ref T[] array, int size)
- {
- GC.Collect();
- Stopwatch stopwatch = Stopwatch.StartNew();
- array = new T[size];
- stopwatch.Stop();
- return stopwatch.Elapsed;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement