Simply Jonathan

Facebook, Always There to Help

A screenshot from Facebook with the message, 'Jonathan, it's almost your birthday!'

Thanks, Facebook. Thanks for also reminding me what my name is, you’re a pal.

(Yes, they did use it as a call to action to donate to a charity, but I still find it a ridiculous hook.)

What’s the Cheapest Empty Iterable in Python?

From the department of ‘optimisations that are so premature that if you ever find yourself actually caring about it, you need to find better problems to solve’, I recently had this thought: What’s the cheapest/fastest iterable in Python?

Reminder: iterable and iterator are not the same thing.

The need for an empty iterable occasionally comes up, like when you need to provide a default value to a missing key in a dictionary, and you need it to be something you can iterate over without running the risk of a TypeError. The beautiful thing, of course, is that you can iterate over an empty iterable and just have nothing happen, so the actual type or contents don’t matter.

So I set out to test it. Again: you should never need to actually care about this. If you can live with the actual overhead of iterating over something, you can live with the overhead if that something is empty, no matter the actual type of iterable.

I evaluated strings, lists, tuples, dictionaries and sets. My hypothesis was that the fastest would be a string or maybe a tuple.

The test was performed on a late-2016 13″ MacBook Pro with a 3.3 GHz Intel Core i7 and I timed it using the timeit module.

First I tested out simply declaring the different types of iterables:

kweli:~ j$ python -m timeit -c '""'
100000000 loops, best of 3: 0.00672 usec per loop
kweli:~ j$ python -m timeit -c '[]'
100000000 loops, best of 3: 0.0187 usec per loop
kweli:~ j$ python -m timeit -c '()'
100000000 loops, best of 3: 0.0119 usec per loop
kweli:~ j$ python -m timeit -c '{}'
10000000 loops, best of 3: 0.0305 usec per loop
kweli:~ j$ python -m timeit -c 'set()'
10000000 loops, best of 3: 0.0924 usec per loop

So far so good: strings are the fastest, followed by tuples and dicts, with sets trailing far behind.

Then to actually iterating over them:

kweli:~ j$ python -m timeit -c 'for i in "": pass'
10000000 loops, best of 3: 0.0433 usec per loop
kweli:~ j$ python -m timeit -c 'for i in []: pass'
10000000 loops, best of 3: 0.0514 usec per loop
kweli:~ j$ python -m timeit -c 'for i in (): pass'
10000000 loops, best of 3: 0.0438 usec per loop
kweli:~ j$ python -m timeit -c 'for i in {}: pass'
10000000 loops, best of 3: 0.0707 usec per loop
kweli:~ j$ python -m timeit -c 'for i in set(): pass'
10000000 loops, best of 3: 0.136 usec per loop

And again, the hypothesis is confirmed, but interestingly the difference between lists and strings/tuples is much smaller when iterating compared to just declaring.

So in conclusion, use a string as an empty iterable, unless you have any reason at all not to. The difference is infinitesimal.

Two Cultures

I was in New York in February, where it was unseasonably warm. So warm that you could see stuff like this in Prospect Park:

A sign that reads 'Danger: Thin Ice' in front of a completely non-frozen lake
Thin ice indeed

Yesterday I was out walking around the lake Damhussøen in my native Copenhagen. Here it is unseasonably cold for March (although not in the what-the-fuck-it’s-68°f-in-New-York-in-February category), and the lake is completely frozen:

A completely frozen lake, complete with a tree branch comfortably nestled on it; no signposts

But in Denmark, you’re on your own. To be fair, based on tracks we saw, it seems that people could actually walk on Damhussøen, though I doubt it’s something anyone would recommend.

Dr. Drang on Apple’s Scheduled Alert Systems 

This is amazing.

I appreciate that many of these products are probably maintained by somewhat disparate departments, but the lack of consistency in how things such as iCloud synching and cleanup act could probably do with some cohesion.

And Siri’s off-by-6 bug is Hall of Fame–worthy.

Cloudflare on Terminating Daily Stormer 

This is an incredibly thoughtful response to a very delicate issue.

I am not, nor have I ever been, a Cloudflare customer, but this post makes me confident they take their responsibility seriously.

I have exactly zero sympathy for The Daily Stormer and their ilk, but I think it is very positive that Cloudflare see the gravity of their decisions. Kicking Neo Nazis off is relatively uncontroversial, but it might not be as clear cut in the future, and it is not a decision that should be taken lightly.

International Media Are Obsessed With FC Midtjylland

I am dumbfounded of the international media’s obsession with Danish football club FC Midtjylland. The latest is a story in The Set Pieces about defensive midfielder Tim Sparv, but previously The Guardian, De Correspondent and Outside of the Boot have also covered them.

I mean, sure, Midtjylland have an owner – Matthew Benham – whose Smartodds system is apparently a revolutionary way to assess football players. But Benham is English and owns Championship club Brentford too; surely they would make more sense to write about, and easier to get to too, at least for the English media?

And it’s not as though Midtjylland’s approach has been an out-and-out success. Aside from the aforementioned Sparv and a few others, their signings have been unimpressive since Benham’s takeover, and an amusingly large portion of them have grown up in the peninsula of Jutland, where Midtjylland are based. They signed a clearly over-the-hill Rafael van der Vaart, who has been ineffective in the few games he’s been picked this season. They did win the championship – the first in their history – in Benham’s first season (2014/15), but the season before that they finished second (and were top for a long time) and in the seasons since they haven’t really been in contention, finishing 3rd and 4th, 12 and a whopping 30 points after champions FC Copenhagen respectively.

None of this is too damning; Copenhagen have been magnificent, especially in this season, and they have a far larger budget than the rest of the league, so finishing behind them is to be expected.

As far as signing local players goes, it is certainly admirable, and every club in the world signs players who don’t work out. It is also a common tendency for sports directors in clubs in smaller footballing nations to sign players who on paper are impressive signings (like van der Vaart), but who are only available because they’re very much past their best.

But the promise of Midtjylland’s system is they would be insulated from such shortcomings, finding players who actually perform, from all over Europe, not just well-performing players from smaller clubs in nearby leagues or fringe players from bigger clubs.

And in light of all of this, I continue to be amazed at why media outside Denmark care. Midtjylland talk a big talk, but they’re a football club with the same problems as any other club in their position, and they certainly haven’t found the holy grail with their analytical approach.

Ras Kass & RZA – the End

My metaphors are meta-fives.

Trump, Putin, and the New Cold War 

An in-depth look at the relationship between Donald Trump and Vladimir Putin. This bit has been making the rounds on Twitter:

In 2008, according to “Dark Territory,” a history of cyberwar by Fred Kaplan, Russian hackers accomplished a feat that Pentagon officials considered almost impossible: breaching a classified network that wasn’t even connected to the public Internet. Apparently, Russian spies had supplied cheap thumb drives, stocked with viruses, to retail kiosks near NATO headquarters in Kabul, betting, correctly, that a U.S. serviceman or woman would buy one and insert it into a secure computer. In the past decade, cyber tactics have become an essential component of Russia’s efforts to exert influence over its neighbors.

The Elements of Python Style 

A proposed style guide for Python, not quite as specific as PEP8, but dealing with some things that PEP8 doesn’t.

I agree with most of this, but this one in particular stood out to me because it echoes what I said in my first impressions of Clojure:

No one wins any points for shortening “response” to “rsp”.

(Via Python Bytes, episode #14)

Birds

Because I’m the world’s worst instagrammer, this is my illustration of a lovely holiday in Thailand: the gathering of a legion of birds in wires, creating an absolute cacophony. (The sound doesn’t come through so well with the traffic noise, but it was deafening.)

It really was a lovely trip, though.

This is Simply Jonathan, a blog written by Jonathan Holst. It's mostly about technical topics (and mainly the Web at that), but an occasional post on clothing, sports, and general personal life topics can be found.

Jonathan Holst is a programmer, language enthusiast, sports fan, and appreciator of good design, living in Copenhagen, Denmark, Europe. He is also someone pretentious enough to call himself the 'author' of a blog. And talk about himself in the third person.