August 2011 Archives

Determining my daughter's age

I imagine that just about every parent who programs has written something like this at one time or another.

use DateTime;

my $birthday = shift || 20110205;
$birthday =~ s/-//g;

my ( $year, $month, $day ) = ( $birthday =~ /^(\d\d\d\d)(\d\d)(\d\d)$/ );
$birthday = DateTime->new(
    year  => $year,
    month => $month,
    day   => $day,
);
my $dur = DateTime->now->delta_days($birthday);

my ( $weeks, $days ) = ( $dur->weeks, $dur->days );
given ($days) {
    when (0) { $days = "exactly" }
    when (1) { $days = " and 1 day" }
    default  { $days = " and $_ days" }
}

say "$weeks weeks $days";

Me and my daughter

In other news, my daughter is "29 weeks exactly" today (though the picture above was actually taken in late June in Haarlem, a small town near Amsterdam).

Test::Most and timeit()

I've been doing so much work with algorithm efficiency that you've probably noticed me writing this at the top of a lot of my sample code:

use Time::HiRes qw(gettimeofday tv_interval);

sub timefor(&$) {
    my $start = [gettimeofday];
    $_[0]->();
    say sprintf "$_[1]: took %s time" => tv_interval($start);
}

I'm tired of writing this all the time, so I've added it to Test::Most 0.25 (heading to cpan and already on github). I renamed it timeit and the message is optional. Further, the function is only exported if you request it.

use Test::Most 'timeit';
ok 1;
timeit { ok 1 };
timeit { ok 1 } 'message';

done_testing();

For the example above (taken from my tests), it's silly to time the ok() function, but you may find it useful for quick 'n dirty timings. Plus, you can use it for any code, not just tests. The above prints out something like this:

t/timex.t .. 
ok 1
ok 2
# t/timex.t line 7: took 0.000124 seconds
ok 3
# message: took 0.000115 seconds
1..3
ok

It will warn if Time::HiRes is not installed.

Update: Fail! The blog title was wrong, so the URL will look strange. Oops.

Beating memoization

Memoize is a great module. It's one of those fantastic modules that, when you need it, does exactly what you need. Like when you're perfectly healthy and want to beg for money so reach for a crutch. That's how many people use Memoize.

Don't get me wrong: I've been lazy and abused this module myself. It's quick, it's easy, and as it says on the tin: "Make functions faster by trading space for time". So you know there's a trade-off: you save time, but your memory overhead increases. That's OK. Memory is cheap. No argument I might put down will dissuade you on that point.

But is it really faster?

Binary search versus hash lookup

A long time ago I had a very silly conversation with a developer where we both agreed that we weren't too concerned about algorithms and data structure memorization because, hey, we could look them up, right? We don't need to use that fancy-pants computer sciency stuff in our jobs because we've never needed to. Of course, that's like saying because you've never had a driver's license, a driver's license is useless. When you don't have a car, you naturally tend to look at problems and solutions without considering the options a car may provide you.

That's why I've been brushing up on my comp-sci lately. I've been working through a bunch of sorting algorithms (insertion sort, merge sort, and quick sort) to better understand their characteristics when I started to think about how I might find if an item is in a list. If it's a large list, I typically use a hash. Most of the time this is fine, but I wanted to understand better what I could do.

Why is booking.com hiring so many developers?

By this time folks know that I've been working hard trying to recruit more people for booking.com. They didn't ask me to, but I do this because I find it fun and I like working with people. Also, if you read my expat blog, you know that I want to help people live and work in other countries. I'm very fortunate that this passion of mine fits very well with my current employer's desire to move people to Europe. Yes, I get paid a bonus for everyone I refer, but I also tell everyone that if they don't want to go through me, they can apply directly to our jobs portal. Extra money is nice, but I'll happily forego that if I can help you live your dream of being an expat.

By now, many of you have seen our advertisement on jobs.perl.org advertising that we're hiring 20+ Perl developers. Many people have speculated as to why. This is to put to rest some of rumors which seem to go around.

Loving 5.14

Just a quick update to let people know that I am not, in fact, dead.

About Ovid

user-pic Freelance Perl/Testing/Agile consultant and trainer. See http://www.allaroundtheworld.fr/ for our services. If you have a problem with Perl, we will solve it for you. And don't forget to buy my book! http://www.amazon.com/Beginning-Perl-Curtis-Poe/dp/1118013840/