Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- namespace DeleteThis
- {
- internal class Program
- {
- private static Stopwatch watcher = new Stopwatch();
- private static void Main()
- {
- var objCount = 30;
- var loopsCount = 5;
- for (int i = 0; i < loopsCount; i++)
- RunClasses(objCount);
- for (int i = 0; i < loopsCount; i++)
- RunStructs(objCount);
- Console.WriteLine("Done");
- Console.ReadKey();
- }
- private static void RunClasses(int objCount)
- {
- watcher.Restart();
- var list = new class1[objCount * 1000 * 1000];
- for (int i = 0; i < list.Length; i++)
- list[i] = new class1(i, i * 2);
- watcher.Stop();
- Console.WriteLine("Class run elapsed: " + watcher.Elapsed);
- GC.Collect();
- }
- private static void RunStructs(int objCount)
- {
- watcher.Restart();
- var list = new struct1[objCount * 1000 * 1000];
- for (int i = 0; i < list.Length; i++)
- list[i] = new struct1(i, i * 2);
- watcher.Stop();
- Console.WriteLine("Struct run elapsed: " + watcher.Elapsed);
- GC.Collect();
- }
- private class class1
- {
- public class1(int x, int y)
- {
- X = x;
- Y = y;
- }
- public int X { get; set; }
- public int Y { get; set; }
- }
- private struct struct1
- {
- public struct1(int x, int y)
- {
- X = x;
- Y = y;
- }
- public int X { get; set; }
- public int Y { get; set; }
- }
- }
- }
- ================================== Run =================================
- Class run elapsed: 00:00:04.5652342
- Class run elapsed: 00:00:04.7994383
- Class run elapsed: 00:00:05.3744633
- Class run elapsed: 00:00:05.3462232
- Class run elapsed: 00:00:05.6153301
- Struct run elapsed: 00:00:00.2896144
- Struct run elapsed: 00:00:00.2870866
- Struct run elapsed: 00:00:00.3003215
- Struct run elapsed: 00:00:00.2613126
- Struct run elapsed: 00:00:00.2651348
- Done
- =================================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement