2

I'm using ScriptingBridge.framework in order to interact with Mail.app. The following code is taking about 30 seconds to load 100 messages. I'm running it on a 2.8GHz Core i7 MacBook Pro with 4GB (1333 MHz DDR3) mem. My OS is 10.7.4.

MailApplication *mailApp = [SBApplication applicationWithBundleIdentifier:@"com.apple.mail"];
MailMailbox *inbox = [mailApp inbox];
SBElementArray *messages = [inbox messages];

NSMutableArray *tmp = [NSMutableArray arrayWithCapacity:[messages count]];
for (MailMessage *message in messages) {
   RRMailItem *mailitem = [RRMailItem new];
   [mailitem setSender:[message sender]];
   [mailitem setSubject:[message subject]];
   [mailitem setDate:[message dateSent]];

   if (message.mailAttachments.count > 0) {
      [mailitem setHasAttachment:YES];
   }

   [tmp addObject:mailitem];
}

RRMailItem is a simple object as follow. It's just an object to hold values. It doesn't have any method:

@interface RRMailItem : NSObject

@property NSString *sender;
@property NSString *subject;
@property NSDate *date;
@property BOOL hasAttachment;

@end

If I remove the if (message.mailAttachments.count > 0), the execution time goes down by 50%, to 15 seconds to load the same 100 messages. Much better, but still high. And I need the IF...

How can I improve the code performance? Any hints and tips are welcome.

TIA,

Bob

Bob Rivers
  • 5,261
  • 6
  • 47
  • 59
  • Don't know objective-c specifically but it looks like it could be a branch prediction. Take a look here: http://stackoverflow.com/questions/11227809/why-is-processing-a-sorted-array-faster-than-an-unsorted-array – Raekye Jul 13 '12 at 01:34
  • http://codereview.stackexchange.com/ – ihake Jul 13 '12 at 01:34
  • Have you used Instruments to see what is really going on? – sosborn Jul 13 '12 at 01:39
  • If the RRMailItem is your custom class then create a method to set sender, subject and dataSent together instead of one by one – TeaCupApp Jul 13 '12 at 02:46