Monthly Archives: March 2009

System.Linq – Unleash the power!

I’ve been poking about with the System.Linq namespace in C# 3.5 this week.  Like most “new” areas of a language, it can be difficult to find the time or motivation to delve into the capabilities it provides, but I’ve just had it proven to me that I should have taken the plunge months ago.

In one small project, I have to compare two generic lists and pull out the records that are equal across the sets, as well as those that are different from one set to another (additions and deletions).  Luckily, the sets are fairly small, so the implementation I wrote last week is good enough to get the job done.  This week, however, I decided to do a quick benchmark to see how much faster it would be to use the new Linq-y methods.

First up, I created a small test class – MyType.

public class MyType
{
	public int MyId { get; set; }
	public string GivenName { get; set; }
	public string FamilyName { get; set; }
	public DateTime DateOfBirth { get; set; }

	public override bool Equals(object obj)
	{
		if (null == obj) return false;
		MyType compareTo = obj as MyType;
		if (null == compareTo) return false;
		return (FamilyName == compareTo.FamilyName && GivenName == compareTo.GivenName  && DateOfBirth == compareTo.DateOfBirth);
	}
}

Next up, I created a comparer class, inheriting from IEqualityComparer<MyType>

public class MyTypeComparer : IEqualityComparer
{
	public bool Equals(MyType x, MyType y)
	{
		return (x.FamilyName == y.FamilyName  && x.GivenName == y.GivenName  && x.DateOfBirth == y.DateOfBirth);
	}

	public int GetHashCode(MyType obj)
	{
		return obj.GivenName.GetHashCode() ^ obj.FamilyName.GetHashCode() ^ obj.DateOfBirth.GetHashCode();
	}
}

Next we create two collections of 10,000 members each.  I did that using the following snippet.

private void SetupDataForTest(int numberOfRecords)
{
	recordSetOne = new List();
	recordSetTwo = new List();
	matches = new List();
	matches2 = new List();

	for (int i = 0; i < numberOfRecords; i++)
	{
		recordSetOne.Add(new MyType {MyId = i, GivenName="Trevor", FamilyName="Test" + i.ToString(), DateOfBirth = new DateTime(1970,1,1).AddDays(i)});

		if(i % 3 == 0)
		{
			recordSetTwo.Add(new MyType { MyId = i, GivenName = "Trevor", FamilyName = "Test" + i.ToString(), DateOfBirth = new DateTime(1970, 1, 1).AddDays(i) } );
		}
		else
		{
			recordSetTwo.Add(new MyType { MyId = i, GivenName="Ian", FamilyName="Wilson" + (i*2).ToString(), DateOfBirth = new DateTime(1971,1,1).AddDays(i*2) } );
		}
	}
}

So now we can get to the meat of the thing and find all the records that exist in both sets.  The old way of doing this was looping through the first set, and for each member loop through the second until we find a match (I’m not going to provide code – it’s too darned ugly).  Doing it this way took around 8 seconds on my dev machine.

The Linqy way, though is to use the Enumerable.Intersect method :

private void RunLinqTest()
{
	var intersection = recordSetOne.Intersect(recordSetTwo, new MyTypeComparer());
	matches.AddRange(intersection);
}

Guess what?  Adding all the matches to a pre-existing collection returned the expected 3334 results, and took 7 milliseconds.Yes, milliseconds.  That means I could run this method 1142 times in the time it took to run the old way.

I’m not going to go into the specifics of how to pull out the added/deleted records (but I’ll give you a clue -> Enumerable.Except), but the performance gains were enormous there, too.

Windows Azure

I’ve just got back from Devweek at the Barbican in London, and the last session I attended was something of a revelation.  You know what it’s like at a conference – it’s the last day, you’re ready to pack up and go home, but you feel you have to attend at least one more session.  In my case, that session was by Microsoft’s Eric Nelson  on Windows Azure (Developing and Deploying your first Cloud Service).

Like pretty much all windows developers these days, I’d heard of Azure and cloud computing, but my initial opinion was “Meh! Not for me”.  Then I attended this session.  There’s obviously been a lot of thought put into this by the teams at Microsoft and, although there’s still a way to go, there are some very compelling features.  I’m not going to go into any detail here (visit the website above for more details) but the standout feature for me is the fact that if you already have the knowledge to develop a web application, you can develop an Azure service. (Yes, there are a couple of gotchas around storage and so on but nothing that can’t be overcome with a little thought and learning)

I didn’t think I would, but I’ve just signed up for the CTP – if nothing else, I can play with the technology a little.  If you want to get in on the ground floor for this tuff, I’d suggest you do too.

(By the way, big thanks to Eric for a couple of great presentations this week.)

Squeezebox Boom

I’ve been a user of SlimDevices products for a good few years now.  I started off with a Slimp3 and don’t know how I’d be able to manage my music without it.  My spare bedroom has around 1000 CDs stashed away, and I don’t know about you  but I can’t be bothered sifting through them all if I want to listen to a specific song.  Having them all encoded as MP3 and available within seconds is great.

Anyway, I’d been toying with the idea of getting a Squeezebox Boom for quite a while now, and a recent promotion at Amazon meant I could get hold of one for only £129, £50 off the standard price.  Too good to miss.

Although the unit is pretty small, the sound it produces is almost too good to be believable.  Rich bass (doubly surprising from the 2” drivers), crisp high end and a generally tight and well controlled delivery. As you can probably tell, I’m impressed with the sound quality.

All the features you’d expect from this kind of networked music system are there – instant access to all the artists, albums of songs in your collection, random playlists, user-defined playlists and favourite track selection.  In addition, you can also synchronise all the players in your home.  That’s pretty cool if you’re having a party and want the same songs playing throughout the environment, and have them all exactly in sync.

If you’re in the market for a player like this, then I’d really recommend you consider the Squeezebox Boom.

More information : SlimDevices website : SlimDevices Boom Forum : Amazon.co.uk

Windows 7 on the MacBook

I’ve just installed Windows 7 on my MacBook’s BootCamp partition, and so far it looks great.  I’m not new to Win7, having had it installed in a Parallels VM since it was released.  The experience on the MacBook’s native hardware, though, is so much better.  For a start, it doesn’t die when you try to run (or create) a WPF application.  For a developer, that’s a pretty important thing. :)

One niggle, though, I can’t get audio.  I know there’s a fix, but after inserting my Leopard DVD, the drive has stopped responding.  Now I have to boot back into OS X in order to eject the disc.

Oh well, I’m going back to playing with this OS, and seeing what it can really do.

…with Bells On

I don’t know if this is just a clever marketing pitch, or if this is genuinely worthy of the attention, but I’m blogging about it anyway.

Morris : A Life With Bells On looks like it could possibly be the next massive British film to come out of left field.  It has pretty much all the elements of a great story.  And morris dancing.  I know – it sounds awful, but if you head over to the site and watch the trailer, maybe, just maybe you’ll agree with me that it looks great.

Unfortunately, the release isn’t quite what the makers hope.  It seems to be available only in a select few cinemas mostly around the south-west.  There’s a petition on the site, though, hopefully to get a nationwide release.

I’ll tell you one more thing – if I am being caught up in some kind of elaborate hoax and this film doesn’t actually exist outside the trailer, then it damn well should.

*UPDATE*

Come on, distributors!  Someone take a chance on this film and get it out nationwide.  Stop being so afraid of “niche”!