/// <summary> /// Repeat action for a specified time and return number of iterations done during the specified time. /// If time was not specified function will result -1. /// If no operation has been performed before the specified time has elapsed, the function will return a zero value. /// </summary> /// <param name="function"></param> /// <param name="timeSpan"></param> /// <returns></returns> public static int Loop(Energy.Base.Anonymous.Action function, TimeSpan timeSpan) { if (timeSpan == null || timeSpan.TotalSeconds == 0) { return(-1); } int count = 0; System.Threading.ManualResetEvent reset = new System.Threading.ManualResetEvent(false); System.Threading.Thread thread = new System.Threading.Thread(() => { try { while (true) { function(); count++; } } catch (System.Threading.ThreadAbortException) { } }); thread.Start(); reset.WaitOne((int)(timeSpan.TotalMilliseconds), true); thread.Abort(); return(count); }
/// <summary> /// Perform a profiling operation by launching the action the specified number of times /// and returning the result of the profiling. /// </summary> /// <param name="function"></param> /// <param name="iterations"></param> /// <param name="warm"></param> /// <param name="name"></param> public static Energy.Core.Benchmark.Result Profile(Energy.Base.Anonymous.Action function, int iterations, int warm, string name) { Result result = Profile(function, iterations, warm); if (!string.IsNullOrEmpty(name)) { result.Name = name; } return(result); }
/// <summary> /// Perform a profiling operation by launching the action the specified number of times /// and returning the result of the profiling. /// </summary> /// <param name="function"></param> /// <param name="iterations"></param> /// <param name="warm"></param> public static Energy.Core.Benchmark.Result Profile(Energy.Base.Anonymous.Action function, int iterations, int warm) { Result result = new Result() { Name = function.Method.Name, Iterations = iterations }; // warm up // while (warm-- > 0) { function(); iterations--; } Stopwatch watch = new Stopwatch(); watch.Start(); // clean up // GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); watch.Stop(); result.Garbage = watch.Elapsed.TotalMilliseconds / 1000; // go on // watch.Reset(); watch.Start(); for (int i = 0; i < iterations; i++) { function(); } watch.Stop(); result.Time = watch.Elapsed.TotalMilliseconds / 1000; if (iterations > 1) { result.Average = watch.Elapsed.TotalMilliseconds / iterations / 1000; } return(result); }
/// <summary> /// Create argument list from string array with specified command options style. /// </summary> /// <param name="args"></param> /// <param name="style"></param> /// <returns></returns> public static ArgumentList Create(string[] args, Energy.Core.Shell.OptionStyle style) { Energy.Base.Anonymous.Function <string, string> testDelA = delegate(string s) { return(s); }; Energy.Base.Anonymous.Action <string> testDelB = delegate(string s) { Console.WriteLine(s); }; ArgumentList list = new Core.Shell.ArgumentList(); List <string> o = new List <string>(); for (int i = 0; i < args.Length; i++) { string s = args[i]; if (false) { } // Double dash else if (style.Double && s.StartsWith("--")) { if (s.Length == 2) { list.Arguments.Add(s); continue; } s = s.Substring(2); } // Slash else if (style.Slash && s.StartsWith("/")) { if (s.Length == 1) { list.Arguments.Add(s); continue; } s = s.Substring(1); if (style.Multiple && Energy.Base.Text.Contains(s, "/")) { string[] a = s.Split('/'); o.AddRange(a); } else { o.Add(s); } } // Single dash else if (style.Single && s.StartsWith("-")) { if (s.Length == 1) { list.Arguments.Add(s); continue; } s = s.Substring(1); if (s.Length == 1) { o.Add(s); } else if (!style.Multiple) { o.Add(s); } else if (style.Short) { foreach (char c in s.ToCharArray()) { if (c == '-') { continue; } o.Add(c.ToString()); } } else { string[] a = s.Split('-'); for (int n = 0; n < a.Length; n++) { o.Add(a[n]); } } } else { list.Arguments.Add(s); } for (int n = 0; n < o.Count; n++) { s = o[n]; if (false) { } else if (style.Colon && Energy.Base.Text.Contains(s, ":")) { int x = s.IndexOf(":"); string v = s.Substring(x + 1); s = s.Substring(0, x - 1); list.Values[s] = v; } else if (style.Equal && Energy.Base.Text.Contains(s, "=")) { int x = s.IndexOf("="); string v = s.Substring(x + 1); s = s.Substring(0, x - 1); list.Values[s] = v; } list.Options.Add(s); } if (o.Count > 0) { o.Clear(); } } return(list); }
/// <summary> /// Repeat action for a specified time and return number of iterations done during the specified time. /// If no operation has been performed before the specified time has elapsed, the function will return a zero value. /// </summary> /// <param name="action"></param> /// <param name="time"></param> /// <returns></returns> public static int Loop(Energy.Base.Anonymous.Action action, double time) { return(Loop(action, TimeSpan.FromSeconds(time))); }
/// <summary> /// Perform a profiling operation by launching the action the specified number of times /// and returning the result of the profiling. /// </summary> /// <param name="function"></param> public static Energy.Core.Benchmark.Result Profile(Energy.Base.Anonymous.Action function) { return(Profile(function, 1, 0)); }
/// <summary> /// Create time trap for execution. While disposed, it will invoke optional action. /// </summary> /// <param name="timeLimit">Time limit in seconds. When finished in shorter time, action will not be executed.</param> /// <param name="action">Action when time exceeds limit</param> /// <returns></returns> public static Energy.Base.Trap Trap(double timeLimit, Energy.Base.Anonymous.Action action) { Energy.Base.Trap trap = new Energy.Base.Trap(timeLimit, action); return(trap); }
public Trap(double timeLimit, Energy.Base.Anonymous.Action <TimeSpan> action) : this(timeLimit) { _ActionFunctionTimeSpan = action; }