Toggle Trillian with Caps Lock (using AutoHotkey)

Behold the Caps Lock key, a vestige of a previous era and scourge to the modern user. I never use the Caps Lock key, and I get the impression that most users don’t. Yet it still becomes accidentally activated, producing bothersome effects.

AutoHotkey is capable of disabling the Caps Lock key. AutoHotkey is a tiny program that lets you assign behavior to keystrokes, mouse actions, and more. It is actually very powerful and can run extremely complex scripts, which makes it somewhat intimidating. For these reasons I avoided it, although Lifehacker frequently praised it, until I finally took the plunge. I’ll never go back.

SetCapsLockState, AlwaysOff

Disabling the Caps Lock key is a one liner. But that seems like such a waste of a key; can’t I do something useful with it? Yes I can!

SetCapsLockState, AlwaysOff

CapsLock::
  if (!capsDown)
  {
    capsDown := 1
    Process, Exist, trillian.exe
    if (%ErrorLevel% == 0)
    {
      Run, C:\Program Files (x86)\Trillian\trillian.exe
    }
    else
    {
      IfWinActive, ahk_class TSSHELLWND
        WinActivate, ahk_class Shell_TrayWnd
      IfWinExist, Trillian
        WinActivate
      else
        SendInput, +^{t}
    }
  }
return

CapsLock Up::
  capsDown := 0
  IfWinExist, Trillian
    SendInput, +^{t}
return

This script shows my Trillian contact list while I hold Caps Lock and hides it when I release the key. Now with one keystroke I can easily peek to see who is online and return to my work undisturbed. It even launches Trillian if it is not already running. I greatly prefer this to Trillian’s dock/autohide feature because I was always making Trillian pop open when I didn’t mean to.

If you want to use this script, be sure to set the run command to Trillian’s location on your computer, and assign the Hotkey Ctrl+Shift+T to the action “Contact List: Toggle Visible” (this setting is found within Automation in Trillian’s Preferences).

I’m sure this script could be adapted to toggle other programs and Instant Messengers.

Note: This works on three of my computers running Windows 7 and Trillian Astra, but I imagine it would work with Trillian 3 and other versions of Windows.

Sequentially Accessing a Rectangular Array

We know that .NET performs optimizations when accessing rectangular arrays, but for sequential access should the inner loop be on the first or second index? Is there even a difference?

The Code

using System;
class Program
{
    static void Main(string[] args)
    {
        int size = 512;
        int count = 1000;
        int[,] array = new int[size, size];
        int total = 0;
        var watch = System.Diagnostics.Stopwatch.StartNew();
        for (int i = 0; i < count; i++)
            for (int x = 0; x < size; x++)
                for (int y = 0; y < size; y++)
                    total += array[x, y];
        watch.Stop();
        Console.WriteLine(String.Format("Sequential access by [x,y]: {0}ms", watch.ElapsedMilliseconds));
        watch = System.Diagnostics.Stopwatch.StartNew();
        for (int i = 0; i < count; i++)
            for (int x = 0; x < size; x++)
                for (int y = 0; y < size; y++)
                    total += array[y, x];
        watch.Stop();
        Console.WriteLine(String.Format("Sequential access by [y,x]: {0}ms", watch.ElapsedMilliseconds));
        Console.WriteLine(total);
        Console.Read();
    }
}

 

The Output

Sequential access by [x,y]: 825ms
Sequential access by [y,x]: 2414ms
0

 

What a difference! Incrementing the first index in the inner loop takes almost 3 times longer, probably because there are so many more cache misses.

 

The Verdict

For best performance, process your rectangular arrays by incrementing the first index in an outer loop, and the second index in an inner loop.

Math.BigMul Exposed

Today a friend and I were reflecting through System.Math (courtesy of IronPython) and we noticed the BigMul method:

Math.BigMul(Int32, Int32) : Int64

Why have a method just for multiplication? It seems to be a trivial reason to add a method to the .NET framework. After all, multiplication with casting does the same thing:

(long)a * (long)b

Being optimistic, I suggested that perhaps Microsoft’s BigMul is implementing a faster and more efficient multiplication algorithm. Maybe there is a clever way to multiply two 32 bit numbers without explicit casting to 64 bit. Naturally, I wrote a simple speed test.

static void Main(string[] args)
{
    int a = 40993;
    int b = 69872;
    long c = 0;
    DateTime start;
    TimeSpan length;
    Console.WriteLine("Inline multiplication");
    start = DateTime.Now;
    for (int i = 0; i < 1000000000; i++)
        c = (long)a * (long)b;
    length = DateTime.Now - start;
    Console.WriteLine(c);
    Console.WriteLine(length.ToString());
    Console.WriteLine();
    Console.WriteLine("Math.BigMul");
    start = DateTime.Now;
    for (int i = 0; i < 1000000000; i++)
        c = Math.BigMul(a, b);
    length = DateTime.Now - start;
    Console.WriteLine(c);
    Console.WriteLine(length.ToString());
    Console.WriteLine();
    Console.Read();
}

The results were not encouraging.

Continue reading Math.BigMul Exposed

Debugging with Internet Explorer and Visual Studio

I prefer Firefox as my default web browser. For website development, Firefox with Firebug
is a killer combination. But when I test a page in Internet Explorer, I sometimes get cryptic Javascript errors that are impossible to track down. It turns out that Internet Explorer needs some coaxing to play nice with Visual Studio.

Enable Javascript Debugging
Open Internet Options and head over to the Advanced tab. Uncheck Disable script debugging (Internet Explorer).

debugging-ie-1

That’s all it takes; the next time you encounter a Javascript error, you will be prompted to debug. Select an instance of Visual Studio and you’ll have interactive debugging.

Launch with Internet Explorer
When you run or debug a website, Visual Studio uses you default system browser. But maybe you prefer to debug in a different browser. To change this setting, right-click on an aspx file in Solution Explorer and select Browse With. Select the desired browser and set it as default.

debugging-ie-2

That’s it! Do you have any tips or tools for website debugging?