如果有效的话,我怎么能整齐地计时呢?

时间:2011-06-29 14:59:00

标签: c#

目前我正在使用以下内容:

Stopwatch stopWatchB = new Stopwatch();
stopWatchB.Start();
_callIndex.Test = _callTable.Get(u => u.PartitionKey == _callIndex.PageMeta.User & u.RowKey == "A");
stopWatchB.Stop();
em1 = stopWatchB.ElapsedMilliseconds;

我的代码效果很好但看起来很乱。秒表开始和停止:-(有没有办法我可以清理它。请注意,我无法更改.Get()方法,返回到_callIndex.Test的数据是一个名为Test的类,它有多个字段。< / p>

4 个答案:

答案 0 :(得分:4)

嗯,首先你可以使用:

Stopwatch stopWatchB = Stopwatch.StartNew();

如果您愿意,也可以先取ElapsedMilliseconds而不先停止它:

Stopwatch stopWatchB = Stopwatch.StartNew();
_callIndex.Test = _callTable.Get(
     u => u.PartitionKey == _callIndex.PageMeta.User & u.RowKey == "A");
em1 = stopWatchB.ElapsedMilliseconds;

这有点简单。或者,您可以创建一个辅助方法:

public static TimeSpan Time(Action action)
{
    Stopwatch stopwatch = Stopwatch.StartNew();
    action();
    return stopwatch.Elapsed;
}

然后:

em1 = StopwatchHelper.Time(() => {
    _callIndex.Test = _callTable.Get(
         u => u.PartitionKey == _callIndex.PageMeta.User & u.RowKey == "A");
}).TotalMilliseconds;

答案 1 :(得分:1)

我用它做“基准”

使用它:

using(var b = new bench())
{
     //stuff
     em1 = b.ElapsedMilliseconds;
}

///

class bench : Stopwatch, IDisposable
{
    private static bool enabled = true;

    public static bool Enabled
    {
        get { return enabled; }
        set { enabled = value; }
    }

    private string func;

    /// <summary>
    /// Initializes a new instance of the <see cref="bench"/> class.
    /// </summary>
    public bench()
    {
        begin("", false, false);
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="bench"/> class.
    /// </summary>
    /// <param name="showStack">if set to <c>true</c> [show stack].</param>
    public bench(bool showStack)
    {
        begin("", showStack, false);
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="bench"/> class.
    /// </summary>
    /// <param name="showStack">if set to <c>true</c> [show stack].</param>
    /// <param name="showStart">if set to <c>true</c> [show start].</param>
    public bench(bool showStack, bool showStart)
    {
        begin("", showStack, showStart);
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="bench"/> class.
    /// </summary>
    /// <param name="func">The func.</param>
    public bench(String func)
    {
        begin(func, false, false);
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="bench"/> class.
    /// </summary>
    /// <param name="func">The func.</param>
    /// <param name="showStack">if set to <c>true</c> [show stack].</param>
    public bench(String func, bool showStack)
    {
        begin(func, showStack, false);
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="bench"/> class.
    /// </summary>
    /// <param name="func">The func.</param>
    /// <param name="showStack">if set to <c>true</c> [show stack].</param>
    /// <param name="showStart">if set to <c>true</c> [show start].</param>
    public bench(String func, bool showStack, bool showStart)
    {
        begin(func, showStack, showStart);
    }

    /// <summary>
    /// Begins the specified func.
    /// </summary>
    /// <param name="func">The func.</param>
    /// <param name="showStack">if set to <c>true</c> [show stack].</param>
    /// <param name="showStart">if set to <c>true</c> [show start].</param>
    private void begin(String func, bool showStack, bool showStart)
    {
        if (bench.Enabled)
        { 
            this.func = func;

            if (showStack || showStart)
                Debug.WriteLine("Start " + func);

            if (showStack)
                Debug.WriteLine("Stack: " + Environment.StackTrace);

            this.Start();
        }
    }

    /// <summary>
    /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
    /// </summary>
    public void Dispose()
    {
        if (bench.Enabled || this.IsRunning)
        {
            this.Stop();

            if (bench.Enabled)
            { 
                Debug.WriteLine("Stop " + func + " " + Elapsed.TotalSeconds.ToString("0.#######") + " seconds");
            }
        }
    }
}

答案 2 :(得分:0)

创建一个实现IDisposable的类,并在创建时启动秒表,在处理时停止它。

您可以创建一个管理此类项目的类,如我的代码所示:

https://stackoverflow.com/questions/6410569/speeding-up-performance-monitoring-code

它比你提出的要多得多(处理时间'子代码和渲染时间值),但是你可以希望得到基本的想法。

编辑:举一个简单的例子,请参阅@ Fredou的回答。

答案 3 :(得分:0)

您可以编写在构造函数中启动Stopwatch的类,并在Dispose()中停止(显然您必须实现IDisposable)。

using (var profiler = new PerformanceProfiler(out elapsedMilliseconds)
{
    // profiled action code ...
}

Debug.WriteLine(elapsedMilliseconds.ToString(CultureInfo.CurrentCulture));