Skip to content

Instantly share code, notes, and snippets.

@salarcode
Created November 7, 2019 23:47
Show Gist options
  • Save salarcode/892e69ad507fe7e7ef3b7df19ea87dd5 to your computer and use it in GitHub Desktop.
Save salarcode/892e69ad507fe7e7ef3b7df19ea87dd5 to your computer and use it in GitHub Desktop.
pre-increment vs. post-increment
/// <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