Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regarding System.currentTimeMillis();

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Arthur, where are your pants? Check under this tiny ad.
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic