Post · 5 min read· pp. 907–910

str2long Contest Results Part 1

[NOTE: Reading this post only makes sense if you read and cared about this previous post.]

Ok, evaluating the submissions has been more work than I anticipated, and also things have gotten pretty busy at work, so I’m going to split the evaluation of the submissions into two parts. This first part will discuss objective metrics while the second will be subjective.

I received approximately 78 implementations of str2long(), including multiple submissions from a number of people. I say “approximately” since I deleted some submissions where the sender noticed it was buggy before I did. All submissions can be found on Github here. A total of 35 submissions pass all of my tests, which include many functional tests in addition to integer overflow checks using Clang’s -fsanitize=integer. My tests are reasonably good (I hope) but I still could have missed things, let me know if this is the case. I tested all submissions on an x86-64 Linux machine, targeting both 64-bit and 32-bit modes. The nastiest test case that I used is one that allocates a 9GB string that is mostly leading zeros; this caused a few submissions that passed all other tests to fail, of course because they used a 32-bit index somewhere. One might reasonably argue that this is a vanishingly unlikely use case, but it’s not prohibited by the specification so I tested for it.

Besides correctness, I also looked at code size and performance. Neither were explicit criteria for this contest, but all other things being equal faster code is better. Code size is mainly interesting because it serves as a very crude way to measure algorithmic complexity. In other words, code size answers the question: How much complexity remains after an optimizing compiler has looked at the function?

Since compilers are notoriously finicky about what they will and won’t optimize, I used a variety of compilers and report only the best result. The compilers are:

  • GCC 4.4.7, 4.5.4, 4.6.3, 4.7.2, and a pre-4.8 snapshot (r196703, compiled from SVN a few days ago)
  • Clang 3.0, 3.1, 3.2, and a pre-3.3 snapshot (r177225, compiled from SVN a few days ago)
  • Intel CC 12.0.5 and 13.1.0

Each compiler targeted x86-64 and was invoked at -O0, -O1, -O2, -Os, and -O3. I’m aware there are other flags controlling code generation but I don’t have time to play those games. Code size was measured using the size command after passing the compiler flags -fno-exceptions -fno-unwind-tables -fno-asynchronous-unwind-tables which are necessary to prevent extraneous code from being generated and counted. With these preliminaries aside, here are the size results:

bytescompilersubmission
96icc-13.0.1 -O1libc
109gcc-snap -Ospascal
110gcc-4.6 -Ospeter
110gcc-snap -Osryan
112icc-13.1.0 -O1bernd_2
115gcc-snap -Ostordek_2
116gcc-snap -Osdavidl
116gcc-snap -Osdavidl_2
122clang-3.0 -Osyolanpa
129gcc-4.6 -Osdaniel_2
129gcc-snap -Osfrancois_2
131gcc-snap -Osjeffrey
136gcc-4.4 -Osyang_2
137gcc-snap -Osdavide
138gcc-4.4 -Osdavid_2
139gcc-snap -Osthomas
145gcc-snap -Osmatthewf_2
145clang-snap -Oschucky_2
148gcc-4.4 -Osmikael_2
159gcc-snap -Osgreg
160gcc-snap -Osphil
165gcc-snap -Osmattias
172icc-13.1.0 -O1stefan
173gcc-4.6 -Osmatthew_3
175gcc-4.4 -Ossidney
176icc-13.1.0 -O1patrick
182gcc-snap -Osbastian
185gcc-4.7 -Osandrew
203clang-3.2 -Ostill
232clang-3.1 -Osolivier
251gcc-snap -Oskevin
275gcc-snap -Osrenaud
276gcc-4.6 -Osben
283gcc-snap -Osethan_2
312clang-snap -Osmats

The libc entry doesn’t count, it’s just a call to strtol that has been wrapped with a bit of logic to make it conform to the str2long specification. Perhaps interestingly, no compiler consistently wins. Happily, -Os is the most popular compiler flag.

Performance was measured by taking the best out of 10 runs on an otherwise idle Core i7-2600. The metric is the average number of nanoseconds required to convert a string including whatever overhead is incurred by the test harness. As Robert Graham observed, the test cases are perhaps skewed too heavily towards invalid inputs, which would tend to penalize code optimized for the valid case.

Here are the results:

nscompilersubmission
21.4icc-13.1.0 -O2bernd_2
21.6icc-13.1.0 -Ospeter
22.4gcc-4.7 -O2yang_2
22.5clang-3.0 -O1ryan
22.6gcc-4.7 -O2till
23.8icc-13.1.0 -O1yolanpa
24.7gcc-4.7 -O2tordek_2
25.3clang-3.2 -Osandrew
25.6gcc-4.4 -O3davidl_2
25.7gcc-4.7 -O2pascal
25.7gcc-4.5 -O3renaud
26.0gcc-4.7 -O2davidl
26.2icc-12.0.5 -O3stefan
26.8gcc-snap -O3bastian
27.1gcc-4.7 -O2chucky_2
27.2clang-3.1 -O3olivier
27.5gcc-snap -O3mikael_2
27.8gcc-4.7 -O2kevin
27.9clang-3.2 -O2davide
28.3gcc-snap -O3david_2
28.5clang-3.2 -O1patrick
28.7gcc-snap -O3sidney
29.6gcc-4.7 -O3phil
32.1gcc-snap -O3greg
33.1clang-3.1 -O2ben
33.1icc-12.0.5 -Osjeffrey
33.8icc-13.1.0 -O2mattias
34.5gcc-snap -O2thomas
34.6clang-snap -O3matthewf_2
37.5gcc-4.7 -O3matthew_3
49.0clang-3.1 -O1francois_2
52.8icc-12.0.5 -O2libc
96.4gcc-4.7 -Osdaniel_2
103.3gcc-4.4 -O3mats
128.3gcc-4.4 -O2ethan_2

Again, no particular compiler wins, and this time no particular optimization flag consistently wins either. The strtol code from libc performs surprisingly well considering that it is implementing a slightly more generic specification than str2long.

These results would perhaps be improved by repeating them on a modern ARM platform and some good ARM compilers, but I don’t have those available right now.

This concludes the objective portion of the contest results. The followup will be subjective and will look at the strategies and idioms that people used to implement the str2long specification without overflowing integers.

Thanks to everyone who submitted code! This has been fun.