Skip to content

Instantly share code, notes, and snippets.

@svick
Created December 3, 2016 18:58
Show Gist options
  • Select an option

  • Save svick/4e59c8c08f5f94a8f9faae78e1396ef6 to your computer and use it in GitHub Desktop.

Select an option

Save svick/4e59c8c08f5f94a8f9faae78e1396ef6 to your computer and use it in GitHub Desktop.
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