davehorsfall,

While AM mentioned using a DB, a very simple way to save a hash/array to a file, is to use a 'foreach' or 'for' loop on the hash or array and print the contents to a file. When you need the hash/array again just use the same technique in reverse.

You don't mention the type of data, but if it's ASCII text, you can use the tab character (\t) between the key and value with the normal carriage return (\n) to end the value field.

But since it's speed you require, I would use the unbuffered 'sysopen/sysread/syswrite'.

To generate the file, use the same loop technique to build a large string of the complete hash/array and then 'syswrite' the string to disk as one operation. To get the hash/array use 'sysread' to get the string back into memory. Then use 'split' to get back your hash/array. Depending on hardware you can get 5 times or more improvement in speed. If using Linux, use the '/dev/shm' Filesystem for saving the string with improved performance ( if your memory is large enough?).

Obviously, if the string is too large for memory, then you will need to use a DB and 'BerkeleyDB' is a good choice.

Regards...Ed

"Well done is better than well said." - Benjamin Franklin


In reply to Re: Saving a hash/array to a file by flexvault
in thread Saving a hash/array to a file by davehorsfall

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.