Wednesday, August 25, 2010

Myers Briggs Type Indicator ENTJ Executive Fieldmarshal

I just took the Myers Briggs Type Indicator assessment and discovered that I'm an ENTJ. This is how my score pairs panned out:

E=18 & I=3

S=7 & N=15

T=28 & F=0

J=24 & P=5

Without reading any further about what an ENTJ personality type is I can only assume that we do not have a problem exposing our scores or writing about our type, otherwise I wouldn't be doing this.

In reading what wikipedia has to say about this I discovered the Keirsey Temperaments which classifies me as a Fieldmarshal. The personality page calls me The Executive. I'm now starting to feel pretty full of myself and without reading what The Executive is all about I give it to my wife to read to me.

She slows down, reads extra loud, and repeats some of the passages:

"...they are not naturally tuned in to people's feelings, and more than likely don't believe that they should tailor their judgments in consideration for people's feelings. ENTJs, like many types, have difficulty seeing things from outside their own perspective. Unlike other types, ENTJs naturally have little patience with people who do not see things the same way as the ENTJ..."

"...sentiments are very powerful to the ENTJ, although they will likely hide it from general knowledge, believing the feelings to be a weakness. Because the world of feelings and values is not where the ENTJ naturally functions, they may sometimes make value judgments and hold onto submerged emotions which are ill-founded and inappropriate, and will cause them problems..."

I read that other ENTJs include Margaret Thatcher and Bill Gates.

Wednesday, August 11, 2010

C# Script to list local users and disabled status on Windows Servers

The following script will list all local users and their disabled status on each of the windows servers that you put into the "servers" array variable. This code is written in C# and you will need to include the System.DirectoryServices assembly.


using System;
using System.DirectoryServices;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] servers = { "ServerName1", "ServerName2", "ServerName3" };
            DirectoryEntry de = new DirectoryEntry();

            foreach (string server in servers)
            {
                de.Path = "WinNT://" + server + ",computer";

                Console.WriteLine("Server: " + server);
                foreach (DirectoryEntry d in de.Children)
                {
                    if (d.SchemaClassName == "User")
                    {
                        int userFlags = (int)d.Properties["UserFlags"].Value;
                        bool disabled = (userFlags & 2) == 2;
                        string name = (string)d.Properties["Name"].Value;
                        Console.WriteLine(name + " (" + disabled + ")");
                    }
                }
                Console.WriteLine();
            }
           
            Console.ReadLine();
        }
    }
}
 

Thursday, July 22, 2010

Factorial Function in C#

 The Factorial Function is a classic interview question. It opens the doors to a discussion about stack overflow (if done recursively) and integer overflow (if the param is too large) as well as discussions around how to handle and catch errors.

Here is one way to do it using recursion:

static Int64 Factorial(int factor)
{
    if (factor > 1)
    {
        return factor * Factorial(--factor);
    }
    return 1;
}

Here is another way to do it using a loop: 

static Int64 Factorial(int factor)
{
    Int64 factorial = 1;
    for (int i = 1; i <= factor; i++)
    {
        factorial *= i;
    }
    return factorial;
}

Neither function does any sanity checks for input (e.g. >= 1) and assumes that we're looking for a factorial of 1 or greater.

There's a third and much better way to do factorials if you are going to use them in code that needs to be optimized. In fact I can't think of a reason why you wouldn't want to use the following method. If you are calculating an Int64 factorial then the maximum factor that you can calculate it for is 20. After that it will overflow the Int64 type. If you are calculating an Int32 factorial then the highest you can go is 12. 

static Int64[] factors64 = { 0, 1, 2, 6, 24, 120, 720, 5040, 40320,
362880, 3628800, 39916800, 479001600, 6227020800, 87178291200,
1307674368000, 20922789888000, 355687428096000, 6402373705728000,
121645100408832000, 2432902008176640000 };
 
static Int64 Factorial(int factor)
{
    return factors64[factor];
}

The tests that I ran showed the recursive factorial function to run 14 times slower than the array lookup and the loop to run 5 times slower.

Friday, July 9, 2010

Google AdSense Best Day Of Week

I've just been doing some Google AdSense analysis for some web sites that have moderate traffic. The owner said that I can publish this bit of information from her site. She's interested to hear if other AdSense publishers have had similar results.

The objective was to find out which day of the week the Cost Per Click (CPC) was the highest. So we ran the averages across a number of pages and the average CPC by day of week ranked the days in order from worst to best as follows:

  1. Monday
  2. Sunday
  3. Thursday
  4. Friday
  5. Wednesday
  6. Tuesday
  7. Saturday

Before running the analysis she was pretty certain that Tuesday was the best day of the week and was surprised to see that Saturday was in fact the best.

Monday, July 5, 2010

Parallel For and ForEach in .Net 4

I've started looking at the Parallel.For and Parallel.ForEach methods from .Net 4's Task Parallel Library. Before I use something as complex and potentially dangerous as this I like to test it out to make sure I'm going to get that performance boost before going down that path.




Here's the console app that I wrote to do the test:

static void Main(string[] args)
{
    int[] someInts = Enumerable.Range(0, 100).ToArray();

    Stopwatch timer = Stopwatch.StartNew();
    foreach (int item in someInts)
    {
        DoItem(item);
    }
    Console.WriteLine("Sequential: {0}", timer.Elapsed);

    timer.Restart();
    Parallel.ForEach(someInts, item => DoItem(item));
    Console.WriteLine("Parallel: {0}", timer.Elapsed);

    Console.ReadKey();
}

static void DoItem(int item)
{
    Random r = new Random((int)DateTime.Now.Ticks);
    for (int i = 0; i < (item * 100000); i++)
    {
        int j = r.Next(0, 10000);
        j = j * j;
    }
}


I ran this a number of times on a quad core machine (Intel Core2 Quad Q9400 @ 2.66GHz) on Windows 7 x64. Average time for the sequential loop was 14.7 seconds and for the parallel loop was 5.6 seconds for a ratio of 2.6 times faster using the parallel loop. This is seems right., you're going to get a loop like this to be about Core-1 times faster because the thread management will "cost" you one of your cores. So on an 8 core machine expect this loop to be 7 times faster and on a quad expect 3 times which is what I almost got.


Saturday, July 3, 2010

CREATE FILE encountered operating system error 5 Access is denied

I have an MSSQL 2008 R2 installation and was trying to attach a DB from a non-R2 installation using SQL Server Management Studio (SSMS) and I was getting the following error:

CREATE FILE encountered operating system error 5(Access is denied.) while attempting to open or create the physical file 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\MyDataBase.mdf'. (Microsoft SQL Server, Error: 5123)

Found plenty of Google results pointing me to suggestions about SQL Server 2000 and 2005 with instructions on how to add user permissions and which user account needed to be added.

Turned out that all I needed to do was to run SSMS as an administrator and the attach worked.

Tuesday, June 29, 2010

Rocket Surgery Made Easy by Steve Krug

I just finished reading Rocket Surgery Made Easy by Steve Krug and recommend you read it.

Javascript: The Missing Manual

It's sub-titled The Do-It-Yourself Guide to Finding and Fixing Usability Problems.

It's a great read. He writes well, has appropriate sarcasm and wit at the right places, and delivers everything you need to know about usability testing. I bet that even usability professionals will find information in there that's of use even though it's not targeted at them. He also gets extra points for quoting Douglas Adams of Hitchhiker's Guide fame: "I love deadlines, I love the whooshing noise they make as they go by."

Right at the beginning of the book he gives you a link to an online video of someone taking a usability test and him guiding them through the test. This would really suck if you didn't have internet access close to you when you were reading the book so watch the usability video as soon as you get the book and don't even wait to get to that part.

One of the cool things that I learned (and I have no idea why I didn't think this would exist) is that you can outsource your usability testing and there are sites on the web that will do it for you. This is a no-brainer if you don't have the resources to set this up yourself and it sounds like a reasonable price to pay for the information that you'll get out of it. Just point the candidate at your site (or your beta site) and let them at it.

I thought he'd listed several online usability sites but while paging back through the book I can only find www.usertesting.com.