Filed Under: Technology with 0 Comments
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
Some minor adjustments to the routine, including an adjusted guess:
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.
Filed Under: Life with 0 Comments
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….
Filed Under: Rants with 0 Comments
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…?
Filed Under: Technology with 0 Comments
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.
Filed Under: Business, Rants, Technology with 1 Comment
UPDATE: 3 July 2008
Updated code to reflect more recent tax tables (2009)
When I was asked to estimate PAYE on a gross monthly salary, i hauled out the calculator and started chipping away, according to the SARS Tax Tables. Not being a tax consultant or looking at various structured packages, the first stab is mostly always a straightforward estimate without investigating further deductions. While doing this, the math-programmer inside me went… “Mmmm. Calculator. Boring. Ruby. Smile”
Turns out, it’s a simple little script; a useful little snippet and, bonus, i migrated some more learning onto Ruby. An aside; there’s definitely something about the difference in speed and endurance of learning between my brain versus my hands. You know the feeling. You can forget a password mentally, but let your fingers do the talking… And utilising muscle memory as an aid is just one of the many senses you can draw on…
Back to the snippet. Not too much interesting going on in terms of code. I chose a multidimensional array for storing the tax table. First used a hash, found it was overkill, reverted. Also did a classic switch in the beginning, to determine which “bracket” your pay falls into, but then figured a straightforward loop works just as well. This was an interesting break in habits from C# however.
In C#, looping through an array would be: for(int ii=0;ii<array.length-1;ii++)
I did the same in Ruby, transliterated the code, first time round: for ii in 0..array.length-1
But then, the knowledge of the “times” method changed my thinking completely: 6.times { |ii| … }
There are, afterall, one of 6 tax brackets you are likely to fall in (for all positive salaries)
And that’s where it hit me: the uncomfortable (more about that later) shift away from a corporate-sponsored, statically typed, IDE-integrated, certificate-oriented, compilable(?) programming language into a community-driven, dynamic scripting language is underpinned by these sudden ferocious rushes of freedom. Too much freedom? Certainly, too much to what i’m accustomed to sometimes.
Why’s that uncomfortable? Well, MSDN, VS, MS communities and the framework tell you how to code- to a large extent. They dictate the patterns, the constructs, the idioms; in short, they impose a very definite way of doing things. And it’s a big abstraction layer, forever changing (but not really) and giving you tons of resources to make your coding easier. This is good. Books, online help, built-in help, IDEs, intellisense… and more of all the good stuff. Don’t get me wrong, these things made me very productive and i’m grateful for that. But then you break away from that.
You gotta search for help (no nicely packaged MSDN DVD delivered to your door). You gotta scratch under the hood. You have to engage with community blogs and real people in a virtual world. You are forced to read opinions. You are stripped to the only the most simple of tools- a plain text editor. On a coding level, you are forced to remember namespaces, method names, variable names, libraries… no more intellisense to rely on. And this is where it was difficult. No more crutches to help me be more productive. I had to start thinking- for real now- and remembering stuff. And then i got scared: what if the “community” changed something and i didn’t know about it- or worse, didn’t agree with it? And I didn’t get an email with an updated change delivered to me automatically via updates or DVD? Hang on! Is that really the way it’s supposed to be? Have i become that lazy? Oops.
And all i was doing was having some fun, writing a little script to calculate PAYE so that next time someone asks me, i save myself a little more time.
Btw, the Source Code is here, if you’re interested.
Filed Under: Technology with 1 Comment
Finally. It has arrived. After some time of deliberating, this once MS-only geek, turned and ran for the cover of open source operating systems; but not happy to just stay there, went one step further and has now immersed himself into the world of Mac
Afterall, i was tired of hearing the debates all the time; and to a large extent, i couldn’t really care which OS is best, since “best” is a loaded term and sparks off quasi-religious wars amongst the zealots; so i decided to live my opinions out, if i had any. And i definitely have some now.
My early comments can be summed in one thought which continuously bumps against the inside of my skull as i discover some new joy lurking under the hood of UI: “how on earth did i ever compute without a mac?”
Filed Under: Rants, Technology with 0 Comments
We have some serious issues facing our world today. Heck, in South Africa, the land of opportunity, we have more serious issues than we sometimes care to face up to. Tackling real issues is hard work. So, what’s the alternative?
Stop the press! Houston, we have a problem! In fact, call Heather Brenner out of retirement! Google does not display a link to it’s privacy statement on the home page. God forbid!
Something so serious demanded the attention of the world media (and my blog :sigh:), along with a host of, ahem, clamour, of petty kings. Good thing they were watching because if Google manage to continue getting away this kind of arrogant in-your-face lawlessness, who knows what cheecky and utterly ludicrious cheap shot they might plan to get away with next? :p
Filed Under: Business, Technology with 0 Comments
That’s roughly equivalent to the sound of the resonating chord which struck me this morning.
But today’s revolutionaries are sheep in wolves’ clothing. They’re lost in the economically meaningless, in the utterly trivial, in the strategically banal: mostly, they’re cutting deals with one another to…try and sell more ads. That is, when they’re not too busy partying.
Oh, if it weren’t so true. The rest of the article, is soul-searching and uncomfortably challenging because it has that “truth bites” edge to it. It’s also filled with quotable quotes, not for the purpose of quoting, but for serving as reminders when you get enough chance to breathe and realize exactly what it is you are doing.
That is to say, it serves, at the bare minimum, as a reality control against going too far in one direction; but only if it failed to catalyze something deeper within you.
Filed Under: Business, Technology with 1 Comment
We have a concept of what the learning curve represents, and unfortunately, the same thing can represent 2 opposite concepts. What makes more sense to me is looking at a learning curve from a classical labour cost perspective and more keenly towards labour productivity. In this sense, the learning curve is interpreted, broadly, as: the more you work with something, the more productive (cheaper, better, faster, more knowledgeable) you become. And one can recognise that sentiment when it’s expressed variously with respect to success, specialization, expertise, productivity, quality or minimizing costs. So where’s the investment?
Programming technology changes rapidly, and sometimes to the detriment of programming and business, sometimes not, but also to the advantage of progress and for the sake of technology itself. But changes are also forced to be incremental in order to be successfully adopted, since any radical departure will result in a prohibitively expensive learning curve where the economic costs outweigh the advantages of the change. Similarly, you also cannot force change too frequently, even if it’s small enough, since you never get to break even or realise a profit from the previous change. I think this last point might also be reflected in the current developer attitudes towards the “next big Microsoft thing” and the ubiquitous jading of old hats. Everyone seems to hanging five for a bit before moving forward. Or maybe Douglas Adam’s theory is kicking in?
At the same time though, you need to keep moving forward. So where do you invest your next generation of development so as to minimize the costs of the learning curve if you want to remain marketable and competitive across:
* web development
* mobile application development
* backend systems
* any platform (platform agnostic)
C++, C, C#, VB, Perl, PHP, Python, Ruby… ?
All these languages have their pro’s and con’s and more importantly, costs. As an example, I recently looked at Symbian C++ development, and the learning curve is relatively expensive. The idioms alone take time to get to grips with, so although you got a very powerful API, C++ on Windows desktop, or legacy ATL knowledge is not easily transferrable to a Symbian C++ development effort. Possible, but no as easy as say, being able to use a standard framework and language, with the same idioms, on both (all) platforms. That would be the ultimate prize (for me).
And it doesn’t have to be the same language. Case in point, i’m currently using Monorail (.NET web development) and RoR (other web development) concurrently on different projects and i’m enjoying the benefit of being able to work in a predictable (hence productive) manner switching between the two, relatively seamlessly. Whereas, switching between asp.net webform development and RoR, as an example would not be feasible. The traditional 30% context switch overhead would double.
So if you’re faced with “what to learn next”, take a closer look at the learning curve and where you can (need to) apply that knowledge in the future, in order to remain competitive- whether globally, or within your own department. Maybe it’s stating the obvious, but it’s surprising just how un-obvious the obvious can become when there are a lot of flashing lights going off all the time. So it’s not always about the language or the technology, but also a lot about the “way” in which things are done; which, by nature, is usually a little more sublime to spot since it’s hiding in plain sight ![]()
Filed Under: Technology with 0 Comments
A.D.D. From the textbooks of an introduction into the business world (Department of Decision Sciences, UNISA):
Control does not, however, keep workers from making mistakes. The motivation of workers and a positive attitude towards their tasks, limit the number of mistakes they make.
It explains why TDD on it’s own will fail, and also why some folk don’t even need a fully bulked up test suite, if one at all. Although personally, i think no matter how good you are, there has to be something challenging in every project which you need a safety net for to keep the productivity going.
And it explains a lot of things about business, but more importantly for me, the business of software production. Also, it helped me realise why the solid programmers i have had the honour of working with are good AND cool to work with.
Attitude towards your craft (and fellow crafters even) and the passion for what you do will be directly proportional to the number of bugs waiting for you in 3 months time on a tight deadline. And, i suppose, the converse would also be true: the number of bugs you’re dealing with today (of your own making) are a direct result of your attitude applied when first implemented, and if not of your own making, those bugs reflect the attitude of the guilty coder.
Of course, i maintain the assumption of all things being equal, most notably: skill and ability.
And therein lies a great life lesson too (it’s part of why i really groove on programming as a lifestyle). The relationship between the bugs in your life and your attitude towards your life is more apparent and plain to spot than you might imagine.