How to accurately measure Java method's running time? -
this question has answer here:
- how time method's execution in java? 35 answers
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)
note other values not zeros (zoom in reveals it)
my questions:
- what main cause of these outliers (spikes)?
- 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
Post a Comment