1

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 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

Community
  • 1
  • 1
user778900
  • 301
  • 3
  • 6
  • 11

5 Answers5

2

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;
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0
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

Community
  • 1
  • 1
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
0

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

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
0

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

Pratik
  • 30,639
  • 18
  • 84
  • 159
0

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

Carlos Quintanilla
  • 12,937
  • 3
  • 22
  • 25