Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift app only performing reasonably when built for Xcode's Time Profiler?

I'm working on an app that processes large float arrays, and I was extremely disappointed to find that Swift is apparently a good 10x slower than Python when running on my iPhone 5. I guess that can't be true, but I would have thought it true had I not tested the app in the Time Profiler and realized that it's probably not my algorithm or Swift's implementation causing the issues.

When the app is built for the profiler, it performs nicely: It does the processing in an imperceptibly small amount of time, as it should. But normally, if I hit the run button (play symbol) in Xcode to build and run, even with the compiler set to be Fastest, it takes almost 20 seconds to complete. (Code remains unchanged between builds.) It's ridiculously slow.

It's reasonably fast when I build and run in a simulator on my MacBook Pro, but even then it's slower than it is when built and run through the Time Profiler on my little iPhone 5.

The performance difference between builds is also present when I place the following test code in the application() function in AppDelegate, so I don't think it's a GUI/threading issue or anything. And the same test code runs quickly when implemented in Objective-C (in a separate app on my device) and Python on my Mac.

var nums: [Float] = []

for var i = 0; i < 250000; i++ {
    nums.append(Float(i) * 0.001)
}

(This takes around 20 seconds unless run through the Time Profiler.)

Have any of you experienced anything like this before? I'd be immensely happy if you could help me figure this out.

like image 825
ICoffeeConsumer Avatar asked Mar 11 '15 00:03

ICoffeeConsumer


People also ask

Which of the following statements correctly describe use of the time profiler tool in the iOS instruments APP application?

The Time Profiler instrument gives insights into the system's CPUs and how effective multiple cores and threads are used. The better this part in your app is, the better your app is performing.

Do you use Xcode's instruments which ones?

Xcode Instruments is best described as a powerful and flexible performance-analysis and testing tool. It's part of the Xcode toolset, which also includes tools like Create ML and the Accessibility Inspector. The instruments can be used to: Track down problems in your source code.


1 Answers

You were building the app in Debug mode instead of Release mode. Debug mode is much easier to debug, but release mode generates faster code.


When building for Debug mode, the compiler inserts special debug symbols that help the debugger. I once tried to debug an app in Release mode and the debugger couldn't even find my variables. Release mode not only doesn't include those symbols, but it also optimizes your app, generating a binary that is much smaller and faster.


Here's how to switch between Debug and Release mode:

  • Click on your scheme in the top-left corner of Xcode.

Click on your scheme in the top-left corner of Xcode.

  • Select "Edit Scheme..."

Select "Edit Scheme..."

  • Click on the "Build Configuration" dropdown.

Click on the "Build Configuration" dropdown.

  • Change the build configuration to Release.

Do I really need to provide alt text here?

  • Rebuild your project.

(I am not providing an image here because I assume you know how to rebuild your project.)

like image 101
NobodyNada Avatar answered Sep 19 '22 23:09

NobodyNada