Created
December 3, 2016 18:58
-
-
Save svick/4e59c8c08f5f94a8f9faae78e1396ef6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using BenchmarkDotNet.Running; | |
| using BenchmarkDotNet.Attributes; | |
| namespace Benchmark | |
| { | |
| public static class Program | |
| { | |
| public static void Main() | |
| { | |
| BenchmarkRunner.Run<RefProperty>(); | |
| } | |
| } | |
| public class RefProperty | |
| { | |
| private const int n = 1000000; | |
| [Benchmark] | |
| public static void Modern() | |
| { | |
| var person = new ModernPerson(); | |
| for (int i = 0; i < n; i++) | |
| { | |
| person.Age++; | |
| } | |
| } | |
| [Benchmark] | |
| public static void OldSchool() | |
| { | |
| var person = new OldSchoolPerson(); | |
| for (int i = 0; i < n; i++) | |
| { | |
| person.Age++; | |
| } | |
| } | |
| } | |
| public class OldSchoolPerson | |
| { | |
| public string Name { get; set; } | |
| public int Age { get; set; } | |
| } | |
| public class ModernPerson | |
| { | |
| private string _name; | |
| private int _age; | |
| public ref string Name => ref _name; | |
| public ref int Age => ref _age; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment