Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

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

creeble:

I can think of a few possibilities, but I don't know if any would be good because i don't know how you intend to access the values, how fast you need to look them up, etc.

For example, you could concatenate all your strings into one long one using the scheme of having a byte containing the length of the field and the text. (You did mention that each object totals about 150 bytes of data, if I understand correctl.) Then your objects would store only the offset of the first field. Each access function would simply need to skip the appropriate number of fields.

$ cat t.pl #!/usr/bin/perl use strict; use warnings; my $joe = kablooie::new('Joe', 'Blow'); my $jane = kablooie::new('Jane', 'Smith'); my $name = $joe->fname(); print "joe's first name: $name\n"; $name = $jane->lname(); print "jane's last name: $name\n"; package kablooie; my $kablooie_storage; BEGIN { $kablooie_storage = "Bob's your uncle"; } sub new { my ($fname,$lname) = @_; my $offset = length($kablooie_storage); $kablooie_storage .= chr(length($fname)) . $fname; $kablooie_storage .= chr(length($lname)) . $lname; return bless \$offset, 'kablooie'; } sub fname { # FName is the first field my $self = shift; my $offset = $$self; my $len = ord(substr($kablooie_storage,$offset,1)); return substr($kablooie_storage,$offset+1,$len); } sub lname { # LName is the second field my $self = shift; my $offset = $$self; my $len = ord(substr($kablooie_storage,$offset,1)); $offset+=$len+1; $len = ord(substr($kablooie_storage,$offset,1)); return substr($kablooie_storage,$offset+1,$len); }

When run, I get:

$ perl t.pl joe's first name: Joe jane's last name: Smith

You could add some simple compression scheme, too. (RLE might be useful if you have lots of repeated characters, and if you don't mind restricting your character set, you could compress the characters into 6 bits each.)

That said, I have no idea how much space you could save using this technique, and whether it would be worth it or not.

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re: Compact data classes by roboticus
in thread Compact data classes by creeble

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 rifling through the Monastery: (2)
As of 2024-04-26 01:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found