/
TriplesCS.cs
74 lines (63 loc) · 1.93 KB
/
TriplesCS.cs
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
using System.Diagnostics;
using System;
using System.IO;
public class TriplesCS
{
private static int CountTriples(int[] arr, int sum)
{
int n = arr.Length;
int count = 0;
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
for (int k = j + 1; k < n; k++)
{
if (arr[i] + arr[j] + arr[k] == sum)
{
count++;
}
}
}
}
return count;
}
private static int[] ReadInput(string fileName) {
string[] lines = File.ReadAllLines(fileName);
int n = int.Parse(lines[0]);
int[] arr = new int[n];
for (int i = 0; i < n; i++)
{
arr[i] = int.Parse(lines[i+1]);
}
return arr;
}
public static void Main(string[] args)
{
if (args.Length != 2) {
Console.WriteLine("Please provide two arguments");
Console.WriteLine("1) An input file");
Console.WriteLine("2) Number of repetitions");
return;
}
string fileName = args[0];
int reps = int.Parse(args[1]);
int[] arr = ReadInput(fileName);
double timeSum = 0;
double timeSqSum = 0;
Console.WriteLine("Benchmarking C# Program...");
for (int i = 0; i < reps; i++)
{
Stopwatch sw = new Stopwatch();
sw.Start();
int triples = CountTriples(arr, 0);
double time = sw.Elapsed.TotalMilliseconds;
timeSum += time;
timeSqSum += (time * time);
Console.WriteLine($"Run {i+1}/{reps}, {time:F2} ms, {triples} 0-Triples");
}
double mean = timeSum / reps;
double stdDev = Math.Sqrt((timeSqSum - reps * mean * mean) / reps);
Console.WriteLine($"{mean:F2} ms +/- {stdDev:F2}");
}
}