2013-04-21 53 views
9

我正在为我的大学制作一个图像隐写术项目。我已经完成了这个项目,并保留了几种不同的算法来隐藏图像中的数据。查找方法的执行时间

我想问的是,在C#中,我可以通过它找到程序中两点之间的执行/运行时间。 例如

//Some Code 
//Code to start recording the time. 
hideDataUsingAlgorithm(); 
//Code to stop recording and get the time of execution of the above function. 

我想这样做,以显示简单(耗时更少)和更有效的,但耗时的算法之间的差异(使用相同的数据和相同的图像)。我对Color和GrayScale Images有大约10种不同的算法。

没有多线程,所以不会是一个问题。 Theres只是一个主线程。

+0

的可能的复制[测量代码执行时间(https://stackoverflow.com/questions/16376191/measuring-code-execution-time) - 这是老了几天,但【注意事项】(HTTPS:/ /meta.stackexchange.com/questions/10841/how-should-duplicate-questions-be-handled/),“*一般规则是保留问题的最佳答案集合,并关闭另一个作为重复*“ – ruffin 2017-10-31 15:48:52

回答

4

您可以使用StopWatch类:

var timer = System.Diagnostics.StopWatch.StartNew(); 
hideDataUsingAlgorithm(); 
timer.Stop(); 
var elapsed = timer.ElapsedMilliseconds; 
14

这是秒表一个有用的扩展方法:

public static class StopwatchExt 
{ 
    public static string GetTimeString(this Stopwatch stopwatch, int numberofDigits = 1) 
    { 
     double time = stopwatch.ElapsedTicks/(double)Stopwatch.Frequency; 
     if (time > 1) 
      return Math.Round(time, numberofDigits) + " s"; 
     if (time > 1e-3) 
      return Math.Round(1e3 * time, numberofDigits) + " ms"; 
     if (time > 1e-6) 
      return Math.Round(1e6 * time, numberofDigits) + " µs"; 
     if (time > 1e-9) 
      return Math.Round(1e9 * time, numberofDigits) + " ns"; 
     return stopwatch.ElapsedTicks + " ticks"; 
    } 
} 

使用方法如下:

Stopwatch stopwatch = Stopwatch.StartNew(); 
//Call your method here 
stopwatch.Stop(); 
Console.WriteLine(stopwatch.GetTimeString()); 
0

你可以声明你的测试方法的委托,并使用以下的扩展方法之一执行N次。根据您获得打印到控制台传递的格式字符串:

  • 首先通话时间
  • 经过时间
  • 呼叫频率

这些都是有用的值。扩展方法使用秒表来获得最高精度。

Action acc = hideDataUsingAlgorithm; 
acc.Profile(100*1000, "Method did run {runs} times in {time}s, Frequency: {frequency}"); 

同时检查启动的效果,你可以使用

acc.ProfileFirst(100*1000, "First call {0}s", "Method did run {runs} times in {time}s, Frequency: {frequency}"); 

这样你就可以很容易地检查你的方法,如果有问题的方法不是一个空洞的方法,它会扭曲时机,因为委托调用会与您的方法调用相当。最初的想法是博客here

对于更深的通话时间分析,分析器也非常有用。您应该尝试使用这些以便能够诊断棘手的问题。

using System; 
using System.Globalization; 
using System.Diagnostics; 

namespace PerformanceTester 
{ 
    /// <summary> 
    /// Helper class to print out performance related data like number of runs, elapsed time and frequency 
    /// </summary> 
    public static class Extension 
    { 
     static NumberFormatInfo myNumberFormat; 

     static NumberFormatInfo NumberFormat 
     { 
      get 
      { 
       if (myNumberFormat == null) 
       { 
        var local = new CultureInfo("en-us", false).NumberFormat; 
        local.NumberGroupSeparator = " "; // set space as thousand separator 
        myNumberFormat = local; // make a thread safe assignment with a fully initialized variable 
       } 
       return myNumberFormat; 
      } 
     } 

     /// <summary> 
     /// Execute the given function and print the elapsed time to the console. 
     /// </summary> 
     /// <param name="func">Function that returns the number of iterations.</param> 
     /// <param name="format">Format string which can contain {runs} or {0},{time} or {1} and {frequency} or {2}.</param> 
     public static void Profile(this Func<int> func, string format) 
     { 

      Stopwatch watch = Stopwatch.StartNew(); 
      int runs = func(); // Execute function and get number of iterations back 
      watch.Stop(); 

      string replacedFormat = format.Replace("{runs}", "{3}") 
             .Replace("{time}", "{4}") 
             .Replace("{frequency}", "{5}"); 

      // get elapsed time back 
      float sec = watch.ElapsedMilliseconds/1000.0f; 
      float frequency = runs/sec; // calculate frequency of the operation in question 

      try 
      { 
       Console.WriteLine(replacedFormat, 
            runs, // {0} is the number of runs 
            sec, // {1} is the elapsed time as float 
            frequency, // {2} is the call frequency as float 
            runs.ToString("N0", NumberFormat), // Expanded token {runs} is formatted with thousand separators 
            sec.ToString("F2", NumberFormat), // expanded token {time} is formatted as float in seconds with two digits precision 
            frequency.ToString("N0", NumberFormat)); // expanded token {frequency} is formatted as float with thousands separators 
      } 
      catch (FormatException ex) 
      { 
       throw new FormatException(
        String.Format("The input string format string did contain not an expected token like "+ 
           "{{runs}}/{{0}}, {{time}}/{{1}} or {{frequency}}/{{2}} or the format string " + 
           "itself was invalid: \"{0}\"", format), ex); 
      } 
     } 

     /// <summary> 
     /// Execute the given function n-times and print the timing values (number of runs, elapsed time, call frequency) 
     /// to the console window. 
     /// </summary> 
     /// <param name="func">Function to call in a for loop.</param> 
     /// <param name="runs">Number of iterations.</param> 
     /// <param name="format">Format string which can contain {runs} or {0},{time} or {1} and {frequency} or {2}.</param> 
     public static void Profile(this Action func, int runs, string format) 
     { 
      Func<int> f =() => 
      { 
       for (int i = 0; i < runs; i++) 
       { 
        func(); 
       } 
       return runs; 
      }; 
      f.Profile(format); 
     } 

     /// <summary> 
     /// Call a function in a for loop n-times. The first function call will be measured independently to measure 
     /// first call effects. 
     /// </summary> 
     /// <param name="func">Function to call in a loop.</param> 
     /// <param name="runs">Number of iterations.</param> 
     /// <param name="formatFirst">Format string for first function call performance.</param> 
     /// <param name="formatOther">Format string for subsequent function call performance.</param> 
     /// <remarks> 
     /// The format string can contain {runs} or {0},{time} or {1} and {frequency} or {2}. 
     /// </remarks> 
     public static void ProfileWithFirst(this Action func, int runs, string formatFirst, string formatOther) 
     { 
      func.Profile(1, formatFirst); 
      func.Profile(runs - 1, formatOther); 
     } 
    } 
} 
0

您还可以使用BenchmarkDotNet

然后你做:

1)你要测试的代码的引用创建一个控制台项目。

using BenchmarkDotNet.Running; 
using BenchmarkDotNet.Attributes; 
class Program 
{ 
    static void Main() 
    { 
     var summary = BenchmarkRunner.Run<YourBenchmarks>(); 
    } 
} 

public class YourBenchmarks 
{ 
    [Benchmark] 
    public object HideDataUsingAlgorithm() 
    { 
     return Namespace.hideDataUsingAlgorithm(); // call the code you want to benchmark here 
    } 
} 

2)内置发行版并且在没有调试器的情况下运行。

3)打开是在bin /发行/ YourBenchmarks-report-stackoverflow.md

该报告包含中位数和STDDEV默认的报告。 BenchmarkDotNet负责热身并启动该过程多次以提供准确的统计数据。

报告示例:

    Method |  Median | StdDev | 
----------------------- |------------ |---------- | 
HideDataUsingAlgorithm | 252.4869 ns | 8.0261 ns | 

对于配置读取docs