Skip to content

Instantly share code, notes, and snippets.

@sbrl
Created February 5, 2015 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbrl/b10bf5c765630562431f to your computer and use it in GitHub Desktop.
Save sbrl/b10bf5c765630562431f to your computer and use it in GitHub Desktop.
A simple test of setting via value and setting via reference.
using System;
using System.Diagnostics;
class ValueVsReference
{
public static void SetValue(int somenumber, int maxi)
{
for (int i = 0; i < maxi; i++)
{
somenumber = i;
}
}
public static void SetReference(ref int somenumber, int maxi)
{
for(int i = 0; i < maxi; i++)
{
somenumber = i;
}
}
public static void Main()
{
int iterations = 100000;
int a = 0;
Console.Write("Iterations: {0} ", iterations);
Console.Write("Value: ");
Stopwatch valuetime = new Stopwatch();
valuetime.Start();
SetValue(a, iterations);
valuetime.Stop();
Console.Write("{0} ", valuetime.Elapsed);
Console.Write("Reference: ");
Stopwatch referencetime = new Stopwatch();
referencetime.Start();
SetReference(ref a, iterations);
referencetime.Stop();
Console.WriteLine(referencetime.Elapsed);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment