简体   繁体   English

在Java中计时操作

[英]timing an operation in java

I want to run my program in eclipse but I want to see how long it takes to run the program. 我想在Eclipse中运行我的程序,但我想看看运行该程序需要多长时间。 In other words I want to time my program. 换句话说,我想为我的程序计时。 I know that in UNIX I can time any operation by placing the word “time” before the command on the command line. 我知道在UNIX中,我可以通过在命令行上的命令前放置“ time”一词来计时任何操作。 But I dont know how I might be able to time my operation in Eclipse. 但是我不知道如何在Eclipse中计时我的操作。 Just to make everything a bit more clear, I want to avoid writing new methods. 为了使所有内容更加清楚,我想避免编写新方法。 Is there a way that I could add sth to my configuration path? 有什么方法可以在配置路径中添加一些内容? Does anyone have an idea of how I can do so? 有人知道我该怎么做吗?

If you don't mind adding two lines of code, you can do this purely in Java: 如果您不介意添加两行代码,则可以完全在Java中执行此操作:

public class Foo {
    public static void main(String[] args) {
        long ms = System.currentTimeMillis();

        // do whatever

        System.out.println(System.currentTimeMillis() - ms);
    }
}

You could also use nanoTime if you need it. 如果需要,您也可以使用nanoTime

在应用程序的末尾添加system.out.println以指示其运行了多长时间。

Put this class somewhere in your code: 将此类放在代码中的某个位置:

package your.package.name.common;

import org.apache.log4j.Logger;

import java.util.concurrent.TimeUnit;

public class CProfile {
    /**
     * Logger for this class
     */
    private static final Logger logger = Logger.getLogger(CProfile.class);

    public static final int SECONDS_TO_ALERT = 2;

    public static void slownessAlert(long startTime, String name) {
        long seconds = TimeUnit.SECONDS.convert(System.nanoTime() - startTime, TimeUnit.NANOSECONDS);
        if (seconds>SECONDS_TO_ALERT){
            logger.warn("slow method detected: "+name+" "+seconds+"s");
        }
    }

}

Then use this snippet in your code in following way: 然后按以下方式在您的代码中使用此代码段:

public void something() {
    long startTime = System.nanoTime();

    //SOME HEAVY COMPUTATIONS

    CProfile.slownessAlert(startTime, "something");
}

It will inform you about slowness when it happens (SECONDS_TO_ALERT>2s). 它会在发生速度缓慢时通知您(SECONDS_TO_ALERT> 2秒)。

如果它是一个Web应用程序,则将speed-tracer插件添加到eclipse中,它将帮助您找出所有时间。

A simple way to do this and guarantee you will get results is to add the following to the top of your main class. 确保并获得结果的一种简单方法是将以下内容添加到主类的顶部。 This code will save the app start time and then add JVM shutdown hook that will fire and print the duration when the JVM shutsdown. 这段代码将节省应用程序的启动时间,然后添加JVM关闭钩子,该钩子将在JVM关闭时触发并打印持续时间。

final static long startTime = System.currentTimeMillis();
static {
Runtime.getRuntime().addShutdownHook(new Thread() {
    public void run() {
        try {
            System.out.println("My program ran for "+( System.currentTimeMillis()-startTime )+" seconds.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM