At the end of the business day,customer satisfaction and user experience that matters for a good quality application.Metro and Windows 8 has been designed specifically for improving user experience.When it comes to metro specifically compared to traditional desktop apps,UI and performance concern a lot.There has always been a trade-off between UX designers,developers who develop complex UI and program with the performance.

Your metro style app needs to be tweaked in both UI and also in code in order to improve performance aspects which matters a lot to user.User can’t sit before the tablet/laptop for hours to let the app start or to let the app respond to input. UI and performance needs to be at a blazing speed.

I will list few of the tweaks that can be done on any metro style app to improve its performance.

1.Hey App,Please Stay Responsive…Don’t be like Spouse ,be like Girl Friend (just for fun)

As apps evolve, users generally expect them to remain responsive while handling more computation regardless of the type of machine. Even though ,app does lot of calculations as like in game,AI or any real-time scenario,users need to feel that its still respond to us very well.

XAML apps are event driven and all components share the same thread.Whether it is framework code for UI (eg. running layout, processing input, firing events, and so on) or an app’s code for UI all are executed on the same thread.That means,if some part of our app logic takes long time to execute then it will say other parts of app,”Hey hold on,I will finish my job first”.This will enable one piece of code to run at a time.

For this to resolve,use Background threads.Move all your intense calculations to background thread ,so that it will perform at the back and will let you(UI) know once it gets finished.

Another important aspect is Programming with async patterns.To help keep your app responsive, the platform provides asynchronous versions of many of its APIs. An asynchronous API executes in the background and returns a result to the calling thread. When calling an API from the UI thread, use the asynchronous version if it's available. For more info on programming with async patterns, see Quickstart: using the await operator for asynchronous programming in the Developer Center.

2.Accessing FileSystem

The file system is a common source of performance issues. Accessing files can be expensive due to disk latency and memory/CPU cycles to store the data.If you are about to access property values on large collection of files use Windows.Storage.BulkAccess APIs.

Traditional way :

   1: StorageFolder library = Windows.Storage.KnownFolders.PicturesLibrary; 
   2:  
   3: IReadOnlyList<StorageFile> files = await library.GetFilesAsync(Windows.Storage.Search.CommonFileQuery.OrderByDate); 
   4:  
   5: for (int i = 0; i < files.Count; i++) 
   6:  
   7: { 
   8:  
   9: // do something with the name of each file 
  10:  
  11: files[i].Name 
  12:  
  13: } 

Optimized way :

   1: StorageFolder library = Windows.Storage.KnownFolders.PicturesLibrary; 
   2:  
   3: StorageFileQueryResult fileQuery = library.CreateFileQuery(Windows.Storage.Search.CommonFileQuery.OrderByDate); 
   4:  
   5: uint itemSize = 100; // determine an item size 
   6:  
   7: bool delayLoad = true; // depends on if/when/how fast you want your thumbnails 
   8:  
   9: FileInformationFactory fileInfoFactory = new FileInformationFactory(fileQuery, Windows.Storage.FileProperties.ThumbnailMode.PicturesView, itemSize, Windows.Storage.FileProperties.ThumbnailOptions.ReturnOnlyIfCached, delayLoad); 
  10:  
  11: IReadOnlyList<FileInformation> files = await fileInfoFactory.GetFilesAsync() ; 
  12:  
  13: for (int i = 0; i < files.Count; i++) 
  14:  
  15: { 
  16:  
  17: // do something with the date the image was taken 
  18:  
  19: files[i].ImageProperties.DateTaken; 
  20:  
  21: // perf gains increase with the number of extra properties that are accessed 
  22:  
  23: } 
  24:  

3. Interop Concern-(Problem of being yourself)

The Windows Runtime allows developers to write XAML apps in their language of choice thanks to the projections of the WinRT APIs available in each language.But,this has some performance overhead due to which your app may slow down.If your apps get slow down because of this,try to avoid calling WinRT APIs at the hot code path of your project.For example, a game engine that is doing tons of physics calculations by constantly querying the position and dimensions of UIElements can save a lot of time by storing the necessary info from UIElements to local variables, doing calculations on these cached values, and assigning the end result back to the UIElements after the calculations are done.

For example,if you are heavily using collections in your program(C# or VB),try to use collections from the System.Collections namespace, rather than a collection from the Windows.Foundation.Collections namespace.

4.Code Execution Optimizations for C# and Visual Basic

Dot-Net developers knew well that our managed app runs over Common Language Runtime(CLR) and JIT-Just In Time compiler will come and pick module whenever necessary(like our boss,calling us )

But , as a metro developer here is a slight change in the code execution environment.

  1. When your app is first installed onto a user’s machine, it is executed using the CLR’s just-in-time (JIT) compiler. This means that the first time a method is executed it has to wait to be compiled.
  2. Later, a pre-compilation service (NGEN) pre-compiles all of the modules that have been loaded on a user’s machine.
  3. This typically runs within 24 hours of installation, but it could take longer depending on the usage patterns of the machine.
  4. Once NGEN runs certain portions of your program,those portions need not to be JIT compiled which will appreciably improve startup performance of application.

If you want to measure the startup performance of your app, then measure with JIT and with NGEN. You can force NGEN by first running your app, and then invoking the pre-compilation task using this command line:

   1: c:\Windows\Microsoft.Net\framework\v4.0.30319\ngentask.exe /runtimewide

This task will pre-compile any apps which were used and are missing NGEN images, and may take a long time the first time it is run.

I will post few more optimizations in the next part soon(Going to take a lunch now).See you soon...