• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 275
  • Last Modified:

"Timing" Calculation:

Hi all,

I need to perform some tasks to calculate the total time that needed to execute a specific method.

But I am totaly no idea what kind of packages that I should use. I have tried to look into java..util.* and the Timer inside is to let me to schedule something and not what I am looking for.


So, I am wondering anyone in here can tell me what package of what interface or what API's classes that I can use in order for me to do this.

I would be very happy if somebody can show me how to do.. ha....

But other than that i have some questions....

1. if I am putting all my code in one method, like this

public Resulset execute() {
//start of my timer
1. connect to db
2. Execute my squery
3. return result set.

//stop the timer.
//calculate the time


Then should be no problem right.

But what happend if I have separate my code into modular... like this...

public boolean getValid() {
 boolean test = executequery();
return test;

public boolean executequery() {
boolean ok=false;
//query statement;
//run the statement

if ok, return ok;

public boolean hasNex() {
if (rs.next()) {

Then how am i going to time the timer??
Is it I have to start to count from the getValid been called, and till hasNext() method been called?

Orrrrrrrrrrrrrrrrrrrrrrr, it's up to me to decide....

Friend, give me some idea. and also guidance....

Thanks in advance..
  • 8
  • 7
1 Solution
java.lang.System.currentTimeMillis() is your friend :)

long startTime = System.currentTimeMillis();
//do the task
long endTime = System.currentTimeMillis();
long duration = endTime - startTime; // in milli second

>> Then how am i going to time the timer??
it depend on which section of the task you want to measure.

>> Orrrrrrrrrrrrrrrrrrrrrrr, it's up to me to decide....
wjh7554Author Commented:

The start time I get is this,

and the endtime is this

Duration :

Question is , what is this number?? H.a....

104134075031, meaning what???
according to api doc
the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.

so your task tooks 1.016 secound.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

wjh7554Author Commented:

Thank you.
wjh7554Author Commented:
kennethxu, it's should be 10.16 and not 1.016.


DOne a very simple test against my count... hm... i think I am correct.

Anyway, thanks for your respond
it should be 1 second = 1000 milli second, try this code:

public class Timing {
     public static void main( String[] args ) throws Exception {
          long startTime = System.currentTimeMillis();
          Thread.sleep( 2000 );
          long endTime = System.currentTimeMillis();
          long duration = endTime - startTime; // in milli second
          System.out.println( duration );
wjh7554Author Commented:

i cn't convert the 1026 to 1.026 or 10.26 either...

I can only see 1.00 or 1 only...

I have tried again the numberformat and also the decimalformat...

let's solve this first ..

DecimalFormat nf = new DecimalFormat("##,###.00");
long duration = endTime - startTime;
double result = duration/100;

out.println("Duration :" +duration);
out.println("Result :" +nf.format(result));

Duration : 596
Result : 5.00


wjh7554Author Commented:
friend, what is this Thread.sleep(2000);??

the only changed is the duration become higher value. and I suspect the 2000 shoud be 1000, but after change it the figure doesn't look what I suppose to be getting..
>> double result = duration/100;
<< double result = duration/100.0;

>> Thread.sleep(2000);??
sleep 2000 milli second, I use this to illustrate you it is 2 second, not 20 second.
wjh7554Author Commented:

How come ??
you add .0 at behind only.

This is the normal trouble shoot method right? Ha... OK copy that.

Kennethxu, serious speaking.
startTime : 1046152354318
endTime   : 1046152355599
Duration  :          1281

So, if this is mili = 1000, then I have 1.28 seconds respond time which is imposible.

And if I said it's 12.8 seocnds, then it's logical because I am coounting when I click the refresh button.

Opps, I entered the counting in the JSP page

<%@ page language="java" import ="java.sql.*,
                      com.wrox.cars.*"  %>

<%@ page contentType="text/html" errorPage="handle_error.jsp" %>

<jsp:useBean id="zz" class="com.wrox.cars.zz" />          

long startTime=System.currentTimeMillis();

out.println("Start Time :"+startTime);

ResultSet rszz = zz.viewDoc();
int counter=0;

while (rszz.next()) {
//<% rszz.getString(ABC); %>


long endTime=System.currentTimeMillis();
out.println("EndTime :"+endTime);
long duration = endTime - startTime;

double result = duration/100.0;
DecimalFormat nf = new DecimalFormat("##,###.00");

out.println("Duration :" +duration);
out.println("Result :" +nf.format(result));



So, the logic no problem right??    
>> you add .0 at behind only.
according Java Language Specification, the result of integer devide integer is integer, any reminder is ignored.

>> So, the logic no problem right??      
your logic is correct, but it is double result = duration/1000.0;

1. when the first time you access a changed jsp page, server will have to compile your jsp page first, that takes long time on a slow machine.
2. from you hit refresh, browser initiate network connection, send request to server, the server parse the request and then call your jsp page, that takes time too.
3. after browser received the data from server, it again takes time to display it on your screen, it take a lot longer time when you have a big page.
those are all contribute to the delay between you hit refresh and see the page and not counted in the logic. In fact, there is no way to count them.

the result is telling you the your bottleneck is not at your jsp page, rather somewhere else.

how many rows are you trying to display in the page.
wjh7554Author Commented:
>>how many rows are you trying to display in the page.

consistantly 90 records.

If the item that you've mentioned above can't be count. Then it seems that my purpose to put in timing on counting can't be achieve.

Actually, we are planning to upgrade the server (AS400) capabilities on handling this JSP ans Java stuff.. But before that I need to gather some solid evidence on how slow is the respond time of each page have been called.

I agree with you regarding some of the factors that may cause the respond become higher.

Anyway, the solution that you suggested to me is to measure the timing in between, right?
So, that mean (refer to my code) it's counting the time spent to get connect, retrieve info and also close connection might take.

So, if 1012, it's only take 1.012 second (yes, divide by 1000). The actual situation need 10 seconds.

Ken, can give me some idea how to capture the exact timing that needed to display a pages, a jsp page (include the time neede in between the connectivitiy among browser and server and etc...)

>> Anyway, the solution that you suggested to me is to measure the timing in between, right?

>>So, that mean (refer to my code) it's counting the time spent to get connect, retrieve info and also close connection might take.
yes, if you mean databse connection.
I'm not sure if you are using connection pool. A real world application use connection pool, which will be much faster, milli-seconds vs seconds, then not using connection pool.

>>The actual situation need 10 seconds.
that's too much. except jsp re-compile that only happens once, other overhead shouldn't be any longer then 1 second. try to ping your server from client machine to see the response time to see if you have network problem.

>> can give me some idea how to capture the exact timing that needed to display a pages.
there is no way for server side program to detect http/network/broswer overhead. but there are some load/performance test tools can help you.
you can find a list of those tools: http://www.softwareqatest.com/qatweb1.html#LOAD
some are free, like TestMaker, Apache JMeter, those tools are not easy to use, I personally don't have much experience with them.
wjh7554Author Commented:
kennethxu, thanks a lot for your time.

I got what you mean already.


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now