Categories

Optimizing code is generally an expensive process (read: time-consuming) and there are established ways of getting to the bottom of “what to optimize”. Thankfully, profilers are available to help with a lot of the guesswork, so it’s generally a good idea to make sure you work with one *most of the time*. Moving along, it was high time for me to look at some Ruby profiling.

The documentation for ruby-profÂ is pretty neat and the library itself is quick to get up and running with. And so we start. For my initial problem, I wrote a goal-seek algorithm for accurately estimating gross earnings, given a target nett earning using a tax table- as opposed to just using a base tax-rate. Anyhow, my first stab algorithm (a simple linear search) included the lines:

```def seek_annual_gross(m_nett, base_perc)
Â Â sample_gross = m_nett * base_perc
Â Â paye = Paye.new(sample_gross)
Â Â p_nett = sample_gross - paye.monthly_tax
Â Â margin = MARGIN*m_nett
Â Â if((p_nett-margin < m_nett) && (p_nett+margin > m_nett))
Â Â  Â return paye.annual_gross.round_to2.to_f
Â Â elsif(p_nett-margin > m_nett)
Â Â  Â return seek_annual_gross(m_nett, (base_perc - (margin)))
Â Â elsif(p_nett+margin < m_nett)
Â Â  Â return seek_annual_gross(m_nett, (base_perc + (margin)))
Â Â end
end
```

The profiler showed up what i kinda suspected- always a good sign. Essentially, my incremental margin for the next step was too small (fixed) and thus, getting closer to the solution was taking too long- and endangered the stack 🙂 What i needed was a better guess at how much to increment.
``` % cumulative self self total time seconds seconds calls ms/call ms/call name 22.58 0.28 0.28 177 1.58 3.22 Integer#times 16.13 0.48 0.20 171 1.17 259.18 NettGoalSeek#seek_annual_gross 6.45 0.56 0.08 179 0.45 0.50 Float#round_to2 ```

```increment = MARGIN*(p_nett - m_nett)/margin
```

and modifying the appropriate calls

```if((p_nett-margin < m_nett) && (p_nett+margin > m_nett))
Â Â return paye.annual_gross.round_to2.to_f
elsif(p_nett-margin > m_nett)
Â Â return seek_annual_gross(m_nett, (base_perc + increment))
elsif(p_nett+margin < m_nett)
Â Â return seek_annual_gross(m_nett, (base_perc - increment))
end
```

And the profiler now reports:
``` % cumulative self self total time seconds seconds calls ms/call ms/call name 2.78 0.16 0.01 11 0.91 6.36 NettGoalSeek#seek_annual_gross 2.78 0.35 0.01 17 0.59 1.18 Integer#times 0.00 0.36 0.00 19 0.00 0.00 Float#round_to2 ```
A significant difference! Incidentally, the time to run, according to the test harness, went down from 1.122316 seconds to 0.189661 seconds. The high-level indicator showing enough of a difference as well.

The by-product of this optimization included the ability to get even more accurate estimations since the stack never overflowed, despite the required margin of error.

The moral: optimization doesn’t need to sacrifice code readability. At the right time, in the right spot, for the right reasons, you can achieve a sweetspot (of sorts) between two opposing(?) constraints. But that’s not to assume i’ve found the nicest sweetspot in this little piece 🙂

So in between refactorings or when there’s a lull in production, indulge the geek inside you.

Categories

## It’s Officially Cold

Say no more… Cape Town is the officially the new South Pole. Ohmygollygoodnessgraciousnessme!

And yes, i am aware that it gets colder than this in other parts of the world… but trust. For us beach addicts here, where the sun fun never sets… the 3 months of winter we have to endure, every year, this IS cold. Brrrrrr….

Categories

## Howto Help the Economy

A bit of locally flavoured South African non-technical posting… on how to help an economy in need.

Indeed, when the chips are down, and prices are going up, and the economy is under threat of recession; what’s the most tactful move you can make? Why, organise a mass-action protest stayaway that effectively costs businesses billions of rands- just to put the cherry on top. Maybe put one or two out of action while you’re at- slam the final nail into the coffin? Oh, and don’t forget to cripple transportation and emergency services- hold the country at ransom during an economic crisis. What a sure fire way to send out a great message. At least you’re guaranteed of getting some attention.

Exactly what the message is however, and to whom it’s addressed to remains a mystery. ‘Cos the ubiquitous “business” and government of South Africa sure aren’t responsible for the price of barrel of oil.

Sadly, true character will always be revealed during the trials. But I suppose the alternative was to organise a call to come together and find innovative ways to save and help each other… Mmmm…

With great power, right, comes…?

Categories

## Snippets

How often you store code snippets, only to lose them again? Often enough (or maybe you aren’t writing that much code). There are plenty useful routines out there, already written, some you could probably rewrite in your sleep without looking them up and probably aren’t worth saving. Others are gems that need to be published for posterity- not for yourself, but more importantly for somebody else struggling with the problem/language for the first time.

So after much delay, besides posting snippets here in this blog, i’ve taken to posting some around. There’s also an interesting angle wrt copyright and code ownership which can be completely circumvented (thus ensuring the longevity of tried and trusted routines) if you publish your snippet in the public domain.

Strictly speaking, depending on the kind of labour contract you’re engaged in, whoever pays you, pays you for your time plus the product of your time, and by implication, with intent and legal claim to that code routine. A highly contentious topic if there ever was but a difficult one to implement or monitor.

Nonetheless, with certain freedoms in place, you can strike a happy balance between ensuring your client gets full copyright and ownership of code particular to their domain, while you remain free to reuse that general routine in another (unrelated or not) project with a clear conscience: public domain. Of course, again technically speaking, you need to decide who pays for the time on that routine, and it should be you unless there is a transparent agreement from your client. Yours to work out or not.

Other than that, just a general shout-out big THANK YOU to everyone who does post code snippets- it’s helped me huge in my career; hoping to pay it back to others somehow. And a general encouragement to everyone else… get pasting.