Monday, July 30, 2012

Premature Optimisation

For over a year in 2008 through 2010, I led a performance optimisation effort of a small team at a very large client on a shockingly complex e-commerce solution. We profiled the codebase, optimised the environment, implemented code optimisations, and worked to get them in our test environments.

One lesson that stands out from that is that you should never optimise code without evidence that it is a bottleneck. Run a profile, analyse the hot-path, and then (most likely) cache the result of the operation or find a smarter algorithm to accomplish the same task.

I had a colleague on my current project raise a concern about the performance of UTC to Local time conversion. I dismissed it as arithmetic, but he persisted. So we tested it and measured it at our volume as taking 34 milliseconds. For some applications that might be a concern, but not for ours.

Bottlenecks usually involve moving data over a network, or na├»ve algorithms that are O(c^n) or O(n!) on moderate to large datasets. (Remember that in-memory Linq statements are really just expressive foreach loops, I’ve been burned by that before.)

In general, however, it’s best to write readable and maintainable code. Hacking an algorithm because you think it might be slow (like worrying about time zone conversion) is a poor reason to make a decision on how to design your solution. Gather some evidence. Premature optimisation is the root of all evil.

Wednesday, July 18, 2012

Self-Driving Cars, Taxis, and Rentals

A prediction about the future, 2017 or 2020 or so:

I'll be able to pull out my smartphone and order a self-driving car. It will come to my GPS location and pick me up. In the event that it's a busy street and the car is non-obvious, my phone will alert me that the car has arrived. This is much like ordering a taxi today by phone, with the added advantage that no one can steal it en route. I'll tap my phone on the door to unlock it with NFC. After I ordered it with my smartphone and was waiting for the few minutes for it to come and get me, I entered my destination into the same smartphone app. The car takes off immediately. When I'm done, I tap out with NFC again, and that's it. My account is charged for the trip. It will probably work like most toll and transit passes where they deduct from a small pre-paid balance and it auto-recharges.

In this world, easily-ordered self-driving cars will replace taxis, shared car services and rental car companies. Why go through the hassle of booking a time slot in the shared car and worry about getting it back on time when it's just as cheap to order a self-driving car? Not to mention driving! Driving is so...pedestrian...so 2012.

Self-driving taxi/shared cars like this will finally be the killer app for full electric cars. These self-aware cars will drive themselves home when the battery is low.

They'll also be a death blow to something like 80% of car ownership. It will be pointless for people in big cities to own cars since borrowing one will be effortless and much more economic and efficient. These same fleet companies will offer discount long-term rates for road trips and holidays.

At first a few players will compete with proprietary systems. Some five years after, the marketplace will standardise, and these cars will become an interchangeable commodity. Governments will step in and run them as "public transit." Luxury brands will come in so that elitists don't have to share cars with the rest of us.

Or so I hope. This on the news that California has recently legalised self-driving cars.

Tuesday, July 17, 2012

Managing Powershell Profile.ps1 with SkyDrive

I use SkyDrive to sync pretty much all of my data (I was an early adopter and so they gave me 25GB free). SkyDrive lives at $home\SkyDrive, and my Powershell Profile.ps1 lives at $home\Documents\WindowsPowerShell\Profile.ps1.

To manage it, I created a folder named $home\SkyDrive\Documents\WindowsPowerShell and used mklink to create a symbolic link (yes, NTFS supports symlinks). Start cmd.exe as an Administrator:

cd \users\username\Documents
mklink /D "WindowsPowerShell" "..\SkyDrive\Documents\WindowsPowerShell"


Now I can keep my profile and various modules & utilities (notably PSCX and git-tfs) in my Powershell folder and roam from machine to machine. Bliss!