[swift-users] Performance critical code in Swift

Jens Persson jens at bitcycle.com
Sat Oct 1 16:13:15 CDT 2016


You could write some examples in both C and Swift in order to gain
experience in how to write your Swift code so that it will (probably) run
as fast as (or faster than) your corresponding C code.

I've done this for a number of different performance critical things and it
is often possible to get the Swift version as fast as the C version.

(If you find something that is not possible (or unnecessarily cumbersome)
to write as fast in Swift as in C, you could probably file a
bug/improvement on bugs.swift.org. I did this some time ago when I noticed
that Swift's optimizer missed an opportunity to unroll loops in a certain
situation and it turned out that a fix/optimizer-improvement was already on
its way.)

You must of course profile/microbenchmark your code in some meaningful way,
setting all relevant compiler flags for both C and Swift, preventing dead
code elimination, measuring average or perhaps median times of lots of
tests, making sure what should and shouldn't be statically knowable, etc.

In Swift you probably want -O -whole-module-optimization, and (rarely) you
might want/need to disable safety checks.

/Jens



On Sat, Oct 1, 2016 at 10:33 PM, Daniel Dunbar via swift-users <
swift-users at swift.org> wrote:

> Yes, it is possible. Exactly how much use of Unsafe style idioms and other
> performance-focused "workarounds" it requires depends a lot on the code in
> question. Can you say more about your problem area?
>
>  - Daniel
>
> > On Oct 1, 2016, at 1:30 PM, Игорь Никитин via swift-users <
> swift-users at swift.org> wrote:
> >
> > Hello!
> > Is it possible for Swift to be as fast as C when writing performance
> critical code? Of course if using C Standard Library for instead of
> Foundation (and so on) and getting rid of dynamic dispatch and reference
> types.
> > Or I need just to use C?
> > _______________________________________________
> > swift-users mailing list
> > swift-users at swift.org
> > https://lists.swift.org/mailman/listinfo/swift-users
>
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20161001/5be83056/attachment.html>


More information about the swift-users mailing list