what is the point oarrayWithCapacity?

Discussion in 'iOS Programming' started by ljg93, May 4, 2012.

  1. ljg93, May 4, 2012
    Last edited: May 4, 2012

    ljg93 macrumors member

    Joined:
    Mar 13, 2011
    #1
    Hi

    I am wondering what the point of arraywithCapacity does when creating a new NSMutableArray

    For example below
    Code:
    NSMutableArray *files;
    fiels = [NSMutableArray arrayWithCapacity:42];
    
    I know it is supposed to just tell the complier to expect a range of 42 on the array, but then again if it goes over 42 it does not matter. So what is the point of even having it?
     
  2. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #2
    One point is to speed up the time it takes to reserve memory for the objects. Memory allocation is considered expensive in time and other resources. If the request allocates chunks in multiples a single object size, the authors of that method can create efficiencies to save time and CPU cycles too.

    The savings probably isn't a big deal for a few instantiations. Now think in terms of thousands, or many hundreds. Several seconds may be saved and the main thread may not seem to block while creating so may.
     

Share This Page