Category Archives: Uncategorized

Faith blog

I decided today that I could do with somewhere to dump random thoughts about my faith in the same way that I dump random thoughts about computing here. Clearly this isn’t the right place to do it, so I’ve set up a Faith Blog with Blogger. Some of you may be interested in it. Don’t let it bother you if not. This is the last you’ll hear about it unless there’s some topic which directly affects both areas.

Vista and External Memory Devices

Update – read the first two comments. I'm leaving the rest of the article as it is in order to avoid revisionism. The solution is in the first two comments though.

According to the Windows Vista feature page, Vista is going to be able to use external memory devices (USB flash drives and the like to you and me) to act as extra memory to save having to go to the hard disk. I've heard this mentioned at a few places, and it's always asserted that EMDs are slower than memory but "much, much faster" than disks. This has just been stated as a fact that everyone would just go along with. I've been a bit skeptical myself, so I thought I'd write a couple of very simple benchmarks. I emphasise the fact that they're very simple because it could well be that I'm missing something very important.

Here are three classes. Writer just writes out however many blocks of 1MB data you ask it to, to whichever file you ask it to. Reader simply reads a whole file in 1MB chunks. RandomReader reads however many 1MB chunks you ask it to, seeking randomly within the file between each read.

Writer

using System;
using System.IO;

public class Writer
{
    static void Main(string[] args)
    {
        Random rng = new Random();
        
        byte[] buffer = new byte[1024*1024];
        
        DateTime start = DateTime.Now;
        using (FileStream stream = new FileStream (args[0], FileMode.Create))
        {
            for (int i=0; i < int.Parse(args[1]); i++)
            {
                rng.NextBytes(buffer);
                Console.Write(".");
                stream.Write(buffer, 0, buffer.Length);
            }
        }
        DateTime end = DateTime.Now;
        Console.WriteLine();
        Console.WriteLine (end-start);
    }
}

Reader

using System;
using System.IO;

public class Reader
{
    static void Main(string[] args)
    {
        byte[] buffer = new byte[1024*1024];
        
        DateTime start = DateTime.Now;
        int total=0;
        using (FileStream stream = new FileStream (args[0], FileMode.Open))
        {
            int read;
            while ( (read=stream.Read (buffer, 0, buffer.Length)) > 0)
            {
                total += read;
                Console.Write(".");
            }
        }
        DateTime end = DateTime.Now;
        Console.WriteLine();
        Console.WriteLine (end-start);
        Console.WriteLine (total);
    }
}

RandomReader

using System;
using System.IO;

public class RandomReader
{
    static void Main(string[] args)
    {
        byte[] buffer = new byte[1024*1024];
        
        Random rng = new Random();
        DateTime start = DateTime.Now;
        int total=0;
        using (FileStream stream = new FileStream (args[0], FileMode.Open))
        {
            int length = (int) stream.Length;
            for (int i=0; i < int.Parse(args[1]); i++)
            {
                stream.Position = rng.Next(length-buffer.Length);                
                total += stream.Read (buffer, 0, buffer.Length);
                Console.Write(".");
            }
        }
        DateTime end = DateTime.Now;
        Console.WriteLine();
        Console.WriteLine (end-start);
        Console.WriteLine (total);
    }
}

I have five devices I can test: a 128MB Creative Muvo (USB), a 1GB PNY USB flash drive, a Viking 512MB SD card, my laptop hard disk (fairly standard 60GB Hitachi drive) and a LaCie 150GB USB hard disk. (All USB devices are USB 2.0.) The results are below. This is pretty rough and ready – I was more interested in the orders of magnitude than exact figures, hence the low precision given. All figures are in MB/s.

Drive Write Stream read Random read
Internal HDD 17.8 24 22
External HDD 14 20 22
SD card 2.3 7 8.3
1GB USB stick 3.3 10 10
128MB USB stick 1.9 2.9 3.5

Where possible, I tried to reduce the effects of caching by mixing the tests up, so I never ran two tests on the same location in succession. Some of the random reads will almost certainly have overlapped each other within a test, which I assume is the reason for some of the tests showing faster seek+read than streaming reads.

So, what's wrong with this picture? Why does MS claim that flash memory is much faster than hard disks, when my flash drives appear to be much slower than my laptop and external drives? (Note that laptop disks aren't noted for their speed, and I don't have a particularly fancy one.) It doesn't appear to be the USB bus – the external hard disk is fine. The 1GB stick and the SD card are both pretty new, although admittedly cheap. I doubt that either of them are worse quality than the majority of flash drives in the hands of the general public now, and I don't expect the average speed to radically increase between now and the Vista launch, in terms of what people actually own.

I know my tests don't accurately mimic how data will be accessed by Vista – but how is it so far out? I don't believe MS would have invested what must have been a substantial amount of resource into this feature without conducting rather more accurate benchmarks than my crude ones. I'm sure I'm missing something big, but what is it? And if flash can genuinely work so much faster than hard disks, why do flash cards perform so badly in simple file copying etc?

Worst product names ever?

A while ago, I decided that EasyMock.NET wasn’t quite up to scratch, and I was going to try to write a replacement. I’m not doing much .NET development at the moment, so it’s not an issue any more (and when I go back to .NET I’ll look at Rhino Mocks which sounds promising). However, I did get as far as picking a name for the new project. I settled on PowerMock in the end, but went through a pun worthy of Simon Tatham first…

Firstly, it would have to start with “N” wouldn’t it? NUnit, NCover etc… Well, what sounds a bit like mocking something, and starts with “N”. How about “NSult”, to be pronounced “insult”?

Next, suppose we wanted a new unit test system at the same time. Maybe something like TestNG but for .NET. Something to judge your code and decide whether it was good or not. Ooh, that sounds a bit like a court-room drama. How about “NJury”?

Of course, they work best when you put them together – adding NSult to NJury…

Sorry. I’ll go to bed now, promise.

ICloneable? Not quite…

I don’t usually post personal news on my blog, but this is fairly major. Some of you may know that my wife, Holly, is pregnant. What you won’t know – and what we didn’t know until today – was that we’re having twins. Eek! A lovely surprise, if somewhat scary. For those of you who like piccies, the scans are on my web site…

Now if you’ll excuse me, I think I’ll go and lie down. And to think I was going to write up C# 2.0 features tonight…

Nice doc comment idea

I’ve just been reading the
transcript of a whiteboard session with Anders Hejlsberg
and one of the questions
is really, really good:

Question: My problem is I’ve got these XML doc comments
that are duplicated. I just strip off one. I guess it would be a neat
language feature to be able to somehow indicate this is my primary- my
big method, right? With all the parameters. Then the other ones are
just going to borrow that XML doc comment.

Hejlsberg: Yes, okay. Now that I think is- that’s not a bad idea.
That yes, they should be able to share the documentation. I can sympathize
with that.

That’s not just “not a bad idea”. That’s a fantastic idea. When I was
writing the BitConverter and BinaryReader etc equivalents
in my miscellaneous utility library
the doc comments for the overloads took significantly longer to write than the actual code.
(Most of the code was just each overload calling a “master” routine.) Now, sometimes
that comment won’t be exactly the same for each overload; sometimes there’ll effectively
be placeholders: “Converts the specified ${type} value into ${n} bytes” or whatever. I don’t
know exactly how this could be done elegantly (and I’m not actually suggesting the ${token} syntax!), but it’s something that should be strongly
considered for a later version of C#. It could make life a lot simpler in some cases.

What kind of deadlock prevention do you want?

Okay, so I’m having another look at the alternative threading ideas
which are part of my threading article. (They’re not that big an
alternative really – not compared with CSP etc – they’d just make
things more pleasant.) I want to add deadlock prevention to my locks,
making it impossible to lock things incorrectly (so long as you’re
locking the simple way – if you lock the associated monitor
independently, that’s your own lookout). Obviously this requires you to
set up what’s correct and what’s incorrect to start with. My question
to you all is: how do you want to be able to set up those
rules? What kind of rules do you need? Do they need to be extensible
somehow? Some desirable things may be impossible, but I’d like to know
what the ideal would look like before working out the realistic. I have
a couple of pretty simple ideas, but I won’t taint your own views by
mentioning them yet…

RSS for my articles

A friend suggested on Sunday that I create an RSS feed for my articles,
so that interested parties would know when I’ve created a new one – so
I’ve done it.
It’s hand-crafted, so there may be a few wrinkles to iron out, but
hopefully it will prove useful. It would be nice if there was a way of
specifying the last time that an item had been updated (rather than the
original publishing date). I can’t see anything like that in the spec but maybe I’ve missed something. The feed works in Thunderbird at least – let me know if you have problems with it.