I’m installing a piece on Thursday that runs an openFrameworks sketch on a Raspberry Pi. Unfortunately the CPU usage grows significantly over time (sometimes upwards of 94%) and the framerate plummets. Its possible that my code could be optimized, but it is also possible that my 512MB RAM Raspberry Pi just can’t handle it. I am currently overclocking to 900MHZ and have allowed my GPU to access 128MB of RAM. Does anyone have any suggestions, or does oLab/ATS have a possible 2 week loner CPU (its gotta be pretty small) with more power?

Finally, if the const ref thing isn’t enough to get this working on an RPI, there are definitely some memory management things that we could do, namely work out a spatial hash structure for words, rather than iterating through all of the words in an increasingly huge vector every frame.

@bakercp I see that - in addition to returning a const ref - you also turned his getWords() method into a const method (guaranteeing that getWords() won’t change the Animation object). Was this just a style choice you were suggesting, or are const methods compiled in a different way that allows them to execute faster?

Ultimately neither const was necessary for this speedup. It was returning by reference that was key. But beware – don’t return references to temporary variables …

Dropping in a const Value& getStuff() return value is always a safe replacement for Value getStuff().

On the other hand, const Value& getStuff() is not a harmless replacement for Value& getStuff() if the caller was relying the the writability of the returned value.

Making a method itself const (placing the const after the method signature) is always a safe replacement.

So ultimately, to answer your actual question … this is primarily a style choice, a user contract, and an extra layer of clarity of API intent. As far as I know, no speed is gained from making a method const.

Great, thanks - that’s what I thought. I knew the ref was eliminating the copying, but I just wanted to make sure some compiler magic wasn’t applied when for const. (And that stackover had this article that tackles that.)