在Java中,我可以使用单个方法作为其他方法的计时器吗?

发布于 2024-10-31 03:39:22 字数 250 浏览 0 评论 0原文

测试运行时时,我按以下方式使用 System.nanotime():

startTime = System.nanotime();

// some statements

System.out.println("Runtime: " + (System.nanoTime() - startTime));

有没有办法为程序中的其他代码块重用此测试模型?换句话说,我可以将其创建为方法并在测试期间将其他方法传递给它吗?

When testing runtime, I use System.nanotime() in the following way:

startTime = System.nanotime();

// some statements

System.out.println("Runtime: " + (System.nanoTime() - startTime));

Is there a way to reuse this test model for other blocks of code in my programs? In other words, can I create this as a method and pass other methods to it during testing?

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

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

发布评论

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

评论(4

南…巷孤猫 2024-11-07 03:39:22

是的,你可以,但它比你的方法更复杂:

time(new Runnable() {
    public void run() {
        System.out.println("statements");
    }
});

static void time(Runnable r) {
    long startTime = System.nanoTime();
    r.run();
    System.out.println("Runtime: " + (System.nanoTime() - startTime));
}

在 Java 7 中,这会变得更简单(我省略了“Measure”实现):

try (Measure m = new Measure()) {
    System.out.println("statements");
}

顺便说一句,最好使用 System.nanoTime( ) 比 System.currentTimeMillis() 更准确,不仅因为前者更准确,还因为后者会在夏令时更改(或其他系统时间更改)时出现卡顿。

Yes, you can, but it's even more complicated than your approach:

time(new Runnable() {
    public void run() {
        System.out.println("statements");
    }
});

static void time(Runnable r) {
    long startTime = System.nanoTime();
    r.run();
    System.out.println("Runtime: " + (System.nanoTime() - startTime));
}

In Java 7, this will get a bit simpler (I left out the 'Measure' implementation):

try (Measure m = new Measure()) {
    System.out.println("statements");
}

By the way, it's better to use System.nanoTime() than System.currentTimeMillis(), not only because the former is more accurate, but also because the later will hickup around summertime change (or other system time changes).

随梦而飞# 2024-11-07 03:39:22

使用 AOP 可能是您最好的选择,它将允许您从外部定义要进行性能测试的方法,并自动用此计时代码来修饰这些方法,而无需修改源代码。

请参阅http://dhruba.name/2008/12/16/spring -aop-timing-aspect/

Using AOP is probably your best bet, it will allow you to externally define what methods you want to performance test, and automatically embellish those methods with this timing code, without having to modify the source code.

See http://dhruba.name/2008/12/16/spring-aop-timing-aspect/

娇俏 2024-11-07 03:39:22

分析器:
实际上我更喜欢通过探查器运行代码。我实际上对瓶颈感兴趣,我想这就是你所追求的。

确实存在其他替代方案。

JUnit 和 JPerf:
如果您有 junit 测试,那么使用 jperf 就是小菜一碟。

动态代理:
如果您的应用程序中有接口,并且您只对接口调用感兴趣,请创建一个动态代理并将其用于时间戳。

秒表对象:
比你的方法更简单的方法是创建一个 Stopwatch 对象(apache commons 有一个),具有开始、暂停、恢复、圈停止方法并使用它。这比您的代码效果更好,因为秒表实例可以与回调、事件和线程一起使用。

AOP:
除了分析器之外,几乎所有上述选项都可以使用自定义代码来实现,但如果您有足够的好奇心来学习 AOP,那就太好了。

Profiler:
I actually prefer running the code through the profiler. I am actually interested in the bottlenecks, and I assume that is what you are after.

Other alternatives do exist.

JUnit and JPerf:
If you have junit tests, then using jperf is a piece of cake.

Dynamic Proxy:
If you have interfaces in your application and iff you are only interested in the interface calls, create a dynamic proxy and use it for time stamping.

Stop watch object:
A simpler approach than yours is to create a Stopwatch object (apache commons has one), with start, pause, resume, lap stop methods and use it. This works better than your code as stop watch instances can be used with callbacks, events and threads.

AOP:
Other than profilers almost all the above options can be realized using custom code, but if you have are curious enough to learn AOP, it would be great.

幻梦 2024-11-07 03:39:22

三个附加答案。首先是(相对)明显的一个:

for (long t = start(); end(t);) {
    System.out.println("statement");
}

static long start() {
    return System.nanoTime();
}

static boolean end(long startTime) {
    System.out.println("Runtime: " + (System.nanoTime() - startTime));
    return false;
}

一个奇怪的,缺点是你会收到警告(变量 v 永远不会被读取)。我想知道这是否可以改进:

for (Void v : time()) {
    System.out.println("statement");
}

static Iterable<Void> time() {
    return new Iterable<Void>() {
        long startTime = System.nanoTime();
        boolean done;
        public Iterator<Void> iterator() {
            return new Iterator<Void>() {
                public boolean hasNext() {
                    if (!done) {
                        done = true;
                        return true;
                    }
                    System.out.println("Runtime: " + (System.nanoTime() - startTime));
                    return false;
                }
                public Void next() {
                    return null;
                }
                public void remove() {
                }
            };
        }
    };
}

以及作弊:

while(time()) {
    System.out.println("statement");
}

static boolean stop;
static long startTime;
static boolean time() {
    if (!stop) {
        startTime = System.nanoTime();
    } else {
        System.out.println("Runtime: " + (System.nanoTime() - startTime));
    }
    return stop = !stop;
}

Three additional answers. First the (relatively) obvious one:

for (long t = start(); end(t);) {
    System.out.println("statement");
}

static long start() {
    return System.nanoTime();
}

static boolean end(long startTime) {
    System.out.println("Runtime: " + (System.nanoTime() - startTime));
    return false;
}

A weird one, with the disadvantage that you get a warning (variable v is never read). I wonder if this could be improved:

for (Void v : time()) {
    System.out.println("statement");
}

static Iterable<Void> time() {
    return new Iterable<Void>() {
        long startTime = System.nanoTime();
        boolean done;
        public Iterator<Void> iterator() {
            return new Iterator<Void>() {
                public boolean hasNext() {
                    if (!done) {
                        done = true;
                        return true;
                    }
                    System.out.println("Runtime: " + (System.nanoTime() - startTime));
                    return false;
                }
                public Void next() {
                    return null;
                }
                public void remove() {
                }
            };
        }
    };
}

And cheating:

while(time()) {
    System.out.println("statement");
}

static boolean stop;
static long startTime;
static boolean time() {
    if (!stop) {
        startTime = System.nanoTime();
    } else {
        System.out.println("Runtime: " + (System.nanoTime() - startTime));
    }
    return stop = !stop;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文