for, forEach, and map (oh my)

We interrupt the recent political ramblings and assorted existential crises to bring you a post about loops in Swift.

I was tempted to use forEach today but then I started wondering what sort of overhead closures add. So I cobbled together something resembling a test. Staring with an array of ints:

var loopIndices = [Int](0..<20000)>

I used these to create characters which are appended to a string. The “classic” for loop looks like:

for ch in self.loopIndices { self.loopStr.append(UnicodeScalar(ch)?.escaped(asASCII: false) ?? "X") }

The forEach version looks like:

self.loopIndices.forEach { self.loopStr.append(UnicodeScalar($0)?.escaped(asASCII: false) ?? "X") }

I ran this test 100 times on my AppleTV and the total time for a regular for loop was 1.8076 seconds. The time in the forEach loop? 3.6851 seconds. So basically twice as long. For good measure I made a map version as well that looks like:

self.loopStr = self.loopIndices.map({ UnicodeScalar($0)?.escaped(asASCII: false) ?? "X" }).joined()

The map has to join as well so it’s not surprising that it’s basically the sum of the other two tests coming in at 5.3892 seconds. Let me summarize in this handy table:

Type Duration Comments
Basic for loop 1.8076 seconds
forEach loop 3.6851 seconds Ew
map 5.3892 seconds Super ew

I thought, since the forEach closures are not @escaping, that Swift might be able to do some trickery since it shouldn’t need to capture values, worry about memory management, etc. So on a hunch I tried the same test with release settings (basically -O -whole-module-optimization instead of -Onone) and when we do that we get:

Type Duration Comments
Basic for loop 1.5675 seconds
forEach loop 1.6468 seconds Better! Only 5% slower, but then…
map 1.5958 seconds What sorcery is this?!

This got me wondering what map looks like without the added join so instead of appending to a String we have:

var loopStrings : [String]

And running this with optimization on yields:

Type Duration Comments
Basic for loop 0.4478 seconds
forEach loop 0.5814 seconds Overhead more visible now. 30% slower
map 0.3168 seconds Maybe it make better guesses about memory allocation?

In general appending to an Array is faster than appending Strings. No surprise there, but look how much better map is. Okay, so maybe we eliminate the need to muck about with memory in our tests and see what happens. We'll take the same array of Ints but add them up or something. Hmm, but map isn't great for that sort of thing, we'll reduce instead. We'll also double the number of items in our array of ints, but this is going to be really fast and our profiling numbers may be down in the noise floor. Whatever, the original question was how much overhead do closures add? So our map is now:

self.loopTotal = self.loopIndices.reduce(0) { $0 + $1 }

And the other loops have been modified to produce the same results. In this case our times are:

Type Duration Comments
Basic for loop 0.0070 seconds
forEach loop 0.0103 seconds Not quite 50% slower
reduce 0.0093 seconds Still better than forEach

With most of the actual work stripped away we can see the closure overhead more clearly. Although it's not a constant value or it would have been at least the difference in time from the previous example (0.1336 seconds). In this case, the difference works out to an extra 3300µS over 4 million iterations. Woo. So if forEach results in code that you believe is clearer you're probably not going to notice the performance hit. But it will be there so it's probably something you want to avoid if you're looking to conserve every cycle.

My Team

Hello people. I’ve been mulling this post for awhile. It’s a bit political again I’m afraid. But then again it feels like everything’s a bit political these days.

So, there’s something that’s been bugging me for a while now. People seem to be treating their chosen political affiliation the same way they would their favorite sports team. This makes no sense to me at all. It’s like asking who your favorite football team is: offense or defense? Oh hey defense is my team and it doesn’t matter that we’re losing the game because we’re totally winning on time of possession! Or our defensive buddies over there are keeping our offense from scoring! Woo! Defense rules! Suck on that offense!

That’s kind of ridiculous. Isn’t it? Hopefully we can agree on that. Maybe?

I mean it’s simplistic but if we keep the sports analogy going, then the Democrats and Republicans would just be different strategies on what wins games. Plenty of people that can disagree whether offense, defense, special teams, or whatever, wins games. And at the end of the day all those people can come together to celebrate a victory or console one another when their team suffers a loss. And everyone’s “team” should be The United States of America and not an ideology.

Can you even begin to imagine if attitudes shifted this way? Regardless which half of the team makes a great play the other half is there to celebrate. You don’t normally hear guys after winning a game say, “well the offense once again saved our bacon. I don’t even know why we have a defense”. Even when it clearly the kicker’s fault for missing an easy field goal (laces out, Dan!), the post game talk is almost always about the team effort, not any individual, faction, or how the crowd didn’t cheer loud enough.

When I think about it, it doesn’t feel like this would be a huge shift in sentiment. Sure let’s keep the sports mentality going, but let’s treat the politicians like we do our favorite team’s coaching staff. Your team may have won a half dozen championships but turn around and lose a half dozen games and see how quick the fans are to demand the coaches head on a spike. Even if you agree 100% with a coach’s philosophy, if your team isn’t winning it doesn’t matter. For that matter it may be one player. Maybe the goalie has lost his mojo, whatever. The point is that criticizing your sports is almost expected. Everyone knows that criticism doesn’t mean you don’t love your team. I can imagine a lot of Vols fans shaking their heads sadly in agreement.

So how about that? Can we give that a try or at least keep that in mind? I’m not on team Democrat or team Republican or even the dastardly team Media (mustache twirling intensifies). I think that’s true for most people. All we really care about is our home team: USA.

The Real Battle

If you’ve ever been to a casino maybe you’ve noticed how maze-like the layout is. Also the lack of clocks and windows, the low ceilings, busy carpet, and of course all the lights, sounds, and action! I’m sure you wouldn’t be surprised to learn that this is not at all by accident. Casinos owners pour quite a bit of research into subtle (and not so subtle) psychological tricks designed to extract the maximum amount of money from you the moment you set foot in one of their establishments. Though perhaps it might come as a surprise to you just how detailed this research is. For example check out the table of contents to this 627 tome on Designing Casinos to Dominate the Competition. No detail is too small to escape the notice of the casinos!

This same research and attention to detail is applied to anything that can be marketed: food, clothing, vehicles, electronics, you name it. All around the world companies with things to sell are devoting incredible resources in discovering all the little tricks that can be employed to give them an edge in selling their product to you. Perhaps you’re skeptical on the effectiveness of marketing but the $200 billion spent every year on advertising leads me to believe that there’s probably something to it. Whether you want to believe it or not our motivations are influenced in big ways and small, directly and indirectly, by marketing and the psychological theories on which it is constructed.

And if you accept the amount of time, energy, and money spent as evidence that these psychological manipulations are effective then surely you can imagine that others have as well. For example just consider the political landscape in the United States right now. Doesn’t it seem like it we’re becoming more and more polarized? I mean come on. Sure Republicans and Democrats have always disagreed but lately it feels like the major political parties are portrayed the same way we’d present sports teams, or even as the good guys versus the villains, instead of human beings associated to some degree with a particular set of ideologies. And why do you suppose that is? Might it have something to do with the monetization of the news and political coverage? Is Rush Limbaugh going to build a more energetic audience by bringing together both sides for a reasonable and nuanced discussion about the role of government in our lives or by painting the other guys as people who literally want to destroy all that you hold dear? Of course the latter is more compelling. It’s entertainment! People want to be entertained. An thinking is work, right? The media understands this and they want to cultivate audiences that can’t wait to tune in to the next episode to see what their heroes (or the dastardly villains) are up to now!

Does that not seem just the tiniest bit plausible? Or do we accept that this continued polarization is occurring organically? Now I’m not necessarily saying that all of the networks execs are in this cabal sitting around smoking their cigars, cackling maniacally whilst rubbing their greedy little hands together as they plot unrest amongst their audiences all in the name of corporate profits. I mean maybe, but more likely the decline of the moderate is the cumulative effect of a thousands little decisions that have been made to get people more fired up and tune in to the next show. Probably seemed kind of harmless really.

Perhaps you think I should look into hat making. Specifically the tin foil variety. Trust me I’ve wondered the same. But even if we’re somehow not being influenced by the big media conglomerates there are doubtless other actors out there that are attempting to influence our thinking. We know for sure of people who deliberately set up fake news sites although it’s debatable whether they were trying to influence the election or just make a few bucks from the gullible. But certainly and more alarmingly, seventeen different United States intelligence agencies have stated with “high confidence” that Russia tried to influence American opinion during the 2016 election. And there is solid evidence throughout the election campaign, and beyond, of Russian agents posing as Americans posting their support for Trump on social media services.

Also consider “Foundations of Geopolitics” written in 1997 by Aleksandr Dugin. This book allegedly had a big influence on Russia’s foreign policy strategy. It goes into detail on strategy with regard to almost every nation and has this to say one the approach to take with the United States:

  • Russia should use its special forces within the borders of the United States to fuel instability and separatism, for instance, provoke “Afro-American racists”. Russia should “introduce geopolitical disorder into internal American activity, encouraging all kinds of separatism and ethnic, social and racial conflicts, actively supporting all dissident movements – extremist, racist, and sectarian groups, thus destabilizing internal political processes in the U.S. It would also make sense simultaneously to support isolationist tendencies in American politics.”

Does this sound at all familiar? Maybe a teeny bit?

I expect, if you’re one of the millions of people that voted for Trump, it’s not easy to confront the possibility that you may have been influenced by Putin and friends. But closing your eyes to the possibility that the Russians may have played a role will not help matters. Though if it makes you feel better there are almost certainly other countries and organizations that have successfully influenced many to take opinions opposite your own. And, if the excerpt mentioned above can be trusted, maybe even those same Russians. Their strategy is to sow discord and it seems to be working!

What’s important to understand is that there are all of these invisible forces out there trying to guide us towards decisions or beliefs to further their own aims and not necessarily those that are best for ourselves, our community, nation, planet, etc.

I believe the modern battlefield is located between our ears and that many lose their battles without ever realizing there’s been one. Once we realize that our thoughts are under attack we can begin to defend ourselves by questioning our motivations and influences. By making sure we’ve fearlessly examined an issue from all angles including especially opinions from those we dislike or might normally disagree with. And that we continue to keep an open mind to new facts and input. I believe that if we put one tenth of the amount of effort into challenging our beliefs as we do seeking out validation for them that the world would be a better place. We need to take a step back and realize that Republicans and Democrats are not enemies, rather those who cultivate that belief are the true enemies.

Money

I like money. There, I just wanted to get that off my chest.

Well, there’s actually a little more than that. My approach to handling money, or more specifically the money set aside for retirement has recently changed pretty radically. Formerly I treated my retirement plan like, say, cleavage. Yeah sure let’s go with that. That is to say, it’s okay to steal a glance every now and again but not something you should fixate on despite urges to the contrary.

That was basically my plan. Put some amount in a retirement account, let it do its thing and when you retire hopefully something good happened. Like a time capsule you open up years later and hope it’s not filled with Pogs. I’d also heard that you should try to save 15% of your income which, had I actually taken to heart as I began my professional career, might have made a difference. Further I’ve never been what you’d call frugal, nor was I a spendthrift. I just didn’t give a lot of thought to a financial plan apart from throw a little money over your shoulder into a retirement account and try to pay down debt whenever possible. I like simple and stress free, but now realize that perhaps I was just postponing the eventual complexity and stress when there’s probably a happy medium assuming it’s addressed with enough time.

So, we recently reached a significant financial milestone and it started me thinking, you know retirement is really not that far away. I’ll be 62 in 14 years. My youngest child is 16. His birth really doesn’t seem that long ago and in less time that that I’ll be SIXTY-TWO! So this is a thing that’s going to happen and I’m starting to wonder how my (finger quotes) “plan” (finger quotes) is going to work out. I’m sure you’ll be surprised to know that, after careful deliberation, I realized that the original plan is a bit of a disaster.

I could go into detail, and in fact I may at some later date, on exactly how I plan to address this (basically this), but I wanted to just throw this out there: if you don’t have a real retirement plan and some means of tracking your progress, you really owe it to yourself to sit down and take a long hard look and attempt to formulate one. There are a number of sites out there that can help you determine first what you’ll likely need to cover annual expenses, next what you’ll need to plan on saving to cover these expenses, and also how best to put all of this together in a way that gives you the best chance at success. I’m partial to Personal Capital myself, but Betterment also has some nice tools available. I’m sure there are many others. If you provide honest and conservative estimates with regard to your financial needs the results may surprise you. You may, as we did, need to take steps to radically change your lifestyle, saving, and spending habits to get back on track.

I keep thinking about this: before I started paying attention I would have thought $1,000,000 was a lot of money. I mean it is, but it’s really not that much money when you consider that the sort of standard rule of thumb is that you should plan on drawing your retirement saving down by no more than 4% annually. That means if you want to be able to count on paying yourself $50,000 a year (which is less than the average household income) then you should plan on a nest egg totaling $1,250,000! This was a bit of a revelation to me. I’d simply not given it much thought. But now I realize that I would no longer categorize a millionaire as super wealthy. It’s really what every single household needs to work towards if they want to go into retirement with any degree of comfort.

Aw Funk

Over the past year or two I have, on a number of occasions, fired up MarsEdit with every intention of explaining my absence from this site and social media during that time. And each time I sit for a few moments, fingers hovering over the keyboard ready to burst forth with all that I wish to say… and each time generally ends with a “whatever” and / or the realization that I don’t have the time or energy to defend my opinions on a whatever topic and those with whom I disagree are unlikely to be swayed anyway so what’s the point? So much of social media and the internet these days seems to be to make sure all are well aware of your opinions and feelings towards those that don’t share them.

Anyway…

Today I’m looking over this site and realizing that I did not post a single thing here in 2016. I posted 8 or fewer tweets in seven out of the last twelve months. My facebook usage has seen a similar decline. And it’s not that I don’t have things to say or that nothing’s been going on. There’s been plenty. Those close to me know that these last couple years have indeed been interesting times. I can’t put an exact date on when this “funk” first began to settle in, but certainly by the fall of 2015.

I don’t like it. I’m not sure what to do about it or if this represents the first step of a return to form for me. Honestly it was seeing Manton’s Micro.blog thing on Kickstarter and thinking, man it sure would be cool to have a thing that I could get super fired about up again. Or maybe at this point step one is just writing down how cool it would be to have a thing to get super fired up about again. It feels like something’s gotta give for sure.