Because… PostScript!

Reading Time: 2 minutes

Because it’s become somewhat of a tradition for me to do something silly on a Friday lunchtime, I thought I’d take on one of The Department‘s basic coding challenges.

The brief was simple;

I want a program that will print out the numbers 1,2,3,4,5,6,7,8,9 in a shuffled order. The order must be different each time the program runs. Note that the same number must be different each time. It should be possible to extend this to work with 52 numbers, in which case I can make a shuffled deck of cards.
You can use the Random number generator in C#, but you must make sure that the same number never appears twice, as a deck of cards which contains more than 4 aces has been known to raise suspicion.

As it was Friday lunchtime however I decided to make the solution anything but simple, firstly I replaced the numbers with the actual card names and secondly I thought I’d write it in PostScript because it demonstrates a totally different form of notation from the way we write the normal imperative languages like C# or Java.

/Suits [(Clubs)(Diamonds)(Hearts)(Spades)] def
/Cards [(Ace)(Two)(Three)(Four)(Five)(Six)(Seven)(Eight)(Nine)(Ten)(Jack)(Queen)(King)] def
/YCursorMax 720 def % 10 inches from bottom
/YCursor YCursorMax def 
/XCursorMin 72 def % 1 inch from the left
/XCursor XCursorMin def
/XColWidth 113 def % 1/4 of the printable page
/Helvetica findfont
12 scalefont 
setfont
/Deck [ 0 1 51 {} for ] def
0 1 50 {
    /SwapLeft exch def
    52 SwapLeft sub realtime rand mul exch mod
    SwapLeft add /SwapRight exch def
    Deck SwapLeft get
    Deck SwapRight get
    Deck exch SwapLeft exch put
    Deck exch SwapRight exch put
} for
0 1 3 {
    dup /Col exch def
    0 1 12 {
        Col 13 mul add Deck exch get
        dup 13 mod
        XCursor YCursor moveto
        Cards exch get show
        5 0 rmoveto
        (of) show
        5 0 rmoveto
        13 div cvi
        Suits exch get show
        /YCursor YCursor 20 sub def
    } for
    /XCursor exch 1 add XColWidth mul XCursorMin add def
    /YCursor YCursorMax def
} for
showpage

C#’s Parallel.Invoke is [Insert Hyperbole Here]

Reading Time: 2 minutes

Freeside VTIt’s simple – we can’t make processors go any faster. But we can add more cores.
The OS can schedule different processes to different cores but to take full advantage of the potential we need to be writing applications that run on multiple cores.
Unsurprisingly there’s a lot of investment in making this easier for programmers – to the point that it’s now easier than falling off a log…

Parallel.Invoke(
    () =>
    {
        if (!GetSomethingFromTheDb(out something))
            something = MakeUpSomethingFromSomewhere();
    },
    () =>
    {
        complexResult = SomeComplexOperation();
    },
    () =>
    {
        someObjectGraph = ReadAndParseSomeFile(fileName);
    });
SomeReallyComplexThing(something, complexResult, someObjectGraph);

The individual actions will be run in parallel, or to be more precise may be run in parallel. The task scheduler takes care of whether or not to actually run them in parallel and the degree of parallelism.

When we arrive at SomeReallyComplexThing all the previous tasks will have been done.

That’s ace. It’s a lot easier than messing about with threads.

Even before the Parallel library was around it wasn’t actually difficult, but you needed some lateral thinking…

Task[] tasks = new Task[] {
    Task.Factory.StartNew(()=>
    {
        if (!GetSomethingFromTheDb(out something))
            something = MakeUpSomethingFromSomewhere();
        }),
    Task.Factory.StartNew(()=>
    {
        complexResult = SomeComplexOperation();
    }),
    Task.Factory.StartNew(()=>
    {
        someObjectGraph = ReadAndParseSomeFile(fileName);
    })
};
Task.WaitAll(tasks);
SomeReallyComplexThing(something, complexResult, someObjectGraph);

Ok, I admit, I’m simplifying. You still need to understand the basic problems of concurrent programming but the mechanics of writing code that can take advantage of parallelism are now trivial.