1

When talk about NSAutoreleasePool, people always say NSRunLoop. Can I find out how long a runloop cycle is?


rob mayoff's comment is very helpful. And there is some sample code to test.

#include <Foundation/Foundation.h>

#include <time.h>

clock_t entryTM = 0;
clock_t exitTM = 0;

void callback(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info)
{
    exitTM = clock();
    NSLog(@"Total: %.5fs", (exitTM - entryTM) * 1.0/CLOCKS_PER_SEC);
    entryTM = clock();
}

int main(int argc, char const *argv[])
{
    CFRunLoopObserverRef observerRef = CFRunLoopObserverCreate(
        kCFAllocatorDefault,
        kCFRunLoopBeforeTimers,
        true,
        0,
        callback,
        NULL);
    CFRunLoopAddObserver(
        CFRunLoopGetCurrent(),
        observerRef,
        kCFRunLoopCommonModes);

    if (CFRunLoopContainsObserver(CFRunLoopGetCurrent(),
        observerRef,
        kCFRunLoopCommonModes))
    {
        NSLog(@"Has observer!");
    }

    [NSTimer scheduledTimerWithTimeInterval: 1 
        target: nil 
        selector: @selector(print) 
        userInfo: nil 
        repeats: YES];

    CFRunLoopRun();

    return 0;
}
wcrane
  • 1,195
  • 7
  • 18

0 Answers0