04-"Time Detector" of "performance optimization"

Source: Internet
Author: User

"Time Detector"The World martial arts, only fast not broken. Many companies believe in this dogma. The app's compaction cycle can be squeezed to the minimum, which leads to a lot of problems in the development, a bit of experience with the engineer sloppy optimization, and worse, the inexperienced engineers will not even optimize the app. In some ways, performance optimizations can be overlooked in your development process. Ten years ago, the hardware resources of mobile devices were very limited. Even floating-point numbers are banned. Because floating-point numbers can cause the code to become larger and slower to calculate. With the rapid development of science and technology, hardware can largely compensate for the software's short board. Now the mobile device 3D hardware processing efficiency is even comparable to the PC, but you can not always rely on hardware and processor speed to cover up your app to do more garbage. (What if Android is running on the iphone as smooth as iOS?) The concept of performance is very well drawn, so we have to rely on the graphical output of the data. You may spend a week optimizing an interesting algorithm, but this algorithm only accounts for 0.5% of the total execution time, No matter how much energy you spend trying to optimize it, no one will notice. Instead of a for loop that takes 90% of the time, you can improve the efficiency by slightly modifying it by 10%, which is a simple modification that can I got a lot of goodwill from everyone. Because. The first thing they feel when they run the app is that they're much faster than before. No one's going to care what you're modifying is a much better algorithm, or a simple for loop.What does this mean?   Instead of taking the time to optimize small details, it's better to find the place where you changed the optimization .  the first tool "Time Event Viewer" (your own fictional name, English-time Profiler),——— He can measure time intervals, interrupt program execution, Keep track of each thread's stack. You can imagine that when you press the pause screen when Xcode is debugging   For example, 100 samples are doing 1 millisecond intervals, and then there are 10 samples at the top of a method stack, you can figure out that the approximate time is 10% 10 milliseconds spent in this method, This is an approximate value .  nonsense less say, time is a detected.   Select Product-profile from the Xcode menu, or select the program to start instruments, you will see a selection window   This is instruments all the test instrument panel, select "Timer Profilter "Click Profile" back to Qidong simulator and app, you will be asked to enter the password once so that instruments can have permission to intercept the process. In the tool window, you can see the time count and leave a small arrow moving to the right of the graphic above the center of the screen. This indicates that the application is running.   Now start running the app and search for some pictures, and you find that finding a result is too slow, and the Search results list page is too much to put up with.   First, make sure that the view in the toolbar selects all of the three options selected, as shown below:   This will ensure that all panels are open. Now, study the explanations below and each of the sections below it: 1. Recording button. The middle of the red button will stop with the startup it is clicked on when the application is currently being analyzed. Note that this is actually stopping and starting the application instead of pausing it.   2. Run the timer and run the navigation, the timer shows how long the app has been running, and the arrows can be moved between them. If you stop and then use the Record button to restart the application, this will start a new run. The display will display "Run2 of 2", you can go back to the first run of data, first you stop the current run, and then press the left arrow back.   3. Run the track.  4. The expansion panel, in the case of a time-sniffing instrument, is used to track the display stack.   5. Panel in detail. It shows the main information about the instrument you are using, which is the most frequently used department, from which you can see the time    6 the CPU is running. Options panel, described later. The   plays come.   DelvePerform an image search and delve into the results. I personally like to look for "dog", of course you can also choose any content you want. Like a cat, a beautiful girl, something. Now scroll up and down the list, let the time detector measure the data, and pay attention to the changes and values of the screen. These values reflect the CPU cycle. But you may find that the following values are too many to see your dazzling. The following configuration options are described below, which opens the call tree on the left and then follows the following configuration:separate by thread: each thread should be considered separately. Only in this way can you pull out the heavy threads that consume a lot of CPU Invert call Tree: From the top down the trace stack, which means that the method you see in the table will have been sampled from frame No. 0, which is usually what you want, so you can see the most time-out method in the CPU. That is, funca{funb{func}} Tick the stack with c->b-a to show the deepest C at the outermost level of the call Hide Missing Symbols: If dSYM can't find your app or system frame, you can't see the method name in the table, only 16 binary values are visible, and if this item can hide the symbols, it's easy to simplify the data Hide System Libraries: Check this to show your app's code, which is very useful. Because usually you only care about the time the CPU spends on its own code, not on the system . Show Obj-c only: Show OC code only, if your program is a program like OpenGL, do not tick sideways because he might be C + + Flatten recursion: recursive function, one entry per stack traceTop Functions: The sum of the time that a function spends directly in the function, and the total time it takes for the function to call the function. Therefore, if function A calls B, then A's time is reported in a time spent plus B. It's really useful to spend the time, because it allows you to pick the maximum number of times each time you go down to the call stack, zeroing in on your most-time-consuming method. If you have enabled the above options, although some values may be slightly different, the order of the following results should be similar to the following table: Through the above you can see that most of the time spent in updating the form of photos. Double-click on this line and you will see the following then this is interesting, isn't it! Almost three-fourths of the time spent in Setphoto: methods are spent creating image data for photos! Now you can see what the problem is, the NSData's Datawithcontentsofurl method does not return immediately, because to go to the data from the Internet, each call takes up to a few seconds to return, and this method runs in the main thread, it is conceivable what results. In fact, in order to solve this problem, the class provides a Imagecache method for the background asynchronous download. Now you can switch to Xcode and manually locate the file, but the instrument has a handy "open Xcode" button right in front of you. Find its panel just above the code and click it: Want to modify the following
  1. -(void) Setphoto: (Flickrphoto *) Photo {
  2. _photo = photo;
  3. Self.textLabel.text = Photo.title;f
  4. NSData *imagedata = [NSData Datawithcontentsofurl:_photo.thumbnailurl];
  5. Self.imageView.image = [UIImage imagewithdata:imagedata];
  6. [[Imagecache Sharedinstance] Downloadimageaturl:_photo.thumbnailurl
  7. completionhandler:^ (UIImage *image) {
  8. Self.imageView.image = image;
  9. [Self setneedslayout];
  10. }];
  11. }
Modify the application Product-profile (or cmd-Remember that these shortcuts will really save you some time) after the instrument is re-run. Please note that this time will ask again if you are using together. This is because you also have a window to open this program, and the instrument assumes you want to run again with the same options. Perform some more searches, and note that the user interface is not so much at this time! These images are now loaded asynchronously and cached in the background, so once they have been downloaded once they do not have to be downloaded again. It looks great! Is it time to release it? Of course it's not.

04-"Time Detector" of "performance optimization"

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.