I am working on an emulator as a side project, but I am having performance issues and cannot figure out where they come from.
The application mainly consists of GLKView for display and a separate thread with an infinite loop for emulating the processor. Here's a sample with the actual emulation code that still displays the problem:
@implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; GLKView *glView = [[GLKView alloc] initWithFrame:self.view.bounds]; glView.delegate = self; glView.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; [EAGLContext setCurrentContext:glView.context]; [self.view addSubview:glView]; glView.enableSetNeedsDisplay = NO; CADisplayLink* displayLink = [CADisplayLink displayLinkWithTarget:glView selector:@selector(display)]; [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; dispatch_get_main_queue(), ^{ dispatch_async(dispatch_queue_create("yeah", DISPATCH_QUEUE_SERIAL), ^{ CFTimeInterval lastTime = 0; CFTimeInterval time = 0; int instructions = 0; while(1) {
Similarly, endless loops basically just count the iterations and print its frequency in MHz.
So, the problem is that when the application starts, the cycle runs at about 9-15 MHz (on iPhone6), and if I look at the GPU report in Xcode Debug Navigator, I see that the processor frame time is 0.2 ms Then, after start in a few seconds, the cycle drops to 1-5 MHz, and the processor frame time increases to 0.6 ms
If I turn off GLKView updates, the loops will never get slower
I also tried using various streaming APIs (gdc, NSThread, pthread), but this does not seem to have any effect.
My question is: am I doing something wrong? Is this just the case when GLKView is not fully initialized for a couple of seconds, and therefore, using less processor than usual, and get speed acceleration? Any other ways that I could structure my code to get maximum performance in a loop?
Update I did some more tests and noticed that the problem also occurs when using CAEAGLLayer instead of GLKView, also that this does not happen on the simulator only on the device. I also tried using an OS X application with NSOpenGLView and this will not happen ...
Update 2 I tried to start the stream after a while, and not immediately, and if the delay is longer than the time it usually takes for a drop, the stream starts to slow down ... I'm not quite sure what to do with it ...
Metal update I tried to use Metal instead of OpenGL, using a simple template template from Xcode, and this also happens to it ...