{"id":9567,"date":"2014-09-13T15:32:25","date_gmt":"2014-09-13T19:32:25","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=9567"},"modified":"2014-10-19T13:58:57","modified_gmt":"2014-10-19T17:58:57","slug":"swift-1-0-performance-and-compilation-times","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2014\/09\/13\/swift-1-0-performance-and-compilation-times\/","title":{"rendered":"Swift 1.0 Performance and Compilation Times"},"content":{"rendered":"<p><a href=\"http:\/\/blog.metaobject.com\/2014\/09\/no-virginia-swift-is-not-10x-faster.html\">Marcel Weiher<\/a>:<\/p>\n<blockquote cite=\"http:\/\/blog.metaobject.com\/2014\/09\/no-virginia-swift-is-not-10x-faster.html\"><p>About a month ago, Jesse Squires published a post titled <a href=\"http:\/\/www.jessesquires.com\/apples-to-apples-part-two\/\">Apples to Apples<\/a>, documenting benchmark results that he\nclaims show Swift now with a roughly 10x performance advantage over Objective-C.  \nAlthough completely bogus,\nthe post was retweeted by Chris Lattner (who should know better, and was supposedly\nmostly interested in highlighting the improvements in the Swift optimizer, rather than\nthe bogus comparison) and has now been <a href=\"http:\/\/rustyshelf.org\/2014\/08\/07\/thoughts-on-swift-from-an-idiot\">referenced<\/a> a number of times as background \nknowledge as to the state of Swift.  More importantly, though the actual mistake\nJesse makes is pretty basic and not that interesting, it does point to some deeper\nmisunderstandings about performance and language that I at least do find interesting.<\/p>\n<p>[&#8230;]<\/p>\n<p>A second takeaway is that the question &ldquo;which language is faster&rdquo; doesn&rsquo;t really make sense, a more relevant and interesting question is &ldquo;does this language make it hard\/possible\/easy to write fast code&rdquo;. Objective-C lets you write really fast code, if you want to, because it has the low-level chops and an understandable performance model. Swift so far can achieve reasonable performance at times, ludicrously bad at other times (especially with the optimizer turned off, which hardly fazes Objective-C), with as far as I can tell fairly little predictability or control. Having 10% faster (or slower) performance for code I don&rsquo;t particularly care about is not worth nearly as much as the knowledge that I can get the 1-5% of code that I do care about in shape no matter what. Swift is definitely not there yet, and given the direction it is taking I am not sure whether it will allow that kind of control, at least in comprehensible ways.<\/p>\n<p>[&#8230;]<\/p>\n<p>A third point is something more general about language.  The whole argument that \nNSNumber and NSArray are &ldquo;built in&rdquo; somehow and int is not displays a lack of\nunderstanding of Objective-C that to me seems staggering.  Even more so, the \nwhole idea that you must only use what comes provided with Cocoa and are not\nallowed to build your own flies in the face of modern language design, throwing\nus back to the times of BASIC (<a href=\"http:\/\/www.folklore.org\/StoryView.py?project=Macintosh&amp;story=MacBasic.txt\">Cathy Doser<\/a>, in the comments).<\/p><\/blockquote>\n<p><a href=\"http:\/\/swiftopinions.wordpress.com\/2014\/09\/12\/does-swift-have-performance-issues\/\">Christoffer Lern&ouml;<\/a>:<\/p>\n<blockquote cite=\"http:\/\/swiftopinions.wordpress.com\/2014\/09\/12\/does-swift-have-performance-issues\/\"><p>Recently I discovered that our current project is seeing O(n&#178;) compile times. While a small project compiles real fast, compile times for projects with 20.000+ lines of code will easily take a few minutes to compile.&nbsp;This is Swift supposedly at 1.0 with no optimizations turned on.<\/p>\n<p>In other news, using Dictionary instead of NSDictionary when interfacing with ObjC code was able to incur a performance penalty of over 14000%. Yes, that&rsquo;s Swift being 140 times slower than using NSDictionary.<\/p>\n<p>Furthermore, putting your source code in the same or different files can give you a performance penalty of over 3000% in itself. Again, yes that is Swift being 30 times slower if you call something in a different&nbsp;<strong>file<\/strong>.<\/p>\n<p>[&#8230;]<\/p>\n<p>I&rsquo;d like to see Swift succeed. It is certainly less bulky than Objective-C. However, when Swift becomes unworkable for projects exceeding 15.000 LOC, then no amount of liking the language is going to help. Nor will you be able to replace it with ObjC\/C if you have a performance sensitive application.<\/p><\/blockquote>\n<p><a href=\"https:\/\/twitter.com\/mpweiher\/status\/510361435441229825\">Marcel Weiher<\/a> on the compilation time:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/mpweiher\/status\/510361435441229825\"><p>1000x slower? Wow, that&rsquo;s worse than I would have expected (and I expected bad). Unlikely to change significantly (see C++,Scala etc)<\/p>\n<p>Expensive default semantics, 100% reliance on optimizer to get remotely reasonable perf., unpredictable performance model.<\/p><\/blockquote>\n<p>I still like the potential of Swift, but it sounds like it&rsquo;s currently very easy to paint yourself into a corner. It is troubling that basic features <a href=\"https:\/\/devforums.apple.com\/thread\/244975?tstart=0\">like dictionaries<\/a> are so slow.<\/p>\n<p>Update (2014-09-14): <a href=\"http:\/\/swiftopinions.wordpress.com\/2014\/09\/13\/swift-1-0-some-caution-recommended\/\">Christoffer Lern&ouml;<\/a>:<\/p>\n<blockquote cite=\"http:\/\/swiftopinions.wordpress.com\/2014\/09\/13\/swift-1-0-some-caution-recommended\/\"><p>Since Scala is a bit infamous for slow compilation times &#8211; partly attributed to type inference &#8211; my fear has been that Swift would remain fairly slow at compilation. However, I&rsquo;ve been assured that Swift&rsquo;s model is much closer to that of F# and C#. The current O(n&#178;) compiler times are due to bugs and not due to any inherent complexity in the language itself.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Marcel Weiher: About a month ago, Jesse Squires published a post titled Apples to Apples, documenting benchmark results that he claims show Swift now with a roughly 10x performance advantage over Objective-C. Although completely bogus, the post was retweeted by Chris Lattner (who should know better, and was supposedly mostly interested in highlighting the improvements [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[69,255,31,46,30,54,138,71,901],"class_list":["post-9567","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-compiler","tag-ios","tag-languagedesign","tag-mac","tag-objective-c","tag-optimization","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/9567","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=9567"}],"version-history":[{"count":8,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/9567\/revisions"}],"predecessor-version":[{"id":9922,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/9567\/revisions\/9922"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=9567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=9567"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=9567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}