Swift on Linux: how to specify compiler optimizations

Several threads on stackoverflow (eg this one) discuss the different optimization levels ( Onone , O , Ounchecked ...) when compiling Swift applications.

However, those postings are related to the development on OSX. It seems that those optimizations can be set directly via Xcode or xcrun ( xcrun swift -O3 ).

I'm wondering how to switch the different optimization levels when using the Swift compiler directly on Linux (Ubuntu 15.10). Currently, I'm building the application just by invoking swift build as it is shown in the docs, but I found no way no change the optimization level.


It is possible to provide the -O , -Onone , and -Ounchecked optimization flags to the Swift compiler, swiftc . However, it appears that there is currently no way to specify additional flags to swift build . See, for example, the following link, even though it is not directly related: https://bugs.swift.org/browse/SR-397. The same bug report suggests that the team is actively working on adding this missing functionality.

One way that I found to work around the problem is to run swift build -v , find the first command that references -Onone , copy it and all the commands that follow it to a shell script, edit the script to use the desired optimization level instead of -Onone , and run the script. This should re-compile the Swift sources using the desired optimization level and rebuild the executable.

In my testing I found that a simple example involving sorting an array runs a couple of orders of magnitude faster if built using -O or -Ounchecked instead of -Onone .

链接地址: http://www.djcxy.com/p/31678.html

上一篇: Swift inout参数性能

下一篇: Linux上的Swift:如何指定编译器优化