Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Fast string construction

by citromatik (Curate)
on Apr 07, 2011 at 13:00 UTC ( #898040=perlquestion: print w/replies, xml ) Need Help??
citromatik has asked for the wisdom of the Perl Monks concerning the following question:

Hi all

I am storing some data in a long string of "ints":

open my $fh, "<", $file or die $!; while (<$fh>){ my ($key,$val) = split /\t/; substr($bin,$key*4,4,pack("N",$val)); }

Since the $keys in the file are not sorted, I need to initialize substr with enough values (to avoid a "substr outside of string" error). Currently I am doing it like follows:

substr($bin,$_*4,4,pack ("N",0)) for (0..$last_val);

Is there a more efficient way (maybe in one go instead of looping) of doing this?

Thanks in advance


Replies are listed 'Best First'.
Re: Fast string construction
by johngg (Abbot) on Apr 07, 2011 at 13:59 UTC

    I wonder if you could use vec instead. I don't think you would need to initialise the string then as it would, I think, be automatically extended at need. I think it also stores bits in the same order as pack q{N}, ...

    knoppix@Microknoppix:~$ perl -E ' > vec( $str, 1, 32 ) = 0xfffff; > say unpack q{b*}, $str; > vec( $str, 3, 32 ) = 0xf; > say unpack q{b*}, $str;' 0000000000000000000000000000000000000000111100001111111111111111 0000000000000000000000000000000000000000111100001111111111111111000000 +0000000000000000000000000000000000000000000000000011110000 knoppix@Microknoppix:~$ perl -E ' > vec( $str, 0, 32 ) = 0xfffff; > say unpack q{b*}, $str; > say unpack q{b*}, pack q{N}, 0xfffff;' 00000000111100001111111111111111 00000000111100001111111111111111 knoppix@Microknoppix:~$

    I hope this is useful.



Re: Fast string construction
by Eliya (Vicar) on Apr 07, 2011 at 13:15 UTC

    You could also use the x operator (repetition):

    $bin = "\0" x (4 * ($last_val+1));

    A benchmark would have to show whether it's more efficient, though...

Re: Fast string construction
by ikegami (Pope) on Apr 07, 2011 at 16:06 UTC
    Does it actually have to be a string, or does it just have to be efficient? You may want to consider Judy::L


      That’s what I love about this site, and about Perl.   It’s a never-ending educational experience.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://898040]
Approved by Eliya
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2016-09-25 09:03 GMT
Find Nodes?
    Voting Booth?
    Extraterrestrials haven't visited the Earth yet because:

    Results (468 votes). Check out past polls.