commit 5ef27dff1269dc22bea157ce85cdc4c29b112f6d Author: Mattrixwv Date: Sun Aug 23 02:30:55 2020 -0400 Initial commit with Stopwatch diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..815d59d --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +#Ignore IDE files +.vs +.vscode + +#Ignore compiled code +CSClasses/bin +CSClasses/obj diff --git a/CSClasses.sln b/CSClasses.sln new file mode 100644 index 0000000..30f2bd8 --- /dev/null +++ b/CSClasses.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30413.136 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSClasses", "CSClasses\CSClasses.csproj", "{A3C01050-4957-4AF3-8908-21966FD99A06}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {A3C01050-4957-4AF3-8908-21966FD99A06}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A3C01050-4957-4AF3-8908-21966FD99A06}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A3C01050-4957-4AF3-8908-21966FD99A06}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A3C01050-4957-4AF3-8908-21966FD99A06}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {B0128E8F-6695-4A7B-8336-9797AD19FC98} + EndGlobalSection +EndGlobal diff --git a/CSClasses/CSClasses.csproj b/CSClasses/CSClasses.csproj new file mode 100644 index 0000000..9f5c4f4 --- /dev/null +++ b/CSClasses/CSClasses.csproj @@ -0,0 +1,7 @@ + + + + netstandard2.0 + + + diff --git a/CSClasses/Stopwatch.cs b/CSClasses/Stopwatch.cs new file mode 100644 index 0000000..f0a5249 --- /dev/null +++ b/CSClasses/Stopwatch.cs @@ -0,0 +1,184 @@ +//C#/CSClasses/Stopwatch.cs +//Matthew Ellison +// Created: 08-21-20 +//Modified: 08-21-20 +//This file contains a class that is used to time the execution time of other programs +/* +Copyright (C) 2020 Matthew Ellison + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . +*/ +namespace mee{ + public class Stopwatch{ + //Variables + private System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); //The stopwatch that is used + private enum TIME_RESOLUTION{ NANOSECOND, MICROSECOND, MILLISECOND, SECOND, MINUTE, HOUR, ERROR }; + private const int NUM_TO_RUN = 100000; + //Functions + //Constructor makes sure all values are set to defaults + public Stopwatch(){ + } + //Returns a long with the elapsed time in nanoseconds. Used by other functions to get the time before converting it to the correct resolution + private long getTime(){ + return stopwatch.ElapsedTicks; + } + //Simultates starting a stopwatch + public void start(){ + stopwatch.Start(); + } + //Simulates stopping a stopwatch + public void stop(){ + stopwatch.Stop(); + } + //Resets all the variables in teh stopwatch + public void reset(){ + stopwatch.Reset(); + } + //Returns the timer in nanoseconds + public decimal getNano(){ + //Each tick = 100 nanoseconds + return (decimal)stopwatch.ElapsedTicks * 100m; + } + //Returns the timer in microseconds + public decimal getMicro(){ + return (decimal)stopwatch.ElapsedTicks / 10m; + } + //Returns the timer in milliseconds + public decimal getMilli(){ + return (decimal)stopwatch.ElapsedTicks / 10000m; + } + //Returns the timer in seconds + public decimal getSecond(){ + return (decimal)stopwatch.ElapsedTicks / 10000000m; + } + //Returns the timer in minutes + public decimal getMinute(){ + return (decimal)stopwatch.ElapsedTicks / 600000000m; + } + //Returns the timer in hours + public decimal getHour(){ + return (decimal)stopwatch.ElapsedTicks / 36000000000m; + } + //Returns the timer as a string at the 'best' resolution. (Goal is xxx.xxx) + public string getStr(){ + //Get the current duration from time + return getStr(getNano()); + } + //Returns a string of the decimal value passed in (assuming the value in nanoseconds) + public static string getStr(decimal nanoseconds){ + decimal duration = nanoseconds; + //Reduce the number to the appropriate number of digits. (xxx.x) + //This loop works down to seconds + TIME_RESOLUTION resolution = new TIME_RESOLUTION(); + for(resolution = TIME_RESOLUTION.NANOSECOND;(resolution < TIME_RESOLUTION.SECOND) && (duration >= 1000);++resolution){ + duration /= 1000; + } + //Check if the duration needs reduced to minutes + if((duration >= 120) && (resolution == TIME_RESOLUTION.SECOND)){ + //Reduce to mintues + duration /= 60; + ++resolution; + + //Check if the duration needs reduced to hours + if(duration >= 60){ + //Reduce to hours + duration /= 60; + ++resolution; + } + } + + //Turn the number into a string + //string time = string.Format("{0C3}", duration); + string time = duration.ToString("C3").Substring(1); + + //Tack on the appropriate suffix for resolution + switch(resolution){ + case TIME_RESOLUTION.NANOSECOND: time += " nanoseconds"; break; + case TIME_RESOLUTION.MICROSECOND: time += " microseconds"; break; + case TIME_RESOLUTION.MILLISECOND: time += " milliseconds"; break; + case TIME_RESOLUTION.SECOND: time += " seconds"; break; + case TIME_RESOLUTION.MINUTE: time += " minutes"; break; + case TIME_RESOLUTION.HOUR: time += " hours"; break; + case TIME_RESOLUTION.ERROR: + default: time = "There was an error computing the time"; break; //TODO: This should throw an exception instead + } + //Return the string + return time; + } + //Overrides default tostring. returns getStr + public override string ToString(){ + return getStr(); + } + + //Tests + public static void Main(string[] args){ + System.Console.WriteLine("Testing start/stop"); + testStartStop(); + System.Console.WriteLine("start/stop completed successfully"); + System.Console.WriteLine("Testing conversion"); + testConversion(); + System.Console.WriteLine("conversion test completed successfully"); + System.Console.WriteLine("Testing stringConversion"); + testStringConversion(); + System.Console.WriteLine("stringConversion completed successfully"); + } + public static void testStartStop(){ + Stopwatch timer = new Stopwatch(); + timer.start(); + timer.stop(); + //If it gets here without throwing an exception everything went well + } + public static void testConversion(){ + Stopwatch timer = new Stopwatch(); + int sum = 0; + //Start the timer + timer.start(); + //Do something to run some time + for(int cnt = 0;cnt < NUM_TO_RUN;++cnt){ + sum += cnt; + } + //Stop the timer + timer.stop(); + //Assert something so the sum isn't ignored during compile + System.Diagnostics.Debug.Assert(sum != 0); + //Check that the different resolutions work out correctly + decimal nano = timer.getNano(); + System.Diagnostics.Debug.Assert(timer.getMicro() == (nano / 1000m), "Micro resolution test failed"); + System.Diagnostics.Debug.Assert(timer.getMilli() == (nano / 1000000m), "Milli resolution test failed"); + System.Diagnostics.Debug.Assert(timer.getSecond() == (nano / 1000000000m), "Second resolution test failed"); + System.Diagnostics.Debug.Assert(timer.getMinute() == (nano / 60000000000m), "Minute resolution test failed"); + System.Diagnostics.Debug.Assert(timer.getHour() == (nano / 3600000000000m), "Hour resolution test failed"); + } + public static void testStringConversion(){ + //Test nanoseconds + string results = Stopwatch.getStr(1.0m); + System.Diagnostics.Debug.Assert(results == "1.000 nanoseconds", "Failed nanoseconds: " + results); + //Test microseconds + results = Stopwatch.getStr(1.0e3m); + System.Diagnostics.Debug.Assert(results == "1.00 microsesconds", "Failed microseconds: " + results); + //Test milliseconds + results = Stopwatch.getStr(1.0e6m); + System.Diagnostics.Debug.Assert(results == "1.00 milliseconds", "Failed milliseconds: " + results); + //Test seconds + results = Stopwatch.getStr(1.0e9m); + System.Diagnostics.Debug.Assert(results == "1.00 seconds", "Failed seconds: " + results); + //Test minutes + results = Stopwatch.getStr(1.0e12m); + System.Diagnostics.Debug.Assert(results == "1.00 minutes", "Failed minutes: " + results); + //Test hours + results = Stopwatch.getStr(1.0e13m); + System.Diagnostics.Debug.Assert(results == "1.00 hours", "Failed hours: " + results); + } + } +} \ No newline at end of file