Re: The cost of using objects rather than plain C variables
Re: The cost of using objects rather than plain C variables
- Subject: Re: The cost of using objects rather than plain C variables
- From: Quincey Morris <email@hidden>
- Date: Sat, 06 Jul 2013 23:52:11 -0700
On Jul 6, 2013, at 23:06 , Vincent Habchi <email@hidden> wrote:
> instead of [myMutableArray add:[[NSString stringFromCString:… encoding:…] componentsSeparatedBy:@", "]], I just wrote: sscanf (myLine, "%f, %f, %f", &t [0], &t [1], &t [2])
> How come I get such a large discrepancy in memory usage between the two solutions? Is the overhead of Cocoa object so huge?
It's not just the overhead of objects. According to the code you posted, you're storing floats instead of strings.
Ken already elaborated on this conceptual answer, but I think the back-of-the-envelope calculation is enlightening:
Each NSString has at least 4 bytes of overhead (the 'isa' pointer); each character is UTF-16; each object is a multiple of 16 bytes. Your values may not fit in the remaining 12 bytes of the smallest object (an input format something like '0.xe-nn', which isn't an unlikely format, wouldn't fit in 12 bytes, even with only 1 significant digit in the mantissa).
In addition, there's at least a pointer's overhead per value in the array itself. That means you could be using 36 bytes per value in the objectified representation, versus 4 bytes in the C representation, or a factor of about 9. That's not so far off the factor of 10 you reported.
If the above is a correct analysis, then using a NSNumber representation instead of a NSString would reduce the memory use to around 100MB. Using a custom object should, as Ken suggests, reduce this by another factor of somewhere between 2 and 3.
Actually, that's not so bad. 33-50MB instead of 20MB, for the objectified vs scalar representation, isn't unbearable, I suspect. However, the C array of scalars is probably the best choice.
_______________________________________________
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