Created
November 7, 2019 23:47
-
-
Save salarcode/892e69ad507fe7e7ef3b7df19ea87dd5 to your computer and use it in GitHub Desktop.
pre-increment vs. post-increment
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
/// <summary> | |
/// pre-increment vs. post-increment | |
/// </summary> | |
/// <url> | |
/// https://medium.com/better-programming/stop-using-i-in-your-loops-1f906520d548 | |
/// </url> | |
public class LoopIncrements | |
{ | |
public int Repeat = 1_000_000; | |
[Benchmark] | |
[MethodImpl(MethodImplOptions.NoInlining)] | |
public void PostIncrement() | |
{ | |
int value = 0, i; | |
for (i = 0; i < Repeat; i++) | |
{ | |
value = i; | |
} | |
if (value == 0) | |
{ | |
Console.WriteLine("This line will never run"); | |
} | |
} | |
[Benchmark] | |
[MethodImpl(MethodImplOptions.NoInlining)] | |
public void PreIncrement() | |
{ | |
int value = 0, i; | |
for (i = 0; i < Repeat; ++i) | |
{ | |
value = i; | |
} | |
if (value == 0) | |
{ | |
Console.WriteLine("This line will never run"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment