Question

I have void* buffer... I need to encapsulate it as fast as possible in a NSData structure (NSData or NSMutableData)

I'm guessing using only one NSMutableData (defined in the class) with

- (void)resetBytesInRange:(NSRange)range
- (void)appendBytes:(const void *)bytes length:(NSUInteger)length

or even

- (void)replaceBytesInRange:(NSRange)range withBytes:(const void *)bytes

will be much faster than using [NSData dataWithData:] every function call...

This is for audio process, so it needs to be the fastest it can be.

Does anyone can confirm that i'll be faster to use NSMutableData instead of NSData constructor ?

EDIT :

the buffer is always the same size, the NSMutableData won't need to re-allocate memory...

Was it helpful?

Solution

If you just want to replace the contents of an (already initialized) NSMutableData *data object with new data from buffer of the same length, then the fastest method is probably

void *mutableBytes = [data mutableBytes];
memcpy(mutableBytes, buffer, length);

because no range check and no (re-)allocation is done. See also the examples in Working With Mutable Binary Data.

But of course, as it is often said in this forum, you should profile your app with Instruments to find out where to optimize the program.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top