How to accurately measure Java method's running time? -


this question has answer here:

i had report mean running time of java method. following way

//method start long starttime = system.nanotime(); //method body here ... long estimationtime = system.nanotime() - starttime;  //method end 

i run method many many times, log results , report mean , variance of running time in microseconds.

here problem got following values (very large spikes exist) enter image description here

note other values not zeros (zoom in reveals it) enter image description here

my questions:

  1. what main cause of these outliers (spikes)?
  2. how report running time accurately in case this?

take @ method body (for curious)

tracerecord resultrecord = new tracerecord();     resultrecord.settimestamp(timestamp);     resultrecord.setuserid(userid);      if (sensorvalue.getsensortype() == sensortype.wifi) {         arraylist<wifibasestation> wifiapsinrange = new arraylist<wifibasestation>();          (int = 0; < sensorvalue.getbasestationsidentifiers().length; i++) {              wifibasestation wifiap = new wifibasestation(sensorvalue.getrepresentativename(i),                     sensorvalue.getbasestationsidentifier(i));             wifiap.getsignalstrengthslist().add(sensorvalue.getsignalvalue(i));             wifiapsinrange.add(wifiap);         }         if (wifiapsinrange.size() > 0) {              double averagelong = 0;             double averagelat = 0;             int matchedcount = 0;              (wifibasestation bs : wifiapsinrange) {                 wifibasestation bsfromtable = wifiuniquewardrivingtable.get(bs.getmacaddress());                 if (bsfromtable != null) {                     gpslocation locationfromtable = bsfromtable.getbasestationlocationusingaverage();                     if (locationfromtable != null) {                         averagelong += locationfromtable.getlongitude();                         averagelat += locationfromtable.getlatitude();                         matchedcount++;                     }else{                         averagelong++;                         averagelong--;                     }                 }else{                     averagelong++;                     averagelong--;                 }              }              if (averagelong != 0) {                 averagelong /= matchedcount;             }             if (averagelat != 0) {                 averagelat /= matchedcount;             }              if (averagelat != 0 && averagelong != 0) {                 resultrecord.setlocationpoint(new gpslocation(averagelong, averagelat, 0));             } else {                 return null;             }         }      } 

the likeliest cause these spikes "stop world" breaks caused gc. there isn't can prevent happening. best try calling system.gc() once before taking first measurement.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -