Re: NSNumber increment
Re: NSNumber increment
- Subject: Re: NSNumber increment
- From: Alastair Houghton <email@hidden>
- Date: Mon, 9 Jul 2007 11:34:16 +0100
On 8 Jul 2007, at 17:12, Ofri Wolfus wrote:
If speed is the issue, I don't think it's NSArray's fault.
Indeed not. NSArrays of objects should be quite fast, though they do
still incur a method call to access them, which is expensive by
comparison to pointer addition (which is what a C array uses).
It's probably going to be the deallocation and allocation of the
NSNumber instances that slows things down.
Agreed.
If the array needs to be resized, I, personally, would create my
own mutable number class to work around this (or use NSValue with a
pointer to a malloc'ed int),
Unless there are only a very small number of them, I wouldn't be
inclined to go with the NSValue with a pointer to an integer, simply
because you're then allocating two chunks of memory for each object,
which is expensive (since you go through the memory allocator twice)
and wasteful (both of them are small, so the allocator imposes a
large overhead by proportion). For small numbers of numbers, NSValue
isn't a bad solution, though a custom mutable number is probably better.
but if it's fixed size i'd just go with a C array like Alastair
suggested.
Even if it needs to resize, the decision really depends on how common
resizing is and how you choose to do it (e.g. it's very common to
grow or shrink arrays in chunks, by keeping track of both the current
capacity and the number of element). It's actually quite difficult to
suggest something specific as we don't really know what this code is
being used for, and getting too specific means you're in danger of
premature optimization :-).
It's even possible that you don't even need the array in the first
place (e.g. sometimes people make separate arrays of things when they
could use a member variable of an object that's already in an array
somewhere). So that's something to think about also.
I'm sure C++ fans would recommend using STL containers (probably an
std::vector, or maybe something from e.g. Boost or Blitz++) as yet
another possible solution. These can be quite useful sometimes,
depending on your application. The downside is that if you use C++,
you have to compile your code as ObjC++ rather than ObjC, and you
need to watch out for typical C versus C++ gotchas.
Anyway, I'm sure that's more than enough for anyone reading this
thread to work out what they need to do in their specific case :-)
Kind regards,
Alastair.
--
http://alastairs-place.net
_______________________________________________
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