using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace LoopScopeTest
{
internal static class Program
{
private static readonly Encoding encoding = Encoding.UTF8;
public static string GetString(this byte[] b) => encoding.GetString(b);
public static byte[] ToBytes(this string s) => encoding.GetBytes(s);
public static byte[] GetFromHex(this string s) => Convert.FromHexString(s);
public static long Average(this long[] l) => l.Aggregate((x, y) => x + y) / l.Length;
private record Data(string[] DataLine)
{
static byte[] key = { }; // Add your key here!
private SHA256 hmac = SHA256.Create();
private byte[] data;
public long Overall;
public long InlineTest;
public long OutOfScopeTest;
public long Difference;
public string Hash;
public Data GetData()
{
_ = long.TryParse(DataLine[0], out Overall);
_ = long.TryParse(DataLine[1], out InlineTest);
_ = long.TryParse(DataLine[2], out OutOfScopeTest);
_ = long.TryParse(DataLine[3], out Difference);
Hash = DataLine[4];
data = ToString().ToBytes();
return this;
}
public bool IsDataValid()
{
byte[] left = Hash.GetFromHex();
byte[] right = hmac.ComputeHash(data);
string a = Convert.ToHexString(left);
string b = Convert.ToHexString(right);
return a == b;
}
public string CompileHash() => BitConverter.ToString(hmac.ComputeHash(data)).Replace("-", "").ToLower();
public override string ToString()
{
return $"{Overall};{InlineTest};{OutOfScopeTest};{Difference}";
}
}
private static long InlineForloop(ulong iterations)
{
Stopwatch sw = new();
var sb = new StringBuilder();
sb.AppendLine($"Testing inline forloop initializer with {iterations} iterations");
sb.AppendLine("for (ulong u = 0; u < iterations; u++) ; ");
Console.WriteLine(sb.ToString());
if (sw == null) sw = new();
sw.Start();
for (ulong u = 0; u < iterations; u++) ;
sw.Stop();
return sw.ElapsedMilliseconds;
}
private static long OutOfScopeForloop(ulong iterations)
{
Stopwatch sw = new();
var sb = new StringBuilder();
sb.AppendLine($"Testing out of scope forloop initializer with {iterations} iterations");
sb.AppendLine("ulong u;");
sb.AppendLine("for (u = 0; u < iterations; u++) ; ");
Console.WriteLine(sb.ToString());
if (sw == null) sw = new();
sw.Start();
ulong u;
for (u = 0; u < iterations; u++) ;
sw.Stop();
return sw.ElapsedMilliseconds;
}
private static long RunTest(string testName, ulong iterations, Func<ulong, long> test)
{
long result = test(iterations);
Console.WriteLine($"{testName}: {result}ms");
Console.WriteLine("\n\n");
return result;
}
private static void Main(string[] args)
{
Stopwatch sw = new();
sw.Start();
const string log = "tests.log";
if(!File.Exists(log))
{
File.WriteAllText(log, "Overall,Inline,Out of Scope,Difference,Hash\n");
}
string[] history = File.ReadAllLines(log);
long[] overallAverage= new long[history.Length];
long[] testAAverage = new long[history.Length];
long[] testBAverage = new long[history.Length];
long[] differenceAverage = new long[history.Length];
Console.WriteLine("Retrieving history\n");
if (history.Length > 1)
{
for (int i = 1; i < history.Length; i++)
{
Data data = new Data(history[i].Split(',')).GetData();
if (!data.IsDataValid())
{
Console.WriteLine($"Invalid Data, Line {i + 1}");
continue;
}
overallAverage[i] = data.Overall;
testAAverage[i] = data.InlineTest;
testBAverage[i] = data.OutOfScopeTest;
differenceAverage[i] = data.Difference;
}
}
if (history.Length > 1)
{
Console.WriteLine($"Overall average {overallAverage.Average()}ms");
Console.WriteLine($"Inline Test average {testAAverage.Average()}ms");
Console.WriteLine($"Out of Initialization average {testBAverage.Average()}ms");
Console.WriteLine($"Difference average {differenceAverage.Average()}ms");
Console.WriteLine();
}
var sb = new StringBuilder();
ulong iterations = int.MaxValue;
long testA = RunTest("Inline Test", iterations, InlineForloop);
long testB = RunTest("Out of Scope Test", iterations, OutOfScopeForloop);
long overall = testA + testB;
long difference = Math.Abs(testA - testB);
Console.WriteLine($"Difference of {difference}ms");
Console.WriteLine($"Total test time {overall}ms");
Console.WriteLine("\nTest complete!\n\n");
sw.Stop();
Console.WriteLine($"Program Runtime: {sw.ElapsedMilliseconds}ms\n\n");
Console.WriteLine($"That's an additional {sw.ElapsedMilliseconds - overall}ms of runtime.");
sb.AppendLine($"{overall},{testA},{testB},{difference},{new Data(new string[] { overall.ToString(), testA.ToString(), testB.ToString(), difference.ToString(), "" }).GetData().CompileHash()}");
File.AppendAllText(log, sb.ToString());
}
}
}
Comments
0 B
|👍
/👎