Showing posts with label performance. Show all posts
Showing posts with label performance. Show all posts

Sunday, 26 February 2012

Android: optimize app loading time

No one likes to wait for an app to launch for more than several seconds. It is critical to optimize the app loading time.

My app loads a large 3D model at launching. It is a custom binary format consisting of vertex array and index array. The vertex format is interleaved since it is from an iOS app. Each vertex is 8 floats: 3 for position, 3 for normal, and 2 for texture coordinates.

It is not suitable for Android if you use Java instead of NDK. So I write a small Java program to convert the binary file so that there is a position array, a normal array and a texture coordinates array. When I load the mesh, I can read all positions to a byte array, wrap it as a byte buffer, use it as a float buffer, then copy it to a direct float buffer, which could be used by OpenGL ES. Same for normals and tex coords. This optimization speeds up the file I/O by 50%. Although it is still kind of slow (4 secs). Most of time is spent copying from the byte array wrapped as float buffer to the direct float buffer.

Since 4 seconds is still slow for launching an app, I ran the mesh loading code in another thread, and add a flag for mesh ready. If mesh is not ready, the render code will skip rendering the mesh. With this change, the app is very fast to load.


Friday, 24 February 2012

Android: Using Flurry to gather information

Flurry is a mobile app analytics service for many platforms including iOS and Android.

It is very easy to use. Just register an account, then create a new app. It will generate an app id for your new app, then you can download its SDK.

For Android, you just need to add an external Jar to your Eclipse project, then add severa lines of code to init and end Flurry sessions.  You can then log events in your app.

Some usages:


  1. log screen size and dpi, so that you will know distribution of screen sizes, and prioritize your layout effort
  2. log time spent in time-consuming procedures, to prioritize optimization effort
  3. log frame rate for OpenGL ES, so that you may exclude some slow device from your app
  4. log supported OpenGL ES version. If OpenGL ES 2.0 is supported by 90% devices, you may start working on it
  5. log pages viewed, to rank the pages

Timing in Java

Timing in Java is easy.

long start = System.nanoTime();

long time = System.nanoTime()-start;

The unit is nanosecond. The long type is enough for 300 years.