Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6237931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:07:19+00:00 2026-05-24T11:07:19+00:00

Is it better (faster & more efficient) to use alloc or autorelease initializers. E.g.:

  • 0

Is it better (faster & more efficient) to use alloc or autorelease initializers. E.g.:

- (NSString *)hello:(NSString *)name {
    return [[NSString alloc] initWithFormat:@"Hello, %@", name];
}

OR

- (NSString *)hello:(NSString *)name {
    return [NSString stringWithFormat:@"Hello, %@", name];
//    return [@"Hello, " stringByAppendingString:name]; // even simpler
}

I know that in most cases, performance here shouldn’t matter. But, I’d still like to get in the habit of doing it the better way.

If they do exactly the same thing, then I prefer the latter option because it’s shorter to type and more readable.

In Xcode 4.2, is there a way to see what ARC compiles to, i.e., where it puts retain, release, autorelease, etc? This feature would be very useful while switching over to ARC. I know you shouldn’t have to think about this stuff, but it’d help me figure out the answer to questions like these.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T11:07:19+00:00Added an answer on May 24, 2026 at 11:07 am

    The difference is subtle, but you should opt for the autorelease versions. Firstly, your code is much more readable. Secondly, on inspection of the optimized assembly output, the autorelease version is slightly more optimal.

    The autorelease version,

    - (NSString *)hello:(NSString *)name {
        return [NSString stringWithFormat:@"Hello, %@", name];
    }
    

    translates to

    "-[SGCAppDelegate hello:]":
        push    {r7, lr}
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_-(LPC0_0+4))
        mov r3, r2
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_-(LPC0_0+4))
        movw    r0, :lower16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC0_1+4))
        movt    r0, :upper16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC0_1+4))
        add r1, pc
        add r0, pc
        mov r7, sp
        ldr r1, [r1]
        ldr r0, [r0]
        movw    r2, :lower16:(L__unnamed_cfstring_-(LPC0_2+4))
        movt    r2, :upper16:(L__unnamed_cfstring_-(LPC0_2+4))
        add r2, pc
        blx _objc_msgSend    ; stringWithFormat:
        pop {r7, pc}
    

    Whereas the [[alloc] init] version looks like the following:

    "-[SGCAppDelegate hello:]":
        push    {r4, r5, r6, r7, lr}
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_2-(LPC1_0+4))
        add r7, sp, #12
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_2-(LPC1_0+4))
        movw    r0, :lower16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC1_1+4))
        movt    r0, :upper16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC1_1+4))
        add r1, pc
        add r0, pc
        ldr r5, [r1]
        ldr r6, [r0]
        mov r0, r2
        blx _objc_retain    ; ARC retains the name string temporarily
        mov r1, r5
        mov r4, r0
        mov r0, r6
        blx _objc_msgSend   ; call to alloc
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_4-(LPC1_2+4))
        mov r3, r4
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_4-(LPC1_2+4))
        add r1, pc
        ldr r1, [r1]
        movw    r2, :lower16:(L__unnamed_cfstring_-(LPC1_3+4))
        movt    r2, :upper16:(L__unnamed_cfstring_-(LPC1_3+4))
        add r2, pc
        blx _objc_msgSend   ; call to initWithFormat:
        mov r5, r0
        mov r0, r4
        blx _objc_release   ; ARC releases the name string
        mov r0, r5
        pop.w   {r4, r5, r6, r7, lr}
        b.w _objc_autorelease
    

    As expected, it is a little longer, because it is calling the alloc and initWithFormat: methods. What is particularly interesting is ARC is generating sub-optimal code here, as it retains the name string (noted by call to _objc_retain) and later released after the call to initWithFormat:.

    If we add the __unsafe_unretained ownership qualifier, as in the following example, the code is rendered optimally. __unsafe_unretained indicates to the compiler to use primitive (copy pointer) assignment semantics.

    - (NSString *)hello:(__unsafe_unretained NSString *)name {
        return [[NSString alloc] initWithFormat:@"Hello, %@", name];
    }
    

    as follows:

    "-[SGCAppDelegate hello:]":
        push    {r4, r7, lr}
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_2-(LPC1_0+4))
        add r7, sp, #4
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_2-(LPC1_0+4))
        movw    r0, :lower16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC1_1+4))
        movt    r0, :upper16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC1_1+4))
        add r1, pc
        add r0, pc
        mov r4, r2
        ldr r1, [r1]
        ldr r0, [r0]
        blx _objc_msgSend
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_4-(LPC1_2+4))
        mov r3, r4
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_4-(LPC1_2+4))
        add r1, pc
        ldr r1, [r1]
        movw    r2, :lower16:(L__unnamed_cfstring_-(LPC1_3+4))
        movt    r2, :upper16:(L__unnamed_cfstring_-(LPC1_3+4))
        add r2, pc
        blx _objc_msgSend
        .loc    1 31 1
        pop.w   {r4, r7, lr}
        b.w _objc_autorelease
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it better (more efficient, faster, more secure, etc) to (A) cache data that
Is there a better, more elegant (and/or possibly faster) way than boolean isNumber =
Which one is better/faster/preferred 1: mov eax, 5 push eax mov eax, [someAddress] push
Which of these two statements is faster/better practice? myList.Where(x => { bool itemOne= x.ItemOne
Try to see which cast is faster (not necessary better): new c++ case or
Between HtmlSelect and DropDownList, which one has a better performance (initialized and rendered faster)?
Or better said: When to use array as a field data type in a
I'm trying to use a faster memory allocator in C++. I can't use Hoard
What's better as default , to return a copy (1) or a reference (2)
What way is better and faster to create a critical section? With a binary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.