开发者

calculation of execution time [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Measure execution time for a Java method

I have an application by which i need to send a large number of sms with in a particulate time limit.Sending sms is done. Now i need to calculate how much time it is actually taking to complete the execution.Can some one help me by giving some code of calculating the execution time.What is the procedure to fetch system time.And my program will get to know that execution is complete.How i will开发者_Python百科 calculate the difference between starting time and ending time of sending sms.Because when sms is complete my program may still be executing. Thanks Koushik


You want to take the time before and after doing the work. You may want the time in second.

long start = System.nanoTime();
// do work
long time = System.nanoTime() - start;
double timeInSeconds = time/1e9;


long start = Syatem.nanoTime();
//executing
long executionTime = System.nanoTime() - start;

This code snippet can be found in the nanoTime() docs
See this question for converting the nanoseconds to other units


Take a look at Spring's StopWatch. There are others out there like it, too.


hi this is the timer coder which was like start counting timer from 00:00:00 to

private long time = 1;
private int hour = 0;
private int min = 0;
private int sec = 0;
private Handler handler = new Handler();
private Runnable counter = new Runnable(){

    @Override
    public void run() {
        if(start){
            time += 1;

            if(sec==60){
                if(min==60){
                    min = 0;

                    hour++;
                }else
                    min++;

                sec = 0;
            }else
                sec++;


            System.out.println((hour+":"+min+":"+sec));
            handler.postDelayed(counter, 1000);
        }
    }

};

you need to start the runnable like this way ok when you start sending then this line of code add it

handler.postDelayed(counter, 1000);

after 1 sec it will start the clock


Here is a stopwatch class for java. This formats the time output as .NET's Stopwatch class

http://carlosqt.blogspot.com/2011/05/stopwatch-class-for-java.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜