I ran these tests using
Apple Swift version 4.1.2 (swiftlang-902.0.54 clang-902.0.39.2)
Target: x86_64-apple-darwin17.6.0
building with just swiftc -O
. Here are my results (numbered the same way yours are):
$ ./main1
compactMap: 375000000000, took 0.289310122 s
filterMap: 375000000000, took 0.000821735 s
$ ./main2
compactMap: 375000000000, took 0.316243825 s
filterMap: 375000000000, took 0.000814479 s
compactMap is 388 times slower than filterMap
$ ./main3
compactMap: 375000000000, took 0.288270726 s
filterMap: 375000000000, took 0.000808948 s
compactMap is 356 times slower than filterMap
$ ./main4
compactMap: 375000000000, took 0.288978948 s
filterMap: 375000000000, took 0.001179241 s
compactMap is 245 times slower than filterMap
compactMap: 375000000000, took 0.295771218 s
filterMap: 375000000000, took 0.00117926 s
compactMap is 251 times slower than filterMap
compactMap: 375000000000, took 0.298000228 s
filterMap: 375000000000, took 0.001179243 s
compactMap is 253 times slower than filterMap
compactMap: 375000000000, took 0.287790936 s
filterMap: 375000000000, took 0.00118008 s
compactMap is 244 times slower than filterMap
compactMap: 375000000000, took 0.290857785 s
filterMap: 375000000000, took 0.001179366 s
compactMap is 247 times slower than filterMap
$ ./main5
compactMap: 375000000000, took 0.291611994 s
filterMap: 375000000000, took 0.001179274 s
compactMap is 247 times slower than filterMap
compactMap: 375000000000, took 0.299940731 s
filterMap: 375000000000, took 0.001179274 s
compactMap is 254 times slower than filterMap
compactMap: 375000000000, took 0.2925131 s
filterMap: 375000000000, took 0.001179412 s
compactMap is 248 times slower than filterMap
compactMap: 375000000000, took 0.29145278 s
filterMap: 375000000000, took 0.001179271 s
compactMap is 247 times slower than filterMap
compactMap: 375000000000, took 0.297499215 s
filterMap: 375000000000, took 0.001179322 s
compactMap is 252 times slower than filterMap
$ ./main6
compactMap: 375000000000, took 0.309454594 s
filterMap: 375000000000, took 0.000833356 s
compactMap is 371 times slower than filterMap
compactMap: 375000000000, took 0.319849605 s
filterMap: 375000000000, took 0.000808931 s
compactMap is 395 times slower than filterMap
compactMap: 375000000000, took 0.31500959 s
filterMap: 375000000000, took 0.000808922 s
compactMap is 389 times slower than filterMap
compactMap: 375000000000, took 0.298115479 s
filterMap: 375000000000, took 0.000808922 s
compactMap is 369 times slower than filterMap
compactMap: 375000000000, took 0.300710155 s
filterMap: 375000000000, took 0.000808922 s
compactMap is 372 times slower than filterMap
so it appears that I can't reproduce the slowdown between 1, 2, and 3, but the performance regression comes in for 4 and 5 and is again resolved in 6.