> 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.


In reply to Re: Memory efficient design by eyepopslikeamosquito
in thread Memory efficient design by harangzsolt33

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.