March 2002 | Perl Hardware Store | 26 |
Suppose you have a function that will take a long time to compute
Cache the results in a database
Run an overnight batch job to populate the database
In the morning, the function is fast
Perl's tie feature and the Memoize module make this easy:
use DB_File; tie %h, 'DB_File', "cmyk.db", O_RDWR|O_CREAT, 0666 or die ...;
use Memoize; memoize 'cmyk', SCALAR_CACHE => [ HASH => \%h ];
for my $r (0 .. 255) { for my $g (0 .. 255) { for my $b (0 .. 255) { cmyk($r, $g, $b); } } }
Next | Copyright © 2002 M-J. Dominus |