Skip to main content

Posts

Showing posts with the label Performance

Performance and Memory tips

Two things play a very significant role in any application development, and those are application’s footprint and performance. Whenever I’m asked to work on these two tracks, I used to visit a huge list of sites to get many more ideas apart from what I already know. So, I thought, why can’t I collate all the good points and add them to my repository. At the same time, I thought of sharing those points in this blog. Rather than making this blog post full of theory, I’m planning to make it simple by just adding the bullet points. Uhh! Enough of gossip. Let’s get started by going through some common and important rules. Create object only when it is really required More the objects lesser the performance :(  Grab resources, use them and release at the earliest Default capacity of StringBuilder is 16. So, if you want to store less than 16 values then make sure to set the capacity. Avoid unnecessary boxing and unboxing Prefer lazy loading Use Static variables cautiously

Better way to play sound file on WPF button click

Recently I was working on a XAML based application, in which my requirement was to play a sound (.wav file) whenever a given button is clicked. So, to achieve this, I wrote a below snippet: My above snippet worked but at the same time, I feel a noticeable delay in beep sound and that make me analyze further and write a blog post :) Now question is, why there is delay between button click and sound ??? Well, there is a simple concept behind it, which I missed while implementing above requirement :( Reason is, the event hierarchy.  Most of us might be aware that Click is a bubbling event, which means event will be fired from the control who initiated it. So, in our case, whenever button is clicked, it bubbles from button to window. And that's the reason, click event handler is executing before the window event is triggered, which is ultimately leading to delay. Now, how to handle this??? Method 1: Then I thought to write a preview event, which will be fired

Troubleshooting data binding

We all know that DataBinding is one of the most powerful concept of WPF. So, today I thought to write something on how to troubleshoot data binding related issues while working with any XAML based application. Here I'll not talk about what and how data binding works, instead I'll jump directly on the relevant part. So, let's start by picking up the troubleshooting methods which can make developer's work bit easy. Way 1: Using Visual Studio output window Visual Studio provides high level information about binding which is sufficient to resolve very small problems like name mismatch, etc. Let's understand this along with a code snippet: <Grid> <TextBlock Text= " {Binding ElementName=label, Path=Hello, Mode=OneWay}" /> <Label Content= " Welcome" Name= " label" /> </Grid> Now open your output window and press F5, application will launch. In output window, you will notice that the message

Windows Phone App' s Tip - Image consideration

Images play a very significant role in any Windows Phone application. So, one should be very careful while dealing with images. As of now, Windows Phone supports only two image formats named JPG/JPEG and PNG. Now before concluding on which image format to choose, let's get into bit more depth of it. PNG format images are non-lossy and need very little CPU effort to display because those are pixel perfect. But at the same time, huge PNG images take too much longer to read from storage and ultimately lead to slower display. On the other hand, JPEG format images are lossy, smaller to store  and based on the compression level much more complicated decoding algorithm is required to display them. Another point regarding image is about opacity and transparency -  All the images that use transparency should be stored as PNG format because JPEG doesn’t support transparency and JPEG format should be used for all the images that are fully opaque. Now when coming to Windows Ph

Performance analysis for String and StringBuilder

Sometimes small-small changes in our code really makes a huge difference to a performance. There are many tips and tricks available and among those, one I am going to discuss over here. I'll be talking about  String vs StringBuilder. One needs to be very careful while playing with strings because memory wise there is a huge impact of strings. I  know, there are lots and lots of articles available on net on String and StringBuilder, but still I am going to show this, using some statistics. Here I am taking Fx 4.0 C# console application with different static methods to showcase my analysis.  Basically what I am doing here is, I am having a String variable named  outputString  and just looping that for 1000 times and concating the string to variable outputString.  Please note, concatenation is done using + symbol. So, what happens internally is, whenever concatenation is done using + symbol, every time, new String object is created. So, as with my snippet. Here I am  lo