Calorie Buddy

Calorie Buddy is the latest of my apps released during summer of this year. Following on from the series of apps Training Buddy and Weight Buddy, Calorie Buddy is another app aimed at helping you keep track of your general health and fitness.

As the name suggests Calorie Buddy is all about monitoring your calorie intake. Simply set a target, enter your consumption each day and a handy pie chart will show you how many calories you have available left to eat that day. To navigate between different days simply swipe to the left or right.

One of Calorie Buddy’s best features though is its live tile that enables you to see your remaining calories that day without even opening the app!

Download Calorie Buddy today

Advertisements

Back to basics string vs StringBuilder

This is simple stuff but is something I see people easily miss by just not thinking about it.

A string is an immutable object, which means once created it can not be altered. So if you want to do a replace or append some more text to the end a new object will be created.

A StringBuilder however is a buffer of characters that can be altered without the need for a new object to be created.

In the majority of situations a string is a perfectly reasonable choice and creating an extra 1 or 2 objects when you appened a couple of other strings isn’t going to make a significant impact on the performance of your program. But what happens when you are using strings in a loop.

A few weeks ago one of my developers had written some code that went through a loop building up some text. It looked a little like this:

string foo = "";

foreach (string baa in someSortOfList)
{
    foo += " Value for " + baa + " is: ";

    var aValue = from x in anotherList
                 where x.name == baa
                 select x;

    foo += aValue.FirstOrDefault().value;
}

Everything worked apart from the fact it took 30seconds to execute!

He was searching through convinced that the linq expressions in the middle was what was taking the time, and was at the point of deciding it could not go any faster without a new approach.

I pointed out not only had he used strings rather than a StringBuilder, but the loop also created around 10 string objects within it. The loop which repeated a couple thousand times was therefore creating 20000 objects that weren’t needed. After we switched froms strings to a StringBuilders the loop executed in milliseconds.

So remember when your trying to work out why your code may be slow, remember the basic stuff.