Created
February 5, 2015 09:59
-
-
Save sbrl/b10bf5c765630562431f to your computer and use it in GitHub Desktop.
A simple test of setting via value and setting via reference.
This file contains 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 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