• 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: Initializing a NSMutableString an odd way
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Initializing a NSMutableString an odd way


  • Subject: Re: Initializing a NSMutableString an odd way
  • From: Andreas Grosam <email@hidden>
  • Date: Sat, 03 Aug 2013 12:00:58 +0200

On 31.07.2013, at 22:01, Greg Parker <email@hidden> wrote:

> On Jul 31, 2013, at 12:28 PM, Vincent Habchi <email@hidden> wrote:
>> David Duncan wrote:
>>> Why would there be? Your just asking for a mutable copy of an empty string. It should be equivalent to [[NSMutableString alloc] initWithString:@«  »]
>>
>> But much slower I expect, since it creates a NSString, takes a mutable copy, then implicitly releases the constant empty NSString.
>
> For giggles I tried some NSMutableString allocation patterns into my microbenchmark test harness.
>
> Simple alloc/init is the fastest:
>
> 100  [[[NSMutableString alloc] init] release]
> 102  [[NSMutableString new] release]
> 109  [NSMutableString string]  // ARC enabled
> 117  [[@"" mutableCopy] release]
> 119  @autoreleasepool { [NSMutableString string]; }  // ARC disabled
> 129  [[[NSMutableString alloc] initWithString:@""] release]
>
> (Smaller numbers are better. Numbers are getrusage(RUSAGE_SELF) time for 10000000 iterations, normalized to fastest=100. Your mileage may vary.)


How much time contributes the allocation of the raw storage? I would guess that initializing an empty string is quite fast (while generally NSString is disappointing slow in almost all other functions).

What's also interesting is the time it takes to *deallocate* an object. Since ARC and weak pointers, this time increased dramatically (due to the requirement to remove the object from the "sparse array").


In those micro-benchmarks its also interesting how this benchmark affects the behavior of the heap. It's likely we see a particular edge case which is either fast or slow. I would expect that allocating and deallocating the same object in a loop leads to faster allocation times. As opposed to the case where differrent objects are allocated and deallocated in random order.


Andreas


>
> ARC and non-ARC scores are the same within measurement noise, except for [NSMutableString string] where ARC can optimize the autoreleased return value so the test doesn't need to spin the autorelease pool. Note th
>
>
>> BTW, what’s the difference between [[NSMutableString alloc] init] and [[NSMutableString alloc] initWithString:@“”]?
>
> Semantically there's no difference: you get the same string with the same retain count.
>
>
> --
> Greg Parker     email@hidden     Runtime Wrangler

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden


  • Prev by Date: Re: There must be a live human at Apple?
  • Next by Date: Re: There must be a live human at Apple?
  • Previous by thread: MODERATOR: End of Thread (was Re: There must be a live human at Apple?)
  • Next by thread: drawing outside the dirtyRect (of a NSView sub class) and overlapping NSTableView
  • Index(es):
    • Date
    • Thread