• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
NSPopUpButton addItemsWithTitles is way slow
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSPopUpButton addItemsWithTitles is way slow


  • Subject: NSPopUpButton addItemsWithTitles is way slow
  • From: Dave Camp <email@hidden>
  • Date: Thu, 15 Aug 2002 17:54:35 -0700

My app has a popup menu with a list of countries to choose from (bit over 200 I think). The country names are in a sorted NSArray, and there are no duplicates.

My first attempt at populating the menu was [popup addItemsWithTities: myArray]; However, this is terribly slow (several seconds). Stopping in the debugger shows that NSPopUpButton iterates my array, and before adding each item it checks to see if there is already an item in the menu with the same title. Obviously, this is a poor choice for me (over 20,000 string compares).

My second attempt at this was to abuse some code recently recently posted to the list:

int index;
for (index = 0; index < [items count]; index++)
{
NSMenuItem *newItem;
newItem = [[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:[items objectAtIndex:index]
action:NULL keyEquivalent:@""];
[newItem setTarget:self];
[newItem setAction:@selector(myAction:)];
[[popup menu] addItem:newItem];
[newItem release];
}

This code is much faster, as NSMenu does not perform duplicate item checking (which I don't need). However, the first time I press any command key combination (Cmd-Q, Cmd-C, whatever) I get the spinning rainbow cursor and my CPU goes to 100% for about 20 seconds. After that things are back to normal. If I stop the code in the debugger while the CPU is pegged, I see:

#0 0x9321408c in -[NSMenuItem _isHidden] ()
#1 0x93080474 in _NSMenuToCarbonIndex ()
#2 0x9307fd7c in AddCarbonMenuItem ()
#3 0x93080234 in CreateCarbonMenu ()
#4 0x930814b8 in _NSGetMenuItemForCommandKeyEvent ()
#5 0x9320f064 in -[NSMenu performKeyEquivalent:] ()
#6 0x930cdd34 in -[NSApplication sendEvent:] ()
#7 0x930ca524 in -[NSApplication run] ()
#8 0x930d2598 in NSApplicationMain ()
#9 0x0000426c in main (argc=1, argv=0xbffffd7c) at main.m:15
#10 0x00003ff0 in _start (argc=1, argv=0xbffffd7c, envp=0xbffffd84) at /SourceCache/Csu/Csu-45/crt.c:267
#11 0x00003e70 in start ()

I'm guessing it's doing some sort of parsing of the menu items I inserted trying to figure out key bindings or something.

There must be a way of doing this without a performance penalty. What am I doing wrong?

Thanks,
Dave
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: NSPopUpButton addItemsWithTitles is way slow
      • From: Jeremy Dronfield <email@hidden>
  • Prev by Date: Re: Waking up the Finder
  • Next by Date: Re: Programming style (was Accessors) revisited ; de-compiler ???
  • Previous by thread: Question about NSMatrix (was Re: Setting a class' action)
  • Next by thread: Re: NSPopUpButton addItemsWithTitles is way slow
  • Index(es):
    • Date
    • Thread