如何测量NSRunLoop / CFRunLoop的时间间隔?

时间:2014-02-14 14:56:15

标签: objective-c cocoa nsautoreleasepool nsrunloop

当谈到NSAutoreleasePool时,人们总是说NSRunLoop。我可以找出一个runloop循环有多长?


rob mayoff的评论非常有帮助。还有一些示例代码需要测试。

#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;
}

0 个答案:

没有答案