Thursday, January 29, 2009

WTF is “Sticky Keys”?

When I grew up with  computers sticky keys was what happened when I accidentally spilled Coke over the keyboard. Not so much anymore.

When running my laptop on battery the screen will dim after 60 seconds or so to save power, which works great when doing work. When watching YouTube videos it’s annoying. My solution is to click the shift key every now and then in order to prevent the dimming and not sending an unwanted keystroke which could maybe stop the playing.

Then it happens… in the middle of watching the video “Sticky Keys” appear. I click ‘ESC’ and the window disappears. During lunch today we started talking about this so I’ve checked it out.

If you click SHIFT 5 times in a row you can turn off sticky keys. This will enable a user to use SHIFT, CTRL, ALT or the Windows key by pressing one key at a time, alas working with one hand. Superb for people smoking or texting on their mobile with one hand while doing computer work with the other. Personally I’m not able to multitask and have now turned off the “Sticky Keys” (Control Panel –> Ease of Access Center –> Set up Sticky Keys).

I have stopped drinking soda with sugar several years ago so “Sticky Keys” is now ancient history – until I install a new computer that is.

Monday, January 26, 2009

C# Javascript library

Came across this piece in the January edition of MSDN Magazine. I talks about creating javascript for AJAX apps using C# and Visual Studio. Sounds too good to be true :) The library can be fetched at projects.nikhilk.net/ScriptSharp

I know I will take a look at it the next time a project requires some nifty scripting.

Wednesday, January 14, 2009

Fast byte array comparison in C#

I got into a discussion with a colleague the other day about string comparison in .Net and whether to use

variable.Equals("mystring")

or

"string" == "string"

both in terms of speed (though it wouldn’t matter in most cases) and in terms of readability. As for speed .Equals is faster as you save one method call. == is implemented as an operator which again calls Equals. Our good friend Reflector is always there when you need him.


The interesting part came when reflecting this and I stumbled upon EqualsHelper and CompareOrdinalHelper. Here .Net casts the strings to pointer arrays and compares an int at a time. This lead me to creating a byte[] comparison function after the same code .Net used internally and benchmarking it.


For an equal array with 11 elements the unsafe is 3 times as fast. For unequal arrays the managed implementation is quicker if the first or second byte differs. From the third on and out the unsafe gains speed. Below is some sample code you can experiment with yourself. The longer the array, the more you gain on the unsafe version. Be sure to test the code compile in release mode.


Microsoft don’t recommend you using unsafe unless it’s performance critical, but since they use it internally we can as well ;) (But you should have a good reason due to complexity imo) Why they compare 10 bytes at a time is beyond me and I haven’t tested if this is some magic number which yields good results for general cases.


class Program
{
static void Main(string[] args)
{
byte[] a = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
byte[] b = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };

Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < 30000000; i++)
{
SafeEquals(a, b);
}
sw.Stop();
Console.WriteLine(sw.Elapsed);

sw = new Stopwatch();
sw.Start();
for (int i = 0; i < 30000000; i++)
{
UnSafeEquals(a, b);
}
sw.Stop();
Console.WriteLine(sw.Elapsed);
}

private static bool SafeEquals(byte[] strA, byte[] strB)
{
int length = strA.Length;
if (length != strB.Length)
{
return false;
}
for (int i = 0; i < length; i++)
{
if( strA[i] != strB[i] ) return false;
}
return true;
}

[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
private static unsafe bool UnSafeEquals(byte[] strA, byte[] strB)
{
int length = strA.Length;
if (length != strB.Length)
{
return false;
}
fixed (byte* str = strA)
{
byte* chPtr = str;
fixed (byte* str2 = strB)
{
byte* chPtr2 = str2;
byte* chPtr3 = chPtr;
byte* chPtr4 = chPtr2;
while (length >= 10)
{
if ((((*(((int*)chPtr3)) != *(((int*)chPtr4))) || (*(((int*)(chPtr3 + 2))) != *(((int*)(chPtr4 + 2))))) || ((*(((int*)(chPtr3 + 4))) != *(((int*)(chPtr4 + 4)))) || (*(((int*)(chPtr3 + 6))) != *(((int*)(chPtr4 + 6)))))) || (*(((int*)(chPtr3 + 8))) != *(((int*)(chPtr4 + 8)))))
{
break;
}
chPtr3 += 10;
chPtr4 += 10;
length -= 10;
}
while (length > 0)
{
if (*(((int*)chPtr3)) != *(((int*)chPtr4)))
{
break;
}
chPtr3 += 2;
chPtr4 += 2;
length -= 2;
}
return (length <= 0);
}
}
}
}

Windows 7 beta - issues

Here's my first comments and experiences with Windows 7.

I downloaded Windows 7 this weekend and did an upgrade of a 32bit Vista sp1 laptop. The upgrade itself took 4 hours which is ok since I have a lot of stuff installed. The system itself behaves much more snappy than Vista. Startup and shutdown is quicker, hibernation is quicker, basically the OS response time is much better.

Issues which I had after the upgrade:

  • Only one cpu core was working. This was due to incorrect ACPI driver being used. Resolution was to disable multi core in the bios and let Windows redetect the cpu. Then shutdown and enable the second core again. This made Windows enable both cores.
  • Daemon tools is not working (SPTD driver)
  • Had to do a repair install on Acrobat Reader 9 and Java in order for them to work in IE 8
  • My built-in Broadcom network card is not working properly (BCM5906M) It won't access the LAN at work and get an IP address. Have to debug a bit more to find out more about the issue.
  • Toshiba bluetooth device is not working properly. Vista drivers won't work and Windows built-in driver are just as crap as Vista. Meaning basically not supporting much at all.

I'll report more issues when they appear, but for now I give two thumbs up for this release!