• 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: Cocoa optimization
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Cocoa optimization


  • Subject: Re: Cocoa optimization
  • From: David Hendrix <email@hidden>
  • Date: Wed, 9 Nov 2005 01:55:31 -0800


On Nov 9, 2005, at 1:37 AM, j o a r wrote:


On 9 nov 2005, at 10.28, David Hendrix wrote:
The NS* functionality probably does a lot of things that you wouldn't consider at first glance. To answer your question properly we also need to know exactly what you mean when you say "malloc/ sprintf".

Yes, I know there's a *lot* of very cool functionality built in. I take advantage of that in many other places in the code -- in this particular bit of the code, though, I'd trade that for pure speed.


Formatting strings in C is pretty tricky to do right. The functionality provided by Cocoa probably tries to be secure and correct, rather than blazing fast.

In this case, it's fairly simple, but variable enough so that a "sprintf" like function is necessary...


To crib from my simple test application -- the C version would look something like:

    char buffer[80];

    sprintf(buffer, "This is record %d of %d", currentIndex, limit);
    char *foo = malloc(strlen(buffer) + 1 * sizeof (char));
    strcpy(foo, buffer);

Where the Objective-C app would do either:

    char buffer[80];

sprintf(buffer, "This is record %d of %d", currentIndex, limit);
NSString *foo = [NSString stringWithCString:buffer encoding:NSASCIIStringEncoding];


or the more simple:

NSString *foo = [NSString stringWithFormat:@"This is record %d of %d", currentIndex, limit];

The first of the two NSString versions seems to be slightly faster, but not blindingly so.

Have you tried to check what "+stringWithFormat:" is doing using Shark? It will probably tell you all you need to know.

That's a good suggestion -- I'll give it a shot.

Have you looked at the functionality provided by CFString? It's usually a bit faster than it's NS equivalent for performance critical code.

I did -- it didn't seem to be significantly faster, though that may have been my fault for using the default allocator, maybe.


_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Cocoa optimization
      • From: "Timothy J. Wood" <email@hidden>
References: 
 >Cocoa optimization (From: David Hendrix <email@hidden>)
 >Re: Cocoa optimization (From: j o a r <email@hidden>)

  • Prev by Date: Re: Cocoa optimization
  • Next by Date: Re: Casting anonymous Objects leads to different values on getter functions (while getting no warnings at compile time)
  • Previous by thread: Re: Cocoa optimization
  • Next by thread: Re: Cocoa optimization
  • Index(es):
    • Date
    • Thread