Skip to content

Instantly share code, notes, and snippets.

@YuvalItzchakov
Created December 7, 2015 18:35
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 YuvalItzchakov/707275b73a54cb476609 to your computer and use it in GitHub Desktop.
Save YuvalItzchakov/707275b73a54cb476609 to your computer and use it in GitHub Desktop.
public class Program
{
public static void Main(string[] args)
{
new BenchmarkRunner().RunCompetition(new IL_Loops());
}
}
public class IL_Loops
{
[Params(1, 5, 10, 100)]
int MaxCounter = 0;
private int[] initialValuesArray;
[Setup]
public void SetupData()
{
initialValuesArray = Enumerable.Range(0, MaxCounter).ToArray();
}
[Benchmark]
public int ForLoop()
{
var counter = 0;
for (int i = 0; i < initialValuesArray.Length; i++)
counter += initialValuesArray[i];
return counter;
}
[Benchmark]
public int ForEachArray()
{
var counter = 0;
foreach (var i in initialValuesArray)
counter += i;
return counter;
}
}
Result:
// ***** Competition: Start *****
// Found benchmarks:
// IL_Loops_ForEachArray_Throughput_HostPlatform_HostJit_NET-HostFramework -w=5 -t=10
// IL_Loops_ForLoop_Throughput_HostPlatform_HostJit_NET-HostFramework -w=5 -t=10
// **************************
// Benchmark: IL_Loops_ForEachArray (Throughput_HostPlatform_HostJit_NET-HostFramework) [-w=5 -t=10]
// Generated project: C:\windows\system32\IL_Loops_ForEachArray_Throughput_HostPlatform_HostJit_NET-HostFramework
// Build:
// Program.cs(7,7): error CS1001: Identifier expected
// OverallResult = Failure
// **************************
// Benchmark: IL_Loops_ForLoop (Throughput_HostPlatform_HostJit_NET-HostFramework) [-w=5 -t=10]
// Generated project: C:\windows\system32\IL_Loops_ForLoop_Throughput_HostPlatform_HostJit_NET-HostFramework
// Build:
// Program.cs(7,7): error CS1001: Identifier expected
// OverallResult = Failure
// ***** Competition: Finish *****
```ini
BenchmarkDotNet=v0.7.8.0
OS=Microsoft Windows NT 6.1.7601 Service Pack 1
Processor=Intel(R) Core(TM) i7-4600U CPU @ 2.10GHz, ProcessorCount=4
HostCLR=MS.NET 4.0.30319.42000, Arch=32-bit
6InvalidOperationException4
Sequence contains no elements
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment