Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- struct MyStruct
- {
- public int Id;
- public string Name;
- public DateTime DateOfBirth;
- public string Comment;
- }
- class MyClass
- {
- public int Id { get; set; }
- public string Name { get; set; }
- public DateTime DateOfBirth { get; set; }
- public string Comment { get; set; }
- }
- static class Program
- {
- static void Main()
- {
- var dob = DateTime.Today;
- const int SIZE = 1 * 1000 * 1000;
- const int REPEATS = 100 * 1000 * 1000;
- Stopwatch watch = Stopwatch.StartNew();
- var s = new MyStruct[SIZE];
- for (int i = 0; i < REPEATS; i++)
- {
- s[i % SIZE] = new MyStruct
- {
- Comment = "abc", DateOfBirth = dob,
- Id = 123, Name = "def"
- };
- }
- watch.Stop();
- Console.WriteLine("struct/field: "
- + watch.ElapsedMilliseconds + "ms");
- watch = Stopwatch.StartNew();
- var c = new MyClass[SIZE];
- for (int i = 0; i < REPEATS; i++)
- {
- c[i % SIZE] = new MyClass
- {
- Comment = "abc", DateOfBirth = dob,
- Id = 123, Name = "def"
- };
- }
- watch.Stop();
- Console.WriteLine("class/property: "
- + watch.ElapsedMilliseconds + "ms");
- Console.ReadLine();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement