• 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
Re: NSPopUpButton addItemsWithTitles is way slow
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSPopUpButton addItemsWithTitles is way slow


  • Subject: Re: NSPopUpButton addItemsWithTitles is way slow
  • From: Matt Neuburg <email@hidden>
  • Date: Fri, 16 Aug 2002 21:18:58 -0700

On Thu, 15 Aug 2002 17:54:35 -0700, Dave Camp <email@hidden> said:

>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).

Yes - I regard this behavior as a bug, by the way, since it isn't
documented and besides, when I say add an item I mean add an item,
regardless of whether another item with that title already exists.

Still, I don't agree about the speed. I just tried this:

int i;
[popup1 removeAllItems];
for (i=0; i<300; i++) {
[popup1 addItemWithTitle: [NSString stringWithFormat: @"%i", i]];
}

and it certainly didn't take "several seconds" to complete. Perhaps there's
something else at play here.

>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

What's all that target-action-key equivalent foo? The button has a target;
that's all you need. Abuse this instead:

NSMenu* m = [[NSMenu alloc] init];
NSMenuItem* mi;
int i;
for (i=0; i<300; i++) {
mi = [[NSMenuItem alloc] init];
[mi setTitle: [NSString stringWithFormat: @"%i", i]];
[m addItem: mi];
[mi release];
}
[popup2 setMenu: m];

m.

--

matt neuburg, phd = email@hidden, http://www.tidbits.com/matt
pantes anthropoi tou eidenai oregontai phusei
Subscribe to TidBITS! It's free and smart. http://www.tidbits.com/
_______________________________________________
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: NSTextFieldCell & editWithFrame
  • Next by Date: Re: best way to refuse events?
  • Previous by thread: Re: NSPopUpButton addItemsWithTitles is way slow [solved - sort of]
  • Next by thread: Re: NSPopUpButton addItemsWithTitles is way slow
  • Index(es):
    • Date
    • Thread