Micro-Optimization and Meatballs

In my previous entry on the real cost of performance, there were some complaints that my code's slow and it sucks. If I had a nickel every time someone told me that, I could have retired years ago. Let's take a look at the specific complaint that the s <> "" comparison is inefficient, using low-level windows API timing in the Stopwatch class:

Const iterations As Integer = 1000000
Dim s As String = "sample string"
Dim sw As New Stopwatch
Dim n As Integer
n = 0
sw.Start()
For i As Integer = 1 To iterations
If s.Length = 0 Then
n += 1
End If
Next
sw.Stop()
Console.WriteLine(sw.ElapsedMs)
n = 0
sw.Start()
For i As Integer = 1 To iterations
If s = String.Empty Then
n += 1
End If
Next
sw.Stop()
Console.WriteLine(sw.ElapsedMs)
n = 0
sw.Start()
For i As Integer = 1 To iterations
If s = "" Then
n += 1
End If
Next
sw.Stop()
Console.WriteLine(sw.ElapsedMs)
n = 0
sw.Start()
For i As Integer = 1 To iterations
If s.Equals(String.Empty) Then
n += 1
End If
Next
sw.Stop()
Console.WriteLine(sw.ElapsedMs)

Here are the results:

Athlon FX-53
2.4 GHz
Pentium-M
1.2 GHz
s.Length = 02.6 ms10 ms
s = String.Empty20 ms46 ms
s =""20 ms43 ms
s.Equals(String.Empty)13 ms26 ms

So, yes, String.Length is five (or more) times faster. And yes, using String.Equals is twice as fast. However, neither of those will work when the string is Nothing, and we're still talking about a difference of 30 milliseconds, on the slowest computer I own, over a MILLION string comparisons! This brings to mind a Bill Murray quote from Meatballs: It just doesn't matter! It just doesn't matter!

Meatballs (1979)

Arguments about which method results in code that is easier to read and easier to maintain will be gladly entertained. Arguments about speed will not. Stop micro-optimizing and start macro-optimizing: per Lippert, code that makes sense is code which can be analyzed and maintained, and that makes it performant.

If you'd like to time this yourself, here's a stopwatch class which uses the high resolution API counters. Good luck-- you're gonna need it. The resolution, I mean.

Read more

Stay Gold, America

We are at an unprecedented point in American history, and I'm concerned we may lose sight of the American Dream.

By Jeff Atwood · · Comments

The Great Filter Comes For Us All

With a 13 billion year head start on evolution, why haven't any other forms of life in the universe contacted us by now? (Arrival is a fantastic movie. Watch it, but don't stop there - read the Story of Your Life novella it was based on

By Jeff Atwood · · Comments

I Fight For The Users

If you haven't been able to keep up with my blistering pace of one blog post per year, I don't blame you. There's a lot going on right now. It's a busy time. But let's pause and take a moment

By Jeff Atwood · · Comments

The 2030 Self-Driving Car Bet

It's my honor to announce that John Carmack and I have initiated a friendly bet of $10,000* to the 501(c)(3) charity of the winner’s choice: By January 1st, 2030, completely autonomous self-driving cars meeting SAE J3016 level 5 will be commercially available for passenger

By Jeff Atwood · · Comments