문제

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...

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top