bitwix

Tangential comments about Software Development

Tuesday, December 31, 2013

It is so speedy I didn’t dare blink

It was December, and I was concerned that the Property Hawk code for calculating tax return figures was running so slowly it was not fit for purpose. One user had reported it timing out before completing.

As always, I suspected database access. I tidied it up, and got no improvement. Was I a victim of success, that we had so many users that I needed to split the database into shards, like the old London phone book (volumes A-D etc)?

Thankfully, I took my time to address this, thinking through other possible problems. When I got around to going through the code, profiling functions, I found a sorting algorithm I had written one day when I couldn't be bothered to implement an IComparer bit of code. To add insult to injury, I found that later in the process a better sorting method was used, so the bad algorithm served no purpose whatsoever. So I started commenting out code:

        public static List FetchForTaxYear(KwDataSet ds, int year, string aorc)
        {
            List result = new List();
            foreach (Rent r in FetchNotDeleted(ds))
            {
                if (r.TaxYear(aorc) == year)
                {
                    result.Add(r);
                    //int idx = FindInsertPosition(result, r);
                    //if (idx >= 0)
                    //    result.Insert(idx, r);
                    //else
                    //    result.Add(r);
                }
            }
            return result;
        }

I asked the user who'd reported the problem, and she responded "it is so speedy I didn't dare blink". So that's good.

Lessons learned? It's not always the database. Suspect your old code, and be ready to change it. A small change may well be enough. Take time to choose a course of action. Don't write order-n-squared algorithms.