Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
106 lines (97 loc) · 4.02 KB
/
Copy pathProgram.cs
File metadata and controls
106 lines (97 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#region Copyright
/*********************************************************************************************
Namespace : BenchmarkLINQ
Description : LINQ vs For-Loop performance estimates
: 2 methods to calculate the sum of even integers in array int[]
*********************************************************************************************
Author : Alexander Bell
Copyright : 2016 Infosoft International Inc
Date Created : 10/22/2016
*********************************************************************************************
DISCLAIMER : This Application is provide on AS IS basis without any warranty
TERMS OF USE : Please Keep the Copyright notice intact/
*********************************************************************************************/
#endregion
using System;
using System.Diagnostics;
using System.Linq;
namespace BenchmarkLINQ
{
class Program
{
private const Int64 LoopCounter = 1000;
private static readonly int[] _arrInt = { 111, 112, 22, 38, 116, 133, 232, 236, 364, 427};
static void Main(string[] args)
{
Console.WriteLine("Runtime LINQ. ms: " + Sum_LINQ(_arrInt).ToString());
Console.WriteLine("Runtime ForLoop. ms: " + Sum_For(_arrInt).ToString());
Console.WriteLine("Press any key...");
Console.ReadKey();
}
#region LINQ-based conditional sum calculation, Performance benchmark
/// <summary>
/// Conditional sum of even integers in int[] using LINQ, performance benchmark
/// 1. Very slow
/// 2. Non-portable to core Java (it requires LINQ library)
/// </summary>
/// <param name="intArray">int[]</param>
/// <returns>double</returns>
private static double Sum_LINQ(int[] intArray)
{
long _sum;
// stopwatch obj (ref: System.Diagnostics)
Stopwatch _sw = new Stopwatch();
// start count ticks
_sw.Start();
// multiple pass defined by LoopCounter
for (long j=0; j<LoopCounter; j++)
{
_sum = intArray.Where(i => i % 2 == 0).Sum(i => (long)i);
}
_sw.Stop();
// duration in ms, rounded
return Math.Round(TicksToMillisecond(_sw.ElapsedTicks), 2);
}
#endregion
#region For-Loop conditional sum calculation, Performance benchmark
/// <summary>
/// Conditional sum of even integers in int[] using For-Loop, Performance benchmark
/// 1. Very fast (more than the order of magnitude faster than LINQ implementation)
/// 2. Easy portable to core Java
/// </summary>
/// <param name="intArray">int[]</param>
/// <returns>double</returns>
private static double Sum_For(int[] intArray)
{
long _ret;
// stopwatch obj (ref: System.Diagnostics)
Stopwatch _sw = new Stopwatch();
// start count ticks
_sw.Start();
for (long j = 0; j < LoopCounter; j++)
{
_ret = 0;
for (int i=0; i< intArray.Length; i++)
{
if(intArray[i] % 2==0) _ret += intArray[i];
}
}
_sw.Stop();
// duration in ms, rounded
return Math.Round(TicksToMillisecond(_sw.ElapsedTicks), 2);
}
#endregion
/// <summary>
/// Auxiliary method to calculate runtime (ms) from Ticks
/// </summary>
/// <param name="Ticks">Int64</param>
/// <returns>double</returns>
private static double TicksToMillisecond(Int64 Ticks)
{
// msec per tick (stopwatch frequency in kHz)
double _msPerTick = (double)1000 / Stopwatch.Frequency;
// duration in msec
return Ticks * _msPerTick;
}
}
}