Created
December 7, 2015 18:35
-
-
Save YuvalItzchakov/707275b73a54cb476609 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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