After re-reading your root post, I'm really puzzled, especially by your ReadFile subroutine listed in italics at the end.

As noted in my earlier reply, programmers need to adapt their coding style over the years as hardware evolves. In mainstream computing (as opposed to Embedded software) memory has nowadays become so abundant and cheap that it's seldom a concern. At least, that's what I see.

> I am trying to load several big files into memory by reading the entire file. (They're between 5MB and 100MB each.) I want to figure out the best way to design my code so it won't waste memory.

It's hard to buy a laptop today with less than 8 GB of memory ... so you could comfortably load forty 100 MB files into memory simultaneously on a low end laptop. If you need more, you can buy 32 GB of PC memory for around $200, a bargain compared to programmer's salaries. ;-)

To avoid eyestrain, I've reformatted your ReadFile subroutine (in italics at the end of the root node) below:

# Usage: STRING = ReadFile(FILENAME, START, LENGTH) # Reads an entire file or part of a file in binary mode. # Returns the file contents as a string. # An optional second argument will move the file pointer before readin +g, # and an optional third argument limits the number of bytes to read. sub ReadFile { my $F = defined $_[0] ? $_[0] : ''; $F =~ tr#><*%$?\r\n\"\0|##d; -e $F || return ''; -f $F || return ''; my $S = -s $F; $S || return ''; my $L = defined $_2 ? $_2 : $S; $L > 0 || return ''; local *H; sysopen(H, $F, 0) || return ''; binmode H; my $P = defined $_1 ? $_1 : 0; $P >= 0 or $P = 0; $P < $S || return ''; $P < 1 || sysseek(H, $P, 0); my $D = ''; sysread(H, $D, $L); close H; return $D; }

Though you stated this code is "irrelevant", I disagree. Certainly, if this appeared in a code review at work, serious questions would be asked, especially around error handling and interface (e.g. going through each bullet point in the "API Design Checklist" section at On Coding Standards and Code Reviews).


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.