Android uses TraceView for performance testing

Source: Internet
Author: User

In Android development, it is often necessary to optimize the performance of the APK, but those methods are more cost-performance, and which are more memory-intensive, we are very difficult to know, fortunately, Google provides us with a tool, TraceView, the tool in the SDK Tools folder inside, As long as the environment variable is configured, it can be used directly.

TraceView is a visual tool for developers in Android to evaluate the performance of each method in the app.

I'll start by writing a simple activity, as follows:

 PackageCom.example.testtrace;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.os.Debug;ImportAndroid.view.Menu; Public  class mainactivity extends Activity {    @Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main); Debug.startmethodtracing ("haha");        Calcuteadd ();    Calcuteplus (); } Public void Calcuteadd() {intsum =0; for(inti =0; I <10000;        i++) {sum + = i; }    } Public void Calcuteplus() {intTotal =100000; for(inti =0; I < the;        i++) {Total-= i; }    }@Override    protected void OnStop() {//TODO auto-generated method stub        Super. OnStop ();    Debug.stopmethodtracing (); }@Override     Public Boolean Oncreateoptionsmenu(Menu menu) {Getmenuinflater (). Inflate (R.menu.main, menu);return true; }}

The code is simple enough to run the addition and subtraction methods separately. The careful yards may have found that I have run a method in the OnCreate method

Debug.startMethodTracing("haha");

A method was run in the OnStop method:

Debug.stopMethodTracing();

Yes, it is in the OnCreate method to open the data collection, in the OnStop method to stop the data collection. where "haha" is the name of the file saved by the collected data.

Install the app on SDcard, and then generate a haha.trace file on SDcard, then export the Haha.trace file and analyze it through TraceView in the Tools directory in the SDK.

We will first copy the "haha.trace" stored in the SDcard root directory to local and then execute "traceview d:/haha.trace", noting that you must first configure the TraceView environment variable. You'll see a panel like this:

On the left is the way we run: Each method has a parent and a children, where:
Parent: Indicates that the method called this method
Children: Indicates that the method called those methods
There are a lot of tabs on the right side of all the methods, and the meanings of their representatives are explained below:
1.Incl Cpu time%
The percentage of time that the method was executed during the run and the total time
2.Incl Cpu Time
The time it takes for the method to run itself
3.EXCL Cpu time%
Excluding sub-methods, that is, the percentage of time that you run the total time
4.EXCL Cpu Time
Do not count the time to call other methods in this method, so look at that method of consumption performance, mainly to see the value of this parameter
5.Incl Real time%
The percentage of the total time that the actual time of the change method itself runs
6.Incl Real Time
The time at which the method itself runs
7.EXCL Real time%
The amount of time that the method itself consumes the CPU and the percentage of time and total time that the method itself runs
8.calls+recurcalls/total
Total number of calls that were called during the method's run
9.cpu Time/call
The time it takes for the method to run during the run

Yes, you can. See the index of the various aspects of a CALCUTEADDF method that we wrote in activity:

OK, about TraceView learning is here, I hope you can enjoy.

Android uses TraceView for performance testing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.