If you have a problem or need to report a bug please email : support@dsprobotics.com
There are 3 sections to this support area:
DOWNLOADS: access to product manuals, support files and drivers
HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects
USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here
NEW REGISTRATIONS - please contact us if you wish to register on the forum
Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright
Ruby: Circular Buffer class
1 post
• Page 1 of 1
Ruby: Circular Buffer class
Hi All,
Here's a little class that knocked up for making circular buffers - i.e. An Array where you push items to the end, and they are removed from the top when the buffer gets too big (FIFO).
A glance at the Ruby API docs will turn up what looks like a very simple way to do this...
But there's a problem with this...
At the start, each time you push something, the array grows. Once the max size is reached, it both grows and shrinks each time an item is added. Each time the array changes size, Ruby has to go crawling to the operating system to sort out the memory allocation - and this has a pretty heavy CPU cost.
The CircularBuffer class does away with that; keeping a fixed amount of memory in use at all times - some benchmark tests showed a two to ten times speed increase when adding items (it gets more efficient the bigger the buffer size, especially once it starts 'circulating').
However, reading the data is slower, as it has to be 'straightened out' from its 'circular' format. So this class is ideal for applications like data logging or MIDI analysers where many quick-fire input events only need reading every once in a while.
More documentation and examples in the schematic.
Happy new year!
Here's a little class that knocked up for making circular buffers - i.e. An Array where you push items to the end, and they are removed from the top when the buffer gets too big (FIFO).
A glance at the Ruby API docs will turn up what looks like a very simple way to do this...
- Code: Select all
@myArray = Array(new)
@max_size = 100
# Put an item on the end of the array
@myArray << item
# Shorten the Array if it got too big
@myArray.shift if @myArray.size > @max_size
But there's a problem with this...
At the start, each time you push something, the array grows. Once the max size is reached, it both grows and shrinks each time an item is added. Each time the array changes size, Ruby has to go crawling to the operating system to sort out the memory allocation - and this has a pretty heavy CPU cost.
The CircularBuffer class does away with that; keeping a fixed amount of memory in use at all times - some benchmark tests showed a two to ten times speed increase when adding items (it gets more efficient the bigger the buffer size, especially once it starts 'circulating').
However, reading the data is slower, as it has to be 'straightened out' from its 'circular' format. So this class is ideal for applications like data logging or MIDI analysers where many quick-fire input events only need reading every once in a while.
More documentation and examples in the schematic.
Happy new year!
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
Don't stagnate, mutate to create!
-
trogluddite - Posts: 1730
- Joined: Fri Oct 22, 2010 12:46 am
- Location: Yorkshire, UK
1 post
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 53 guests