1

what i want to do is to find the the time consumed by firing a certain method so i am using System.currentTimeMillis() before and after the method then get the difference but, is there other way since getting the currentTimeMillis is also time consumable and i am working on increasing the performance of a certain protocol. here what i have did

public void someMethod
{
long x=System.currentTimeMillis();
... my bussiness
long y=System.currentTimeMillis();
int timeConsucmed =y-x;

}
Mohammed Falha
  • 967
  • 8
  • 22
  • That is a common approach. – Andrew Logvinov Jun 09 '13 at 06:31
  • Look at [this question](http://stackoverflow.com/questions/180158/how-do-i-time-a-methods-execution-in-java). The answers do have `System.currentTimeMillis()` and `System.nanoTime()`, but one of the questions mentions using a profiler, which is probably the better option for you if you want more accurate execution timing. – ajp15243 Jun 09 '13 at 06:32

3 Answers3

1

If you are doing for this for testing a single method use the method which you are using ..otherwise go for this library,If you are doing this multiple times.

Java™ Execution Time Measurement Library

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

I strongly recommend using codahale's metrics and use the timer functionality.

zengr
  • 38,346
  • 37
  • 130
  • 192
0

It doesn't cost much, at most 1-2 microsecond. If you want you can change it to

public void someMethod
{
   //my bussiness
}

from whereever you are calling

long x=System.currentTimeMillis();

someMethod();

long y=System.currentTimeMillis();
int timeConsucmed =y-x;
smttsp
  • 4,011
  • 3
  • 33
  • 62