0

In my app, a user can "speed-read" text by having words flashed on the screen at a speed that they set. I have coded up this functionality in my UIViewController using a repeating NSTimer and updating the UILabel by displaying the next word index, but it's not going as fast as it should be.

For example, I tested it with 100 words at 1000 words per minute. Instead of taking 6 seconds like it should be, it takes 6.542045 to finish flashing all of the words. This is a big problem since I'm supposed to spit back to user user how long it took for them to read the text.

How do I find out what part of the code is taking so long? Is it the updating of the UILabel that's eating up 0.54~~ of the time?

EDIT

My sample project can be viewed here: https://github.com/cnowak7/RSVPTesting

The flashText method that I have should be firing only 100 times. Well, 101 if we count the time when the method realizes there are no more words and terminates the NSTimer. In the console, at the end of reading, I can see that the method is being fired 111 times. I think I might be doing this the wrong way.

Rafi
  • 1,902
  • 3
  • 24
  • 46
  • Please show us your code – Luca Angeletti Jun 17 '16 at 00:24
  • I'm almost finished putting together a small sample project that I'll post to github. It'll be up very soon. – Rafi Jun 17 '16 at 00:26
  • How fast is your timer 'ticking'? NSTimer is not a high-resolution timer. A tick at .001s is approaching the accuracy limits - http://stackoverflow.com/questions/11835023/nstimer-accuracy – Paulw11 Jun 17 '16 at 00:55
  • My sample project can be viewed here: https://github.com/cnowak7/RSVPTesting – Rafi Jun 17 '16 at 01:26
  • Your sample project doesn't display a word for one timer cycle at the end of each sentence. – Paulw11 Jun 17 '16 at 02:05
  • 2
    Your code should look like this -https://gist.github.com/paulw11/330180bae16730f9964f0b29c0d1494c – Paulw11 Jun 17 '16 at 02:07
  • @Paulw11 *face palm* I can't believe I didn't see that earlier! That's what I get for trying to be too careful with conditional checking. – Rafi Jun 17 '16 at 02:27

2 Answers2

2

Your specific question seems to be: How do I find out what part of the code is taking so long? Is it the updating of the UILabel that's eating up 0.54~~ of the time?

Inside Instruments, provided with Xcode, is a Time Profiler tool. https://developer.apple.com/library/ios/documentation/DeveloperTools/Conceptual/InstrumentsUserGuide/Instrument-TimeProfiler.html

You can run your code and watch this tool to see exactly how much time is being spent executing every part of your routines. It will break down exactly which method is taking the most time, by percentage of overall time and concrete time spans, giving you a precise understanding of where you should focus your efforts in shaving off those precious partial seconds through refactoring/optimizations.

I'm an Objective-C guy, so rather than try to muddle my way through a Swift example, I'll let this guy do the talking. https://www.raywenderlich.com/97886/instruments-tutorial-with-swift-getting-started

christopherdrum
  • 1,513
  • 9
  • 25
1

Whenever you want to know about time consumption in iOS you should always go for Instruments and select time profiler as shown in the image.enter image description here

Time profiler will help you get to the code which is taking too much time.

Usuf
  • 2,569
  • 1
  • 13
  • 10