Race between the If statement and the Delegate Swapping/Substitution Pattern

Which is faster delegate swapping or simple if statements? I wanted to know too and it turns out it’s a tie, as far as I can see. The idea is that you might have some code you want to  execute until some condition occurs then execute some different code. This might be a run once thing or a run until X is true. Either way you could use an if statement. However, many times I prefer to use a delegate, I think it looks better and the code is easier to read. Anyway, I created a small program to test this and here are the results:races

10000:10000 calls using Delegates averaging 0.005937576 seconds.
10000:10000 calls using Ifs       averaging 0.005312568 seconds.
5000:10000 calls using Delegates averaging 0.005156316 seconds.
5000:10000 calls using Ifs       averaging 0.005156316 seconds.
2:10000 calls using Delegates averaging 0.005000064 seconds.
2:10000 calls using Ifs       averaging 0.005156316 seconds.
1:10000 calls using Delegates averaging 0.005000064 seconds.
1:10000 calls using Ifs       averaging 0.005156316 seconds.

I declare a tie because different runs of the program yield slightly different results even though the run times are averaged over 100 runs. The first number is the number of times the TaskOne method is called, the second number is the total number of times the run method was called. Each set was run 100 times for the averaging seconds number.

Pseudo code for the Ifs method:

public void Ifs()
{
if (!TaskOneIsComplete())
TaskOne();
TaskTwo();
}

Pseudo code for the Delegates method:

public void Delegates()
{
_executeMe();
}

private void InitialDelegate()
{
TaskOne();
TaskTwo();

if (TaskOneIsComplete())
_executeMe = AfterDelegate;
}

private void AfterDelegate()
{
TaskTwo();
}

DelegateSubstitutionVSIfStatements.cs (2.96 KB)


Giving up on boo for the AIM bot due to incomplete Generics support.

Well after getting an internal compiler error and trying to find out what the problems was, I decided to see if I could recreate the issue. In doing that, I wanted to know how to set constraints on generic types, and as it turns out this is still in progress as detailed in this post. Oh well, I guess having the AIM bot code in c# will readable by more people anyway. 

I really enjoy working in boo and still have more of the language to explore including Syntactic Macros and Generators.


Using the Prototype Javascript Library to Fix my CSS Layout

I made some tweaks to my blog layout specifically the columns and added a footer. The footer is where the problem started. With CSS using float for the side columns works great and the footer will fall below the main content as long as the main content column is taller then the floating columns. If not, the floating column will float over the footer. To fix this I used the prototype{#hm8i} javascript library to adjust the height of the main column if the floating column is taller. I added a MadKast{#e12e} size constant because the MadKast script will usually load after the height adjustment leaving the main content column short about 25 pixels. Anyway, here’s the code:

<script language="javascript">
<!--
_madKastSize = 25;

Event.observe(window, "load", function() {
if ($('left') && $('left').offsetHeight > $('right').offsetHeight)
$('right').setStyle({height:($('right').offsetTop + $('left').offsetHeight + _madKastSize) + "px"});
});
// --> </script>
</p>

Building an AIM Bot in .Net using boo

I’ve been working on a AIM Bot using the aimcc sdk. After signing up and downloading the sdk from developer.aim.com I found the documentation for .net lacking and found just getting up and running to be a long process. Below are some of the issues I ran into.

**Application.Run()</p>

</b>To start with I needed to create a class which extends Control. Then add a method to start the application, at the beginning of the method call CreateControl() at the end of the method you need to call Application.Run() to setup the message loop. I’m working in boo but this should work fine in c#.

class Bot(Control):
def Start():
CreateControl()
#AccSession setup to go here
Application.Run()

PInvoke

Next pinvoke is required to creat the AccSession instance. This is in some of the SDK samples, but it wasn’t obvious looking at the java and c++ sample bots. It only calls acccore.dll, but other dll’s in the accsdk/dist/release/ are needed I’m just using the full path, but should copy the required dll’s to my project file.



    [DllImport(“C:\accsdk\dist\release\acccore.dll”, EntryPoint:“#111”, PreserveSig:false, SetLastError:true)] 
    private static def AccCreateSession(
    [MarshalAs(UnmanagedType.LPStruct)] riid as Guid,
    [MarshalAs(UnmanagedType.IDispatch)] ref session as object) as IntPtr:
        pass

Calling the method is pretty strait forward, just pass in an object and then cast the object to a AccSession.


CreateControl()
o as object
AccCreateSession(typeof(IAccSession).GUID, o)
session = o as AccSession

</pre>

Login

The next step was to wire up the events I wanted to get catch, I choose to wire up all the events for logging. You need to provide your API key and then login.


handler = AccSessionEventHandler(BoxesImResponder())
handler.WireSession(</em>session)
session.ClientInfo.Property[AccClientInfoProp.AccClientInfoProp_Description] = key
session.Identity = username
session.SignOn(password)
Application.Run()

That’s finally all I needed to get the bot running. I fought with setting preferences for a long while like the java example shows, but I ended up following the c++ example to filter which messages get accepted. More to follow in later posts. In addition, I plan on posting the full source code when the application is working.

</p>

Implied generic parameter types

I’m not sure of the exact name of this feature, but I stumbled across it today. If a generic method uses the type for a parameter, the complier assumes the generic type, which makes for cleaner code.

using System;

class Program
{
static void Main()
{
MyClass.WriteInputStatic("test");
MyClass obj = new MyClass();
obj.WriteInput(12);
}
}

public class MyClass
{
public static void WriteInputStatic<T>(T input) { Console.WriteLine(input); }
public void WriteInput<T>(T input) { Console.WriteLine(input); }
}

I can’t think of many use cases, but kind of a cool feature anyway. I was using it to serialize and deserialize a type for testing the correct serialization/deserialization.

internal static T Serialize<T>(T worker) where T : class
{
    MemoryStream serializationStream = new MemoryStream();
new BinaryFormatter().Serialize(serializationStream, worker);
byte[] bytes = serializationStream.ToArray();

MemoryStream deserializationStream = new MemoryStream(bytes);
object obj = new BinaryFormatter().Deserialize(deserializationStream);
return obj as T;
}
</p>

Instant wiki with rBuilder MediaWiki Appliance

The MediaWiki Appliance is

An appliance that bundles up PHP, MySQL, Apache and MediaWiki to provide a self-contained, turn-key Wiki appliance.</p>

http://www.rpath.org/rbuilder/project/vehera-base/

Setup was easy and quick and it runs in Virtual Server and Virtual PC 2007.


Overriding component parameters when using Castle.MicroKernel

Overriding component parameters when using the Castle.MicorKernel turned out to be kinda weird. Anyway, here’s one way to do it.

IoC.Container.Kernel.RemoveComponent("ComponentA");
IoC.Container.Kernel.AddComponentInstance(
"ComponentA",
typeof (ComponentA),
new ComponentA("Customer parameter for testing."));

I needed to override the parameter value of ComponentA for some unit tests. The parameter is normally set in the configuration file. After trying a few different approaches, I ended up with the code above. It’s more of an end to end unit test, and as such ComponentA will be resolved as a result of another Component being resolved.


Brutalist Framework