Internet explorer users: Please follow the permalink at the bottom of this post to see the correct video… We are working on this! Sorry!
I frequent StackOverflow.com answering MATLAB question. Here is the answer to one that recently went by.
In this three minute video I show how to implement a circular buffer for keeping a limited time history. This is useful for keeping only the history that you need for a variable. A strip chart could be plotted from one of these.
Like Petter, I wonder if this should be considdered an efficient implementation or only an easy one?
I use a few rinbuffers here and there and they are all implemented like this, because it is easy. However, the line buffer = [newValue buffer(1:end-1)] gives the optimizer in me the shivers. Is that just me?
I profiled this with a really large amount of data, more iterations and longer buffer. That part of the video is on the “cutting room floor”. Basically, there is a negligible performance hit. To be a real test, it would need to be done in context to see if this architecture is a problem.
This video is in keeping with one of the central themes of my programming style: “Avoid premature optimization”. If I were to try and avoid the churn that this circular buffer goes through, the complexity and readability would suffer.
-Doug
Leave a Reply
About
Doug Hull is a proud MathWorker who is on a mission to help you with MATLAB.
Wouldn’t that solution force the entire array to be reshuffled every time a new value is added?
Like Petter, I wonder if this should be considdered an efficient implementation or only an easy one?
I use a few rinbuffers here and there and they are all implemented like this, because it is easy. However, the line buffer = [newValue buffer(1:end-1)] gives the optimizer in me the shivers. Is that just me?
I profiled this with a really large amount of data, more iterations and longer buffer. That part of the video is on the “cutting room floor”. Basically, there is a negligible performance hit. To be a real test, it would need to be done in context to see if this architecture is a problem.
This video is in keeping with one of the central themes of my programming style: “Avoid premature optimization”. If I were to try and avoid the churn that this circular buffer goes through, the complexity and readability would suffer.
-Doug