Izzo
Izzo

Reputation: 4928

Would using the std::vector object for DSP applications be ineffiecient?

I'm currently trying to implement some DSP algorithms using C++ and am curious as to whether or not I'm being efficient.

I'm specifically trying to design a 'DigitalFilter' class which will produce a filtered output when given a series of inputs. Now the issue that I'm facing is that the size of the filter (i.e. the number of filter coefficients) can vary. Thus the size of the a DigitalFilter class instance will vary. So for example, one instance of DigitalFilter may only need to hold 4 filter coefficients, while another may need to hold 90 filter coefficients.

The obvious and easy way to hold these coefficients would be to hold them using the std::vector object. This object essentially can vary in size, which seems like it would be appropriate for my application.

However, I also know that this implemented using heap allocated memory (as opposed to stack memory). Thus, once I set up my filter and start using it to do mathematically intensive calculations, it will constantly be referencing heap data. I realize the expensiveness typically associated with vectors is the need to completely reallocate memory location in case the vector becomes too big to fit in its original place in memory, however, this shouldn't be a big concern in my application because none of the vectors will be sized before filtering operations begin. However, I'm still curious about the efficiency.

So my question(s): What kind of time hit would be involved with referencing heap data vs stack data? Is it possible that the processor's cache memory hold onto this heap data for faster access?

Upvotes: 1

Views: 263

Answers (2)

Marcus Müller
Marcus Müller

Reputation: 36402

Your point is moot. I assure you, handling data for DSP within std::vector is not only perfectly possible, it's also commonly done – for example, GNU Radio and its highly optimized DSP primitive library, libVOLK, use vectors extensively.

There's a lot of very strange literature that suggests that heap and stack memory behave differently – that's absolutely not the case on any platform that I've worked with (those are limited to x86, x86_64, ARMv7, and H8300 so far) and you can safely disregard these.

Memory is memory, and the memory controller/cache controller of your CPU will keep locally what was used last/most frequently. As long as your memory model is sequential (Bjarne Stroustrup has held a nice presentation with the topic "I don't know your data structure, but I'm sure my vector will kick its ass"), your CPU cache will hold it locally if you access it.

Upvotes: 3

tddguru
tddguru

Reputation: 392

The access time of heap memory versus stack memory is the same on any standard PC hardware.

Since you are not resizing the vector in your filtering algorithm, you can specify the size of the vector when you create it:

std::vector<int> coef(90);

You could also use an array directly.

int * coef = new int[90];

Upvotes: 3

Related Questions