README
1NAME
2 Tie::Array::Pack - An array implemented as a packed string
3
4SYNOPSIS
5 use Tie::Array::Pack
6 tie @array, Tie::Array::Pack => 'd';
7 $array[$_] = rand() for (1..1e6); # slow but memory-efficient
8
9INSTALLATION
10 To install this module type the following as usual.
11
12 perl Makefile.PL
13 make
14 make test
15 make install
16
17DESCRIPTION
18 One of the drawbacks for using Perl's native array is that it is a
19 memory-hog. Normally it takes 20 bytes a scalar (16 bytes for scalar +
20 overhead). This can be a problem when you need to handle millions of
21 numbers in-memory. This module saves memory in exchange for speed.
22
23 With this module all you have to do is
24
25 tie @array, Tie::Array::Pack => $fmt
26
27 where $fmt is one of the following that is supported by pack().
28
29 c A signed char value.
30 C An unsigned char value.
31 s A signed short value.
32 S An unsigned short value.
33 i A signed integer value.
34 I An unsigned integer value.
35 l A signed long value.
36 L An unsigned long value.
37 n An unsigned short in "network" (big-endian) order.
38 N An unsigned long in "network" (big-endian) order.
39 v An unsigned short in "VAX" (little-endian) order.
40 V An unsigned long in "VAX" (little-endian) order.
41 q A signed quad (64-bit) value.
42 Q An unsigned quad value.
43 j A signed integer value (a Perl internal integer, IV).
44 J An unsigned integer value (a Perl internal unsigned integer, UV).
45 f A single-precision float in the native format.
46 d A double-precision float in the native format.
47 F A floating point value in the native native format
48 D A long double-precision float in the native format.
49
50 If the format is not supported, it simply croaks.
51
52 EXPORT
53 None.
54
55 EMPTY ELEMENT
56 You can optionally specify the value which spefies the empty element as
57 follows;
58
59 tie @array, Tie::Array::Pack => l, -1; # -1 represents an empty value;
60
61 The default value is 0. Since this stores data in the packed string, the
62 array is never sparse. To illustrate the issue, try the following;
63
64 tie @array, Tie::Array::Pack => d;
65 $array[4] = 2;
66 print join(",", @array), "\n" # 0,0,0,0,2;
67
68 PICK THE RIGHT FORMAT
69 Another issue is that you need to pick the right format or you will get
70 an unexpected result.
71
72 tie @array, Tie::Array::Pack => C;
73 @array = (251..260);
74 print join(",", @array), "\n" # 251,252,253,254,255,0,1,2,3,4
75
76 In this case, the warning is issued.
77
78 HOW SLOW IS IT?
79 Since this module has to pack() for each STORE and unpack() for each
80 FETCH, it is much slower than the native array. Still it is as fast as
81 in-memory DB_File (with $DB_RECNO) and much, much faster than Tie::File.
82 Below is the result on my MacBook Pro.
83
84 n = 1000
85 Rate T::F DB_File T::A::P native
86 T::F 3.80/s -- -98% -98% -100%
87 DB_File 158/s 4049% -- -4% -92%
88 T::A::P 164/s 4209% 4% -- -92%
89 native 2058/s 54016% 1204% 1156% --
90
91 n = 10000
92 Rate T::F DB_File T::A::P native
93 T::F 1.06/s -- -93% -93% -99%
94 DB_File 15.8/s 1393% -- -1% -92%
95 T::A::P 15.9/s 1409% 1% -- -92%
96 native 194/s 18248% 1129% 1116% --
97
98SEE ALSO
99 In Perl Core:
100 perltie, perlpacktut, Tie::Array, Tie::File, DB_File
101
102 On CPAN
103 Tie::Array::Packed and Tie::Array::PackedC - Almost identical except
104 for the interfaces. This module is simpler and pure-perl only.
105
106AUTHOR
107 Dan Kogai, <dankogai@dan.co.jp<gt>
108
109COPYRIGHT AND LICENSE
110 Copyright (C) 2006 by Dan Kogai
111
112 This library is free software; you can redistribute it and/or modify it
113 under the same terms as Perl itself, either Perl version 5.8.8 or, at
114 your option, any later version of Perl 5 you may have available.
115
116