GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
Tie::Array::Pack(3) User Contributed Perl Documentation Tie::Array::Pack(3)

Tie::Array::Pack - An array implemented as a packed string

  use Tie::Array::Pack
  tie @array, Tie::Array::Pack => 'd';
  $array[$_] = rand() for (1..1e6); # slow but memory-efficient

To install this module type the following as usual.

   perl Makefile.PL
   make
   make test
   make install

One of the drawbacks for using Perl's native array is that it is a memory-hog. Normally it takes 20 bytes a scalar (16 bytes for scalar + overhead). This can be a problem when you need to handle millions of numbers in-memory. This module saves memory in exchange for speed.

With this module all you have to do is

  tie @array, Tie::Array::Pack => $fmt

where $fmt is one of the following that is supported by pack().

    c   A signed char value.
    C   An unsigned char value.
    s   A signed short value.
    S   An unsigned short value.
    i   A signed integer value.
    I   An unsigned integer value.
    l   A signed long value.
    L   An unsigned long value.
    n   An unsigned short in "network" (big-endian) order.
    N   An unsigned long in "network" (big-endian) order.
    v   An unsigned short in "VAX" (little-endian) order.
    V   An unsigned long in "VAX" (little-endian) order.
    q   A signed quad (64-bit) value.
    Q   An unsigned quad value.
    j   A signed integer value (a Perl internal integer, IV).
    J   An unsigned integer value (a Perl internal unsigned integer, UV).
    f   A single-precision float in the native format.
    d   A double-precision float in the native format.
    F   A floating point value in the native native format
    D   A long double-precision float in the native format.

If the format is not supported, it simply croaks.

None.

You can optionally specify the value which spefies the empty element as follows;

  tie @array, Tie::Array::Pack => l, -1; # -1 represents an empty value;

The default value is 0. Since this stores data in the packed string, the array is never sparse. To illustrate the issue, try the following;

  tie @array, Tie::Array::Pack => d;
  $array[4] = 2;
  print join(",", @array), "\n" # 0,0,0,0,2;

Another issue is that you need to pick the right format or you will get an unexpected result.

  tie @array, Tie::Array::Pack => C;
  @array = (251..260);
  print join(",", @array), "\n" # 251,252,253,254,255,0,1,2,3,4

In this case, the warning is issued.

Since this module has to pack() for each STORE and unpack() for each FETCH, it is much slower than the native array. Still it is as fast as in-memory DB_File (with $DB_RECNO) and much, much faster than Tie::File. Below is the result on my MacBook Pro.
n = 1000
           Rate    T::F DB_File T::A::P  native
 T::F    3.80/s      --    -98%    -98%   -100%
 DB_File  158/s   4049%      --     -4%    -92%
 T::A::P  164/s   4209%      4%      --    -92%
 native  2058/s  54016%   1204%   1156%      --
    
n = 10000
           Rate    T::F DB_File T::A::P  native
 T::F    1.06/s      --    -93%    -93%    -99%
 DB_File 15.8/s   1393%      --     -1%    -92%
 T::A::P 15.9/s   1409%      1%      --    -92%
 native   194/s  18248%   1129%   1116%      --
    

In Perl Core:
perltie, perlpacktut, Tie::Array, Tie::File, DB_File
On CPAN
Tie::Array::Packed and Tie::Array::PackedC - Almost identical except for the interfaces. This module is simpler and pure-perl only.

Dan Kogai, <dankogai@dan.co.jp<gt>

Copyright (C) 2006 by Dan Kogai

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.

2006-12-22 perl v5.32.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.