显示最小、最大、平均……时间的秒表类?

发布于 2024-10-21 14:16:39 字数 384 浏览 6 评论 0原文

我正在寻找一个模拟秒表的java类(例如 Spring 的 StopWatchCommons' StopWatch),但这会给出最小、最大和平均时间。最好也对最后 n 次运行进行平均。

有这样的东西还是我自己构建它?

问候,

维姆

I am looking for a java class that simulates a StopWatch (like Spring's StopWatch or Commons' StopWatch) but that would give minimum, maximum and average times. Preferable also average over last n runs as well.

Is there such a thing or do I just build it myself?

regards,

Wim

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠檬色的秋千 2024-10-28 14:16:39

我认为你应该将这两个概念分开:

  • 一个类负责计时(例如,使用 start()/stopped()/elapsedMillis() 方法)
  • 一个类负责根据数据生成有趣的统计数据

这两个概念实际上是非常不同的事情, IMO - 单独测试绝对比一起测试更容易。您很可能能够为每个单独的组件找到第三方库 - 或者可能使用现有的 StopWatch 类,但编写自己的统计分析器。

I think you should split up the two concepts:

  • One class responsible for the timing (e.g. with start()/stopped()/elapsedMillis() methods)
  • One class responsible for producing interesting stats based on data

The two are really pretty separate things, IMO - and definitely easier to test separately than together. You may well be able to find third party libraries for each separate component - or perhaps use an existing StopWatch class, but write your own stats cruncher.

猥琐帝 2024-10-28 14:16:39

根据乔恩·斯基特的回答,这就是我想出的,以防其他人想要它。它使用 Spring 框架中的 StopWatch 类:

public class StopWatch extends org.springframework.util.StopWatch
{
private static final String LINE_SEPARATOR = System.getProperty( "line.separator" );
// ------------------------------ FIELDS ------------------------------

org.springframework.util.StopWatch.TaskInfo m_minimumTimeTask = null;
org.springframework.util.StopWatch.TaskInfo m_maximumTimeTask = null;
private final String m_id;

// --------------------------- CONSTRUCTORS ---------------------------

public StopWatch()
{
    this( "" );
}

public StopWatch( String id )
{
    super( id );
    m_id = id;
}

@Override
public void setKeepTaskList( boolean keepTaskList )
{
    throw new UnsupportedOperationException( "The task list is always kept to be able to calculate the min, max and average" );
}
// -------------------------- PUBLIC METHODS --------------------------

public long getMinimumTimeMillis()
{
    if (m_minimumTimeTask != null)
    {
        return m_minimumTimeTask.getTimeMillis();
    }
    else
    {
        return -1;
    }
}

public long getMaximumTimeMillis()
{
    if (m_maximumTimeTask != null)
    {
        return m_maximumTimeTask.getTimeMillis();
    }
    else
    {
        return -1;
    }
}


public void stop() throws IllegalStateException
{
    super.stop();
    updateMinimumTime();
    updateMaximumTime();
}


public String shortSummary()
{
    StringBuilder builder = new StringBuilder();
    builder.append( "StopWatch '" ).append( m_id )
            .append( "': running time (millis) = " ).append( getTotalTimeMillis() );

    if (getTaskCount() > 0)
    {
        builder.append( LINE_SEPARATOR ).append( "-----------------------------------------" ).append( LINE_SEPARATOR );
        builder.append( "min: " ).append( m_minimumTimeTask.getTimeMillis() ).append( " ms (" )
                .append( m_minimumTimeTask.getTaskName() ).append( ")" ).append( LINE_SEPARATOR );
        builder.append( "max: " ).append( m_maximumTimeTask.getTimeMillis() ).append( " ms (" )
                .append( m_maximumTimeTask.getTaskName() ).append( ")" ).append( LINE_SEPARATOR );
        builder.append( "avg: " ).append( getAverageTimeMillis() ).append( " ms" );
    }
    return builder.toString();
}

// -------------------------- PRIVATE METHODS --------------------------

private void updateMinimumTime()
{
    if (m_minimumTimeTask == null)
    {
        m_minimumTimeTask = getLastTaskInfo();
    }
    else
    {
        if (getLastTaskTimeMillis() < m_minimumTimeTask.getTimeMillis())
        {
            m_minimumTimeTask = getLastTaskInfo();
        }
    }
}

private void updateMaximumTime()
{
    if (m_maximumTimeTask == null)
    {
        m_maximumTimeTask = getLastTaskInfo();
    }
    else
    {
        if (getLastTaskTimeMillis() > m_maximumTimeTask.getTimeMillis())
        {
            m_maximumTimeTask = getLastTaskInfo();
        }
    }
}

public long getAverageTimeMillis()
{
    if( getTaskCount() > 0)
    {
        return getTotalTimeMillis() / getTaskCount();
    }
    else
    {
        return -1L;
    }
}
}

如果运行 stopWatch.prettyPrint(),它看起来像这样:

StopWatch 'TestMinMaxAndAverage': running time (millis) = 1100
-----------------------------------------
min: 100 ms (run3)
max: 500 ms (run4)
avg: 275 ms
-----------------------------------------
ms     %     Task name
-----------------------------------------
00200  018%  run1
00300  027%  run2
00100  009%  run3
00500  045%  run4

Following up to what Jon Skeet answered, this is what I have came up wiht in case somebody else wants it. It uses the StopWatch class from the Spring framework:

public class StopWatch extends org.springframework.util.StopWatch
{
private static final String LINE_SEPARATOR = System.getProperty( "line.separator" );
// ------------------------------ FIELDS ------------------------------

org.springframework.util.StopWatch.TaskInfo m_minimumTimeTask = null;
org.springframework.util.StopWatch.TaskInfo m_maximumTimeTask = null;
private final String m_id;

// --------------------------- CONSTRUCTORS ---------------------------

public StopWatch()
{
    this( "" );
}

public StopWatch( String id )
{
    super( id );
    m_id = id;
}

@Override
public void setKeepTaskList( boolean keepTaskList )
{
    throw new UnsupportedOperationException( "The task list is always kept to be able to calculate the min, max and average" );
}
// -------------------------- PUBLIC METHODS --------------------------

public long getMinimumTimeMillis()
{
    if (m_minimumTimeTask != null)
    {
        return m_minimumTimeTask.getTimeMillis();
    }
    else
    {
        return -1;
    }
}

public long getMaximumTimeMillis()
{
    if (m_maximumTimeTask != null)
    {
        return m_maximumTimeTask.getTimeMillis();
    }
    else
    {
        return -1;
    }
}


public void stop() throws IllegalStateException
{
    super.stop();
    updateMinimumTime();
    updateMaximumTime();
}


public String shortSummary()
{
    StringBuilder builder = new StringBuilder();
    builder.append( "StopWatch '" ).append( m_id )
            .append( "': running time (millis) = " ).append( getTotalTimeMillis() );

    if (getTaskCount() > 0)
    {
        builder.append( LINE_SEPARATOR ).append( "-----------------------------------------" ).append( LINE_SEPARATOR );
        builder.append( "min: " ).append( m_minimumTimeTask.getTimeMillis() ).append( " ms (" )
                .append( m_minimumTimeTask.getTaskName() ).append( ")" ).append( LINE_SEPARATOR );
        builder.append( "max: " ).append( m_maximumTimeTask.getTimeMillis() ).append( " ms (" )
                .append( m_maximumTimeTask.getTaskName() ).append( ")" ).append( LINE_SEPARATOR );
        builder.append( "avg: " ).append( getAverageTimeMillis() ).append( " ms" );
    }
    return builder.toString();
}

// -------------------------- PRIVATE METHODS --------------------------

private void updateMinimumTime()
{
    if (m_minimumTimeTask == null)
    {
        m_minimumTimeTask = getLastTaskInfo();
    }
    else
    {
        if (getLastTaskTimeMillis() < m_minimumTimeTask.getTimeMillis())
        {
            m_minimumTimeTask = getLastTaskInfo();
        }
    }
}

private void updateMaximumTime()
{
    if (m_maximumTimeTask == null)
    {
        m_maximumTimeTask = getLastTaskInfo();
    }
    else
    {
        if (getLastTaskTimeMillis() > m_maximumTimeTask.getTimeMillis())
        {
            m_maximumTimeTask = getLastTaskInfo();
        }
    }
}

public long getAverageTimeMillis()
{
    if( getTaskCount() > 0)
    {
        return getTotalTimeMillis() / getTaskCount();
    }
    else
    {
        return -1L;
    }
}
}

If you run stopWatch.prettyPrint(), it look like this:

StopWatch 'TestMinMaxAndAverage': running time (millis) = 1100
-----------------------------------------
min: 100 ms (run3)
max: 500 ms (run4)
avg: 275 ms
-----------------------------------------
ms     %     Task name
-----------------------------------------
00200  018%  run1
00300  027%  run2
00100  009%  run3
00500  045%  run4
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文