Perl & Binary Data

Peter Scott peter at PSDT.com
Tue Dec 17 13:40:22 CST 2002


At 11:31 AM 12/17/02 -0800, I wrote:
>abez at abez.ca wrote:
> > > > >while ($len = sysread($fd,$buffer,$size)) {
> > > > >         my @o = unpack("s*",$buffer);
> > > > >         foreach (@o) {
> > > > >                 $_/=2;
> > > > >         }
> > > > >         syswrite($fd,pack("s*",$buffer,$size);
> > > > >}
> > > > >
> > > > >This seems really slow for datarates of 178kb/s
>
>This seems to work:
>
>my $data = "\x10\x02\x80\xff";  # Test data
>
>for (my $i = 0; $i < length($data); $i += 2) {
>   my $x = vec($data, $i >> 1, 16);
>   vec($data, $i >> 1, 16) = ($x >> 1) | ($x & 0b10000000);
>}

I forgot to say, please let me know if it's faster.  It avoids a 
pack/unpack cycle, which has to be worth something.  Although maybe 
vec() is no faster.  Perhaps you could benchmark, I don't have the time.


--
Peter Scott
Pacific Systems Design Technologies
http://www.perldebugged.com/




More information about the Victoria-pm mailing list