问题
Android documentation states about getElapsedCpuTime() that
Returns elapsed milliseconds of the time this process has run.
I tried using getElapsedCpuTime(), the time in milliseconds is quite confusing. After many (4 to 5) seconds, the elapsed time returned is just 2000 something. Either it is slower than original milliseconds (1000 in 1 sec) or I am having issue in proper understanding?
I'm simply testing to get current cpu elapsed time on each button click, here's the code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.build_data_view);
Button btn = (Button) findViewById(R.id.refresh);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String elapsedCpuTime = android.os.Process.getElapsedCpuTime()
+ "";
((TextView) findViewById(R.id.build_data))
.setText(elapsedCpuTime);
}
});
}
回答1:
in the name it sugest the time counted is the CPU time the process has spent so far. If you´ve started the process half an hour ago it may have just spent three seconds of CPU time. In the API http://developer.android.com/reference/android/os/Process.html they say it is the time the process is beeing running, effective time must be. So only the time spent in CPU and not the time ellapsed since process instantiation.
Add an Asynctask
doing nothing but while(true)
and you´ll see the time spent growing linearly but not as 100% of the ellapsed time because your proc will only have its share.
来源:https://stackoverflow.com/questions/30342369/android-getelapsedcputime-returns-lesser-milliseconds