performance of encoding an array of structs
performance of encoding an array of structs
- Subject: performance of encoding an array of structs
- From: Hank Heijink <email@hidden>
- Date: Tue, 12 Jun 2007 13:11:52 -0400
Hi everyone,
I'm stumped on encoding an array of structs - it works, but takes
forever. Here's what's going on:
I have a structure that is basically an NSPoint with a time stamp:
typedef struct _MVTimedPoint {
float x;
float y;
double time; // this actually stores an NSTimeInterval. I should
change the type.
} MVTimedPoint;
These structs are stored in a C array, wrapped in a class
MVTimedPointBuffer. The class implements autoresizing of the _buffer.
@interface MVTimedPointBuffer : NSObject <NSCoding> {
MVTimedPoint *_buffer;
int _bufferSize;
int _bufferPointer; // between 0 and _bufferSize-1
}
The encoding function is this one:
- (void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeObject:[self contents] forKey:@"contents"];
[coder encodeInt:_bufferSize forKey:@"bufferSize"];
}
- (NSArray *)contents
{
NSMutableArray *result = [[NSMutableArray alloc]
initWithCapacity:_bufferPointer];
int i;
// Not very efficient, but couldn't think of an easier way. I tried
converting the MVTimedPoints to NSStrings
// in a C array and then converting that array with +[NSArray
arrayWithObjects:count:], but that doesn't seem
// to be a whole lot faster.
for (i = 0; i < _bufferPointer; i++)
[result addObject:MVStringFromTimedPoint(_buffer[i])];
}
The MVStringFromTimedPoint function just makes a string from the
MVTimedPoint with +[NSString stringWithFormat:].
This all works fine, except for the time it takes. If array of
MVTimedPoints is in the order of 20000 elements, it takes about 10
seconds to encode the array on a MacBook. This seems rather
excessive, especially since this array will hold as much as a million
elements.
I ran Shark on my application and it seems to spend all its time in -
[NSKeyedArchiver finishEncoding], specifically in _flattenPlist in
CoreFoundation. This doesn't mean all that much to me, and I can't
figure out which of my functions make it so slow.
So... Can I do something to speed things up? Am I completely on the
wrong track?
Any help is much appreciated!
Thanks a lot,
Hank
Hank Heijink
www.hankheijink.com
email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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