Standard

WCF Error: Could Not Register URL

When hosting WCF services from Visual Studio you may, as I recently did, come across this error:

HTTP could not register URL http://localhost:12345/Service1.svc Your process does not have access rights to this namespace (see http://go.microsoft.com/fwlink/?LinkId=70353 for details).

The easy fix is to run Visual Studio as an administrator. The problem is you’ll just get this again if you don’t have the option to run your application as an administrator.

There is a great StackOverflow post on how to manually register the WCF service outside of Visual Studio (or the app you are building) in Windows 7.

This worked great for me until I went to deploy to a Windows 2003 Citrix server. In Windows 7 you use netsh to register the WCF service. Windows 2003 does not have netsh, instead using HttpCfg.exe. HttpCfg.exe is a pain to figure out. Don’t bother. Just use PaulWh’s HttpNamespaceManager tool. It worked great for me on Windows 2003, my WCF service was up in running in no time!

PaulWh’s HttpNamespace Manager Tool

Leave a Comment
Standard

Josephus’ Circle

Here is my solution to the latest code contest from The Daily WTF, Programming Praxis: Josephus’ Circle.

What I like about this solution is that it represents in code, how the problem is actually carried out in real life. The soldiers are counted off one by one.

Also, I just LOVE .Net generics!

static int Josephus(int numSoldiers, int soldiersToSkip)
{

     // create soldiers
     List<int> Soldiers = new List<int>();
     for (int i = 0; i < numSoldiers; i++)
     {
          Soldiers.Add(i);
     }

     // kill soldiers
     int iSoldier = 0;
     while (Soldiers.Count() > 1)
     {

          // skip ahead by the requested number of soldiers
          iSoldier = iSoldier + soldiersToSkip;

          // if we've gone over, then wrap back around to the top
          while (iSoldier > Soldiers.Count())
          {
               iSoldier -= Soldiers.Count();
          }

          // kill the choosen soldier
          Soldiers.RemoveAt(iSoldier - 1);
     }

     // return answer
     return Soldiers[0] - 1;

}
Leave a Comment
Standard

Performance Penatly of Recusive Calls

The recent post on The DailyWTF has brought in many comments with people providing code samples in many different languages. The post, Programming Praxis: Russian Peasant Multiplication, offers a simple coding challenge.

The challenge screams for recursion and many of the answers in the comments went this route. Of course, when we talk recursion we always have to qualify it as saying there is a performance penalty for using recursion.

Using this Russian Multiplication challenge as an example, I’ve done some testing to see how much the penalty is.

I have three versions of the code:

The first version is the solution I came up with before reading the comments in on the DailyWTF. It uses recursion inside of an enclosure class to ease readability.

public long RussianMultiplication(long val1, long val2)
{
     long val2_first = 0;
     while (val1 != 1)
     {
          // divide val1 by half until it reaches 1
          val1 = Num1(val1);

          // multiple val2 by 2 until
          val2 = val2 * 2;

          // record the first instance of val2 being multiplied
          if (val2_first == 0)
          {
               val2_first = val2;
          }
     }

     // multiple the first value of val2 and the last value of val2
     return val2_first + val2;
}

private long Num1(long val)
{
     if (val == 1)
     {
          return 1;
     }
     else
     {
          return Math.Abs(val / 2);
     }
}

The second version is the same as the above, but uses no recursion at all:

public long RussianMultiplication(long val1, long val2)
{

long val2_first = 0;
while (val1 != 1)
{

// divide val1 by half until it reaches 1
val1 = Math.Abs(val1 / 2);

// multiple val2 by 2 until
val2 = val2 * 2;

// record the first instance of val2 being multiplied
if (val2_first == 0)
{
val2_first = val2;
}

}

// multiple the first value of val2 and the last value of val2
return val2_first + val2;

}

And the final version is done with full recursion. This is simply the first full (C#) recursion example from the comments on DailyWTF:

public long RussianMultiplication(long val1, long val2)
{

if (val1 < 2)
return (val1 == 1 ? val2 : 0);
if (val1 % 2 == 1)
return val2 + RussianMultiplication(val1 / 2, val2 * 2);
return RussianMultiplication(val1 / 2, val2 * 2);

}

Let’s call the three examples Partial Recursion, No Recursion, and Full Recursion. To test them I put them each in a class in a C# console app and ran each through a loop having them multiple 1 x 999,999 through 999×999 x 1.

Over 5 runs, here are the averages:

Partial Recursion: 43 ms
No Recursion: 584 ms
Full Recursion: 218 ms

Using No Recursion as a baseline, that shows a performance increase, not a performance decrease! Full Recursion only takes 37% of the time as No Recursion. Partial Recursion only takes 7% of the time.

While I’m happy to see my solution as the quickest, I have to admit that I’m a little surprised by the results. I’m tempted to dig into this further with some code analyzers.

However, I think the point is that there is no hard and fast rule about recursion. You can not rule out recursion because of a fear of a “performance penalty”. There is nothing that can replace testing the code and seeing the results.

Leave a Comment
Standard

Springtime Haiku

The weather is finally warm! I was on a ride and looked down at my tires to see they were coated gray. The rollers had worn off on them! By the time I got back home the gray was gone and I had come up with this:

tires coated gray
the shame of my roller miles
spring beckons me out

Leave a Comment
Standard

Resume tip from Joel Spolsky

I’ve reviewed hundreds of resumes over the last few months and I’ve got to agree with Joel Spolsky’s latest resume tip:

When a startup CTO sees a resume that says things like:

  • Responsible for $30m line of business
  • Architected new ERP platform
  • Managed team of 25 developers
  • Optimized business processes

they think, “Spare me, that’s all we need, somebody running around trying to manage and optimize and architect when we just need someone who isn’t afraid to write code.” Here’s the stuff CTOs at startups want to see on a resume:

  • Single-handedly developed robust 100,000 LOC threadsafe C++ service
  • Contributes to OpenBSD file system in spare time
  • Wrote almost 75% of the Python code running IsIt2009Yet.Com

A great tip. It’s no coincidence that I’m posting our startup’s developer openings at jobs.joelonsoftware.com.

Leave a Comment
Standard

Gwinnett Cycling

Riding a bike in Georgia presents unique challenges. We’ve got narrow roads, surprisingly unfriendly drivers, and the temperature can range from 20 ° in the winter to 105 ° in the summer.

But I’ve been ridding out here a few years and I’ve learned to really enjoy it. It’s possible to commute year round, we’ve got the Dick Lane Velodrome for track racing, the Georgia Cup for road racing, and I’ve heard our mountain biking ain’t bad either.

My focus in this blog will be commuting from and around Gwinnett, cycling advocacy in Gwinnett and Georgia, family cycling, and other news that I think will be of interest to North Georgia cyclists.

It seems there are a lot more cyclists in Gwinnett and Atlanta then just last year. If you just got a bike and have a question about cycling, or want to try commuting but don’t know where to start, shoot me an email.

Leave a Comment