in reply to Memory efficient design
> I want to figure out the best way to design my code so it won't waste memory ...
> I'm trying to understand what's going on in the background so I can design my code to be efficient
While those are fun and instructive things to do, just a friendly word of caution. Your primary focus should be writing simple, clear and correct code (here's my top ten list). If your simple, clear, easy to maintain code is fast enough, job done. If not, resist the urge to change it until you've benchmarked it!
Don’t Optimize Code -- Benchmark It
-- from Ten Essential Development Practices by Damian Conway
While guessing or speculating might be fun, there is no substitute for measuring. Even experts often get a surprise when they measure.
Making the problem trickier still, what is "fast" changes over time as hardware evolves. One recent example I remember illustrates how crucial cache misses are on modern CPUs. Though certainly no expert, I still remember falling off my chair as it dawned on me (while measuring with Intel VTune) that almost every memory access into my elegant 4GB lookup table missed the L1 cache, missed the L2 cache, missed the L3 cache, then waited for the cache line to be loaded into all three caches from main memory, while often incurring a TLB cache miss, just to rub salt into the wounds. :)
Some simple examples of using Perl's core Benchmark module can be found at point number 10 in Conway's article. From CPAN, Devel::NYTProf is also highly recommended.
|
---|