Does #import on pch slow down the compile

2020-07-11 08:22发布

I was reading this post about imports and I had one question. Does the #import that comes in the prefix.pch file by default slow down the compile time? Should I remove it and import only when necessary?

#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
#endif

3条回答
萌系小妹纸
2楼-- · 2020-07-11 08:48

This may sound controversial, but contrary to what everyone is saying about modules, I've had the complete opposite experience. I've got, what you'd consider, a large project (considerably large with various dependencies and libraries). Recently I started noticing that compile times were very very very long (around 5 minutes normal build time and around 15 minutes 'archive' time on a 2014 Mac Pro 12 cores and 32 GB RAM).

I ran an experiment by simply turning off Enable Link Frameworks Automatically on all libraries and projects as well as Enable Modules. I 'archived' and it took 3 minutes archive.

I turned 'Enable Modules' ON and did the same, and it took, as normal, 15 minutes. Strange and weird, but I'm sticking with these options turned off.

查看更多
疯言疯语
3楼-- · 2020-07-11 08:53

No. It actually improves the compilation speed.

This is a nice tutorial that actually clears all the confusion over use of #import statements and .PCH files. Also it tells you in detail about something new known as "modules", introduced in iOS7.

查看更多
劳资没心,怎么记你
4楼-- · 2020-07-11 09:05

Since Xcode 5, there is a new feature introducing precompiled sources database. Xcode 5 basically compiles all the required frameworks just once, saves builds in the database and that already compiled pieces uses while compiling your code. So nothing in the library is not compiled multiple times.

So my guess is, while using Xcode 5, you don't need to worry.

You can watch WWDC 2013 video "What’s New in Xcode 5" introducing this feature at https://developer.apple.com/wwdc/videos/

If I'm wrong, please anyone more informed, feel free to correct me.

查看更多
登录 后发表回答