Created
September 18, 2016 14:24
-
-
Save epsi1on/35bcdc3c72ac1f0a574ef5051a7cb5e9 to your computer and use it in GitHub Desktop.
brief code to evaluate three methods for processing the progress
This file contains 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
internal class Program | |
{ | |
private static void Progress(int length) | |
{ | |
const int TOTAL = 100; | |
var s = new System.Diagnostics.Stopwatch(); | |
int j = 0; | |
var last = 0l; | |
Console.Write("{0,12}", length); | |
s.Start(); | |
for (int i = 0; i < length; i++) | |
{ | |
// WARNING: the multiplication can overflow. | |
if ((TOTAL*(long) i)/length != last) | |
{ | |
j++; | |
last = (TOTAL*i)/length; | |
// ProgessChanged(last, null); | |
} | |
} | |
s.Stop(); | |
Console.Write("{0,12}", j); | |
Console.Write("{0,10}ms", s.ElapsedMilliseconds); | |
j = 0; | |
// Better way to report progress: | |
double step = length/(double) TOTAL; | |
double current = 0.0; | |
s.Restart(); | |
for (int i = 0; i < length; i++) | |
{ | |
if (i > current) | |
{ | |
j++; | |
current += step; | |
// ProgessChanged((int)((current + 0.5) / step)), null); | |
} | |
} | |
s.Stop(); | |
Console.Write("{0,12}", j); | |
Console.Write("{0,10}ms", s.ElapsedMilliseconds); | |
Console.WriteLine(); | |
} | |
private static void TestProgressProcessing() | |
{ | |
int[] values = {5, 10, 25, 50, 72, 100, 101, 777, 2100, 2107, 5000000, 11119999, 211119999}; | |
Console.WriteLine(" Size Calls Div Time Div Calls Mod Time Mod"); | |
foreach (var item in values) | |
{ | |
Progress_long(item); | |
Progress_Double(item); | |
Progress_Hybrid(item); | |
Console.WriteLine("======"); | |
} | |
} | |
private static void Progress_long(int length) | |
{ | |
var sp = System.Diagnostics.Stopwatch.StartNew(); | |
var percent = 0; | |
var step = 1; //Math.Max(length/500, 1); | |
var hundred = 100L; //convert to 100 without L prefix to cause the overflow | |
try | |
{ | |
for (var i = 0; i <= length; i += step) | |
{ | |
if (percent != hundred*i/length) | |
//checked will throw exception if any overflow occurs - only to be sure no overflow occurs | |
{ | |
ClearCurrentConsoleLine(); | |
Console.Write(percent = (int) (hundred*i/length)); | |
Console.Write("%"); | |
} | |
} | |
sp.Stop(); | |
Console.WriteLine("Long, Done: {0}, {1} ms, no overflow occurs", length, sp.ElapsedMilliseconds); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(", Failed: {0}, overflow occurs", length); | |
} | |
} | |
private static void Progress_Double(int length) | |
{ | |
var sp = System.Diagnostics.Stopwatch.StartNew(); | |
const int Total = 100; | |
double step = length/(double) Total; | |
double current = 0.0; | |
try | |
{ | |
for (var i = 0; i <= length; i++) | |
{ | |
//System.Threading.Thread.Sleep(100); | |
if (i > current) | |
{ | |
current += step; | |
var percent = (int) ((current/step) + 0.5); | |
ClearCurrentConsoleLine(); | |
Console.Write(percent); | |
Console.Write("%"); | |
} | |
} | |
sp.Stop(); | |
Console.WriteLine("double, Done: {0}, {1} ms, no overflow occurs", length, sp.ElapsedMilliseconds); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(", Failed: {0}, overflow occurs", length); | |
} | |
} | |
private static void Progress_Hybrid(int length) | |
{ | |
var sp = System.Diagnostics.Stopwatch.StartNew(); | |
double current = 0.0; | |
var next = 0; | |
try | |
{ | |
for (var i = 0; i <= length; i++) | |
{ | |
if (i >= next) | |
{ | |
var percent = (int) ((100L*i)/(length)); | |
next = (int) ((percent + 1)/100d*length); | |
ClearCurrentConsoleLine(); | |
Console.Write(percent); | |
Console.Write("%"); | |
} | |
} | |
sp.Stop(); | |
Console.WriteLine("Hybrid, Done: {0}, {1} ms, no overflow occurs", length, sp.ElapsedMilliseconds); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(", Failed: {0}, overflow occurs", length); | |
} | |
} | |
public static void ClearCurrentConsoleLine() | |
{ | |
int currentLineCursor = Console.CursorTop; | |
Console.SetCursorPosition(0, Console.CursorTop); | |
Console.Write(new string(' ', Console.WindowWidth)); | |
Console.SetCursorPosition(0, currentLineCursor); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment