Android: getElapsedCpuTime returns lesser milliseconds

99封情书 提交于 2019-12-11 10:47:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!