Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obj-C circular buffer object, implementing one?

I've been developing for the iPhone for quite some time and I've been wondering if there's any array object that uses circular buffer in Obj-C? Like Java's Stack or List or Queue. I've been tinkering with the NSMutableArray, testing it's limits... and it seems that after 50k simple objects inside the array - the application is significantly slowed down.

So, is there any better solution other than the NSMutableArray (which becomes very slow with huge amounts of data). If not, can anyone tell me about a way to create such an object (would that involve using chain (node) objects??).

Bottom line: Populating a UITableView from an SQLite DB directly would be smart? As it won't require memory from an array or anything, but just the queries. And SQLite is fast and not memory grinding.

Thank you very much for you time and attention, ~ Natanavra.


From what I've been thinking it seems that going for Quinn's class is the best option possibly. I have another question - would it be faster or smarter to load everything straight from the SQLite DB instead of creating an object and pushing it into an array?

Thank you in advance, ~ Natanavra.

like image 300
natanavra Avatar asked Dec 25 '09 22:12

natanavra


People also ask

How would you implement a circular buffer?

Circular Buffers can be implemented in two ways, using an array or a linked list. An empty object array along with its capacity is initialized inside the constructor as the type of elements added is unknown. Two pointers namely head and tail are maintained for insertion and deletion of elements.

How does a circular buffer work?

A circular buffer is a utility used to transfer successive data values from a producer thread to a consumer thread, who retrieves the data in FIFO (first in first out) order. This kind of data structure will be used when pipelining threads, a subject discussed in detail in Chapter 15.


2 Answers

Apologies for tooting my own horn, but I implemented a C-based circular buffer in CHDataStructures. (Specifically, check out CHCircularBufferQueue and CHCircularBufferStack.) The project is open source and has benchmarks which demonstrate that a true circular buffer is quite fast when compared to NSMutableArray in the general case, but results will depend on your data and usage, as well as the fact that you're operating on a memory-constrained device (e.g. iPhone). Hope that helps!

like image 97
Quinn Taylor Avatar answered Sep 22 '22 22:09

Quinn Taylor


If you're seeing performance issues, measure where your app is spending its time, don't just guess. Apple provides an excellent set of performance measurement tools.

like image 43
NSResponder Avatar answered Sep 26 '22 22:09

NSResponder