How I made my core testsuite 2x faster

There is more than one way to make perl5 twice as fast, but this is what I did today. I fixed it on one machine.

My Macbook Air gives constantly better results in my hash function benchmarks than my big Linux Desktop PC, because it has a newer i7 Haswell, and the linux has only an older i5 CPU. Both have fast SSD's and enough RAM.

But when I run the perl5 testsuite the linux machine is twice as fast. Typically 530s vs 1200s. Which is odd and very annoying.

And then I fixed it with one little change.

$ time ./perl -Ilib -MNet::Domain -e'print Net::Domain::hostname()'

real    1m0.151s
user    0m0.028s
sys 0m0.011s

$ hostname
airc

$ sudo hostname airc.local
airc.local

 $ time ./perl -Ilib -MNet::Domain -e'print Net::Domain::hostname()'
 airc.local
 real   0m0.039s
 user   0m0.027s
 sys    0m0.008s

You see that Net::Domain::hostname didn't return a value. It timed out. Great for testsuites and benchmarks.

The code in Net::Domain calls for MacOS just hostname, which is fast, but for darwin it calls sys_hostname. But this is fast also. So what else is going on?

sub domainname {

  return $fqdn
    if (defined $fqdn);

  _hostname();

  # *.local names are special on darwin. If we call gethostbyname below, it
  # may hang while waiting for another, non-existent computer to respond.
  if($^O eq 'darwin' && $host =~ /\.local$/) {
    return $host;
  }

I cannot beat this magic, so I changed my hostname on my laptop. Problem solved. Aargh

Elapsed: 637 sec

2 Comments

It might be of interest to check out Wikipedia: .local.

I wonder if Net::Domain should remove the OS check as .local has been reserved for Multicast DNS resolution since Feb 2013.

Thanks for sharing this tip! I found it on Perl Weekly.

About Reini Urban

user-pic Working at cPanel on cperl, B::C (the perl-compiler), parrot, B::Generate, cygwin perl and more guts, keeping the system alive.