Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Case 1</strong></p> <p>Of the options listed, I'd use:</p> <pre><code>NSMutableString *str3 = [NSMutableString stringWithCapacity:1000]; </code></pre> <p>…if you know the destination size, or estimate it with a little room at the top and are able to quickly determine the exact size, or the size worst case scenario, this could save multiple reallocate and copy operations. If you don't know the size in the worst case scenario, or if it takes a lot of time to calculate, then you may as well use <code>[NSMutableString string]</code> or <code>[NSMutableString new]</code>. Also, <code>*WithCapacity</code> is a hint, which the frameworks are free to ignore.</p> <p>Of course, the body of your loop and the size you reserve also implies that all the values are [0…9] (specifically, that all values consume one character), and you could in that case likely do far better by using format strings with more arguments. However, <code>i</code> is obviously larger than 9 for most iterations, and will consume on average 3 characters each, so 3000 would be a more appropriate reserve capacity for the exact code you posted.</p> <p><strong>Case 2</strong></p> <p>Of the options listed, I'd use:</p> <pre><code>NSMutableString *str4 = [NSMutableString string]; </code></pre> <p>Even better, if you don't need to add it to an autorelease pool: <code>[NSMutableString new]</code> or <code>[[NSMutableString alloc] init]</code>.</p> <p><strong>Other Notes</strong></p> <p>Yes, keeping objects out of autorelease pools (e.g. use alloc+init) can improve performance and reduce peak memory usage significantly. Sometimes, this is beyond your control, and in some environments (e.g. ARC), this may happen even though you use an autoreleased convenience constructor - e.g. <code>[NSMutableString string]</code>.</p> <p><strong>The Faster Solution</strong></p> <p>Finally, if this case you have outlined really is a performance concern, the fastest way would be to create a <code>char</code> buffer on the stack and then create one <code>NSString</code> from the result of copying the numbers over to the <code>char</code> buffer. Assuming your <code>int</code>s are all 0-9, it would be very fast and easy, then simply create an <code>NSString</code> from the (terminated) cstring. You can even do this if the input size varies, or is very large (results in a very long string).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. COThe difference in memory usage between an `@autoreleasepool` block and manually releasing at the end of the scope shouldn't be all that great. So unless you're peppering releases all through your code (which does free up the memory more quickly but IMO makes it harder to see whether you released it at all), I'm skeptical about the difference in most cases. Out of curiosity, do you have any hard numbers on this?
      singulars
    2. CO@Chuck that is, however, not typical real world execution - very very few developers explicitly create autorelease pools regularly (beyond entry of a new thread/program). so the pools and the dependencies of the objects become large often. there are also a number of factors which reduce performance, it's not simply allocation count. belief that one can/should be oblivious to what 'the system handles efficiently, and automatically' leads to problems (cont)
      singulars
    3. CO(cont) not unlike objc garbage collection activity requiring a *significant* amount of a process's CPU time (a real example from OS X). and yes, i've extensively optimized a number of cocoa programs. the short of it is: explicitly controlling and minimizing objects' lifetimes, using immutable types in the appropriate places, etc. can make a program's execution a few times faster. one link exists in Rickay's post (which you have also commented on, so... I would have assumed that you saw it).
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload