1

Hey there, I am fresh to iPhone development and Objective C. Sorry if this question is too stupid....

Here is my problem, I want to calculate the time taken by one of my function. like UIGetScreenImage. Here is the code:

-(void)screenCapture{
   CGImageRef screen = UIGetScreenImage();
   UIImage* image = [UIImage imageWithCGImage:screen];
   CGImageRelease(screen);
   UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}

what should I do to calculate the time taken by this process? Sample code would be appreciated.

Thanks for your kind assistance. Look forward to your replies and ideas. :D

2 Answers 2

4

You can get current date on method start and finish and check time passed between those 2 moments:

-(void)screenCapture{ NSDate* startDate = [NSDate date]; ... NSDate* finishDate = [NSDate date]; NSLog(@"%f", [finishDate timeIntervalSinceDate: startDate]); }

Edit: I believe my approach described above is (to put it mildly) not the best solution to measure process time. Now I use approach described in "Big Nerd Ranch" blog here that uses mach_absolute_time function. I copied the code from the post to illustrate that method - with this code snippet you can measure rub time of arbitrary block:

#import <mach/mach_time.h>  // for mach_absolute_time() and friends

CGFloat BNRTimeBlock (void (^block)(void)) {
    mach_timebase_info_data_t info;
    if (mach_timebase_info(&info) != KERN_SUCCESS) return -1.0;

    uint64_t start = mach_absolute_time ();
    block ();
    uint64_t end = mach_absolute_time ();
    uint64_t elapsed = end - start;

    uint64_t nanos = elapsed * info.numer / info.denom;
    return (CGFloat)nanos / NSEC_PER_SEC;

} // BNRTimeBlock
3
  • hello, thanks for your prompt reply. Another problem here is where to find the log information about the time duration? Through Console of Mac? or somewhere else? Thanks again for your help :D
    – Nihao
    Oct 18, 2010 at 12:45
  • if you run application in debug mode then log messages will be on debugger console (cmd+shift+R shortcut). otherwise I suppose you can find output in device logs through organizer window, but using logs while debugging is more common
    – Vladimir
    Oct 18, 2010 at 12:51
  • Hey Vladimir, Thanks you so MUCH for your kind and detailed explanation. It really helps me a lot. Wish you a happy day :D
    – Nihao
    Oct 18, 2010 at 12:55
2

I think an easier (and more straightforward) solution can be found here

NSDate *methodStart = [NSDate date];

/* ... Do whatever you need to do ... */

NSDate *methodFinish = [NSDate date];
NSTimeInterval executionTime = [methodFinish timeIntervalSinceDate:methodStart];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.