Thursday, October 30, 2008

The difference between Java and Javascript

 I recently posted the following question on Stack Overflow: What's the difference between Javascript and Java? and received some great answers.

Greg Hewgill's answer of "Java and Javascript are similar like Car and Carpet are similar" was spot on and amusing. Shog9's answer was hillarious.

The other answers citing the differences listed a few I hadn't thought of but essentially there's almost nothing to tie these two languages together but for part of a word and some curly braces.

But that's not what this is about. What I'm finding fascinating about Stack Overflow is asking questions that I already know the answer to and discovering a whole bunch of answers and opinions that I hadn't thought of and further broadening my knowledge. There're some very clever people hanging out there - I wish I was one of them.

Thursday, October 23, 2008

Running VPN on a VM

Just been doing some testing on running VPN on a VM and on the host OS. The host in this case is Server 2008 x64 and the VM software is Hyper-V and the guest VM is Vista x64.

As expected, the OS running VPN takes on the IP address of the VPN while host or guest will continue to use the original IP that you were previously connected to. Obviously the VPN is running over your original IP to get to the VPN but to the outside world this is the IP that you're using.

Wednesday, October 22, 2008

String Reverse in C#

 I had to write a string reverse function today and wasn't happy with my initial (basic) function so I posted it on Stack Overflow to see what other algorithms that developers might come up with. My restriction was that it had to be in C# 2.0 and so I couldn't use LINQ. Out of curiosity I decided to do a performance test on each of the functions and while I was at it I threw in the LINQ equivalent to see how it would perform. For the test of a 500 character string I looped each function 100,000 times and for the test of a 10 character string I looped the functions 10,000,000 times.
These are the functions that I tested:
// string concatenation with for loop
public string ReverseA(string text)
{
    char[] cArray = text.ToCharArray();
    string reverse = String.Empty;
    for (int i = cArray.Length - 1; i > -1; i--)
    {
        reverse += cArray[i];
    }
    return reverse;
}

// Array.Reverse function
public string ReverseB(string text)
{
    char[] charArray = text.ToCharArray();
    Array.Reverse(charArray);
    return new string(charArray);
}

// push/pop Stack<>
public string ReverseC(string text)
{
    Stack resultStack = new Stack();
    foreach (char c in text)
    {
        resultStack.Push(c);
    }

    StringBuilder sb = new StringBuilder();
    while (resultStack.Count > 0)
    {
        sb.Append(resultStack.Pop());
    }
    return sb.ToString();
}

// LINQ
public string ReverseD(string text)
{
    return new string(text.ToCharArray().Reverse().ToArray());
}

// StringBuilder
public string ReverseE(string text)
{
    char[] cArray = text.ToCharArray();
    StringBuilder reverse = new StringBuilder();
    for (int i = cArray.Length - 1; i > -1; i--)
    {
        reverse.Append(cArray[i]);
    }
    return reverse.ToString();
}


 


What surprised me was how poorley LINQ did on the short string compared to the others.
You may also find the rudimentary test harness that I put together interesting:
delegate string ReverseDelegate(string text);

public void RunPerformance()
{
    int loopCount = 10000000;
    int stringSize = 100;
    string[] reverseTextArray = Enumerable.Repeat("abcde", stringSize).ToArray();
    string reverseText = String.Join("", reverseTextArray);
    // reverseText = "1234567890";
    DateTime startTime, endTime;
   
    ReverseDelegate[] reverseFunc = new ReverseDelegate[5];
    reverseFunc[0] = ReverseA;
    reverseFunc[1] = ReverseB;
    reverseFunc[2] = ReverseC;
    reverseFunc[3] = ReverseD;
    reverseFunc[4] = ReverseE;

    AssertThatFunctionsWorkCorrectly(reverseFunc);

    TimeSpan[] reverseTime = new TimeSpan[reverseFunc.Length];

    for (int i = 0; i < reverseFunc.Length; i++)
    {
        startTime = DateTime.Now;
        for (int j = 0; j < loopCount; j++)
        {
            reverseFunc[i](reverseText);
        }
        endTime = DateTime.Now;
        reverseTime[i] = endTime - startTime;
        Console.WriteLine("{0}/{2} took {1}", i, reverseTime[i], reverseFunc[i].Method);
    }

    Console.WriteLine("break point");
}

private void AssertThatFunctionsWorkCorrectly(ReverseDelegate[] reverseFunc)
{
    string test = "abcdefghi";
    string expected = "ihgfedcba";
    for(int i=0; i

Monday, October 20, 2008

Phoenix Startup Weekend Sunday 19 Oct 2008

The last day was good. We had more direction about what we wanted to achieve in that day and what it would take to achieve it. The team is very good and it did not appear that we took a lot of effort to make it come together. The prototype that we put together worked and played the player. The puppet master pulled the strings and web sites updated, text messages were sent and answer machines responded with the appropriate distress calls.

Dan, a Microsoft rep played the game towards the late afternoon and apart from some minor glitches which were nothing to do with our technology he succeeded in playing the game and accomplishing the mission which was do defuse a fake bomb (actually an iPhone). He also gave us a glowing recommendation at the sum-up at the end. The audience also had plenty of questions which was a good sign.

Sunday, October 19, 2008

Phoenix Startup Weekend Saturday 18 Oct 2008

The group started with a strategic meeting about where we wanted to get to and also the expanse and potential of the Alternate Reality Game project. There were a lot of good ideas and the potential for this product could be huge. Monetizing it without the player being offended appears to be fairly easy inasmuch as the oportunities for doing such appear wide spread.

Problems faced during the day:

  • The network at Gangplank (our hosts) was very slow. This could have been because the amount of traffic was not what it was set-up for.
  • The four developers on the team were split between the Java world and .NET worlds meaning that two of the developers could not work to their full potential.
  • The marketers/implementers were more focused on what the potential of the product could be and should have probably spent more time looking at what we can produce in the span of 1.5 days.

These were minor set backs and I hope that we get a working prototype finished by tomorrow evening.

Saturday, October 18, 2008

Phoenix Startup Weekend Friday Night 17 Oct 2008

The Phoenix Startup Weekend kicked off to a good start on Friday evening. Pizza and beer wer served. About 30 ideas were pitched.

I pitched two which got 3 and 2 votes respectively and were therefore dropped from the ideas list. The first idea was a web managed Scavenger Hunt which I didn't even vote for myself because it was very similar to the group that I eventually joined which is an Alternate Reality Game project. The second idea that I pitched was for a web based list learning system which I still think has a lot of potential but I don't think that I pitched it very well.

Of the 30 or so projects pitched about 6 to 8 of them went on to have groups formed to discuss their potential. One of the interesting fallouts from this stage of development was the importance of having developers buy into your idea. There was a small group of people who were very enthusiastic about their project but there were no developers that had joined that group. I believe that the decided to disband and redistribute.

After joining my group It was immediately obvious that there were a lot of very inteligent, motivated and highly talented people around me. This weekend promises to be at the very least a fantastic education.

Sunday, October 12, 2008

.NET Directory Notes

The documentation for the XmlWriter.Create Method (string) function has ArgumentNullException as the only exception that will be thrown for this function. I've found that System.IO.DirectoryNotFoundException can also be thrown here.

The AppDomain.CurrentDomain.BaseDirectory does not terminate with a back slash if you are running a unit test - you need to add this if you're going to append further directories. To make the using of AppDomain.CurrentDomain.BaseDirectory work in both unit tests and production code you might want to create a property to access the AppDomain.CurrentDomain.BaseDirectory that looks something like this:

string SafeBaseDirectory
{
            get
            {
                return AppDomain.CurrentDomain.BaseDirectory.EndsWith(@"\") ? 
                                AppDomain.CurrentDomain.BaseDirectory :
                                AppDomain.CurrentDomain.BaseDirectory + @"\";
            }
}