Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Just storing your number in binary rather than ascii with delimiters would cut your memory requirements to about 1/3 on average. Assuming that your example is representative and your numbers will fit in 16-bits. You mentioned the figures of 50,000 sets and assuming 4 per set as shown.

Instead of approx 1 MB, your down to 390k, ignoring the cost of the arrays which should be constant That's a start.

Projecting the timing of the code below gives around 3 seconds to pack the 50k x 4 x 16 bits and 0.002 secs to unpack 100 lines, which comfortably fits your spec, and my machine is hardly the quickest around.

#! perl -slw use strict; use Benchmark::Timer; my $t= Benchmark::Timer->new(); my (@ary1, @ary2); #! 1000 lines of 4 random numbers (0-65536) in ascii with commas. @ary1 = map{ join ',', map{ int rand(2**16) } 1..4 } 1 .. 1000; print $ary1[0]; $t->start('packing'); #! The same numbers stored as binary strings @ary2 = map{ pack 'S4', split',', $_ } @ary1; $t->stop('packing'); print length $ary2[0]; print '1000 in ascii: ', scalar @ary1, ' : ', length "@ary1"; print '1000 in binary: ', scalar @ary2, ' : ', length "@ary2"; $t->start('unpacking'); for (@ary2) { my @line_of_nums = unpack 'S4', $_; # print "@line_of_nums"; } $t->stop('unpacking'); $t->report(); __END__ C:\test>229792 52318,4206,9238,48758 8 1000 in ascii: 1000 : 22310 1000 in binary: 1000 : 8000 1 trial of packing (50ms total) 1 trial of unpacking (20ms total)

Examine what is said, not who speaks.

The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.


In reply to Re: Compressing a set of integers by BrowserUk
in thread Compressing a set of integers by toma

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2024-05-25 20:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found