• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regarding System.currentTimeMillis();

 
rajendar medishetty
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,
Iam trying print the application execution time using System.currentTimeMillis(); while entering and exiting as follows :

long before = System.currentTimeMillis();
chain.doFilter(request, response);
long after = System.currentTimeMillis();
config.getServletContext().log(" " + (after - before) + "ms");

The response times Iam getting are somewhat extra ordinary.........
they are in steps i mean to say values are as follows in milli seconds
0,15,16,31,32,46,47,62,63,78

These for different scenarios but u can observe there is exactly 15 ms difference.....and there is no times between 0 to 15 ms, 16 to 31 ms

I want is there any problem with System.currentTimeMillis();

thanks
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

The currentTimeMillis() routine has to be implemented in terms of whatever the underlying operating system makes available. You don't say what platform you're running on, but different ones report time to different resolutions. I know Windows used to have only a 50-millisecond resolution; I'm sure that's improved these days -- perhaps it's now 15.
 
rajendar medishetty
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Iam using windows only.....
but i didnot get u wat do u mean by resolution of 50 ms.

can u pls explain me.

thanks
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rajendar medishetty:
Hi

Iam using windows only.....
but i didnot get u wat do u mean by resolution of 50 ms.

can u pls explain me.

thanks


Do you remember "least count" in Physics? Same thing.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rajendar medishetty:
Iam using windows only.....
but i didnot get u wat do u mean by resolution of 50 ms.


You can simply say that it will show you the time after every 50 ms before that it will print the same as before. I hope you got it.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I got this. Didn't check it, though.


Operating systems granularity:
Windows 95/98: 50 milliseconds
Windows NT/2000: 10 milliseconds
Linux: 1 millisecond (can get finer resolution in some versions´┐Ż-
nanoseconds)


Here is the link to pdf
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As others have said the system clock is probably giving you time measurements in discrete chunks. I'm not sure why the values wouldn't be exactly 0,15,30,45 etc. This is one case where an average time can be more accurate. For example if you take 3 measurements: 0, 15, 0 then the average of 5 ms. is closer than any of the other measurements.

Being as you are performing your performance measurements on servlets you should check out the jamon servlet filter. You can do all that you are trying to do and more with no changes to your application (just add some lines to your web.xml).

See http://www.jamonapi.com
 
uj johansson
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i didnot get u wat do u mean by resolution of 50 ms.

The resolution is the smallest change that's detectable. Like on most watches you cannot measure smaller time frames than about seconds so the resolution would be 1 second in that case.

To get around this you can repeatedly call the method you want to measure in a loop say N times so the measured time gets well above the resolution. Then you divide this total time with N and you get the average time for one method call.
[ December 31, 2005: Message edited by: uj johansson ]
 
uj johansson
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also note that you should run time measurements repeatedly say 10 times in a row without restarting the program. It's because the JVM needs time to "warm up", that's to optimize the code. Only then do you get a realistic measure. Your method will run faster and faster up to a point where it stabilises.
[ December 31, 2005: Message edited by: uj johansson ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!