Logo

dev-resources.site

for different kinds of informations.

Caching & Memoization with state variables

Published at
7/4/2024
Categories
perl
caching
memoization
Author
chrisarg
Categories
3 categories in total
perl
open
caching
open
memoization
open
Author
8 person written this
chrisarg
open
Caching & Memoization with state variables

(This is a cross post from B.P.O ).

Chapter 3 of [Higher Order Perl]<(https://hop.perl.plover.com/) describes various approaches to memoization of an expensive function: private cache and the Memoize module. The book was written in 2005 (Perl was at version 5.8 back then) , so it does not include another way for function caching that is now available : caching through state variables (introduced in Perl 5.10). The Fibonacci example considered in HOP also requires the ability to initialize state hash variables (available since Perl 5.28). The code below contrasts the implementation with a state variable v.s. the memoize module:

use v5.38;
use Time::HiRes qw(time);
use Memoize;

sub fib_cache_with_state ($number) {
    state %fib = ( 0 => 0, 1 => 1 );
    unless ( exists $fib{$number} ) {
        $fib{$number} = fib_cache_with_state( $number - 1 ) +
          fib_cache_with_state( $number - 2 );
    }
    return $fib{$number};
}

memoize 'fib';

sub fib ($number) {
    return $number if $number < 2;
    return fib( $number - 1 ) + fib( $number - 2 );
}

my $number = 80;

## using the memoize module
my $start_time = time;
my $fi1        = fib($number);
my $end_time   = time;
my $dt1        = $end_time - $start_time;

## using a state variable to memoize
$start_time = time;
my $fib2 = fib_cache_with_state($number);
$end_time = time;
my $dt2 = $end_time - $start_time;

printf "Fibonacci of %d with the memoize module took : %.2g\n", $number, $dt1;
printf "Fibonacci of %d with a  state variable  took : %.2g\n", $number, $dt2;

printf "Speedup state var /memoize module: %.2g\n", $dt1 / $dt2;
say "Difference in calculations : ", $fi1 - $fib2;

Enter fullscreen mode Exit fullscreen mode

State variable is faster for CPUs , but the Memoize module is faster for humans. Both of them are great tricks to know :)

caching Article's
30 articles in total
Favicon
How to Implement Caching in PHP and Which Caching Techniques Are Best for Performance?
Favicon
Understanding Memcached: A Powerful In-Memory Caching Solution by Abhay
Favicon
Caching with Redis for Backend in Apache Superset
Favicon
The Most Popular Database Caching Strategies Explained
Favicon
Melhorando o Desempenho da Sua Aplicação PHP com Lithe Cache
Favicon
Cache Strategies: A Complete Guide with Real-Life Examples πŸš€
Favicon
Caching β€” An overview
Favicon
Bloom Filters
Favicon
HybridCache in ASP.NET Core - New Caching Library
Favicon
Advanced Data Caching Techniques for High-Performance Systems
Favicon
Redis: Understanding the Basics
Favicon
Implementing Caching Strategies for Improved Performance
Favicon
Improving the Performance of Your PHP Application with Lithe Cache
Favicon
Redis caching with Mongoose
Favicon
Building Scalable Web Applications: Techniques for Handling High Traffic
Favicon
πŸš€Optimize Web Performance in the Cloud with Caching!πŸš€
Favicon
Top 5 Next.js Caching Solutions for Next.js Apps (2024)
Favicon
Building a cache in Python
Favicon
Caching in .Net 8: Improving Application Performance
Favicon
Davide's Code and Architecture Notes - Cache Expiration vs Cache Eviction (and Eviction Policies)
Favicon
Conquering the Cache Calamity: My Journey to HNG Internship
Favicon
Next.js Caching Issues With Fetching Data
Favicon
MemoryCache in C#: A Practical Guide
Favicon
Caching & Memoization with state variables
Favicon
Laravel Caching - Explained Simply
Favicon
A way to cache responses in Grape API
Favicon
The Power of Caching and How to Implement It in Your Python Applications
Favicon
Caching
Favicon
Cost-Effective Image Management: Maximizing Efficiency Through Network Image Caching in Mobile Apps
Favicon
Top Redis Use Cases

Featured ones: