2017 © Pedro Peláez
 

library memoize

Memoize functions with a trait or by extending an object.

image

drupol/memoize

Memoize functions with a trait or by extending an object.

  • Sunday, January 28, 2018
  • by drupol
  • Repository
  • 2 Watchers
  • 2 Stars
  • 147 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 9 Versions
  • 0 % Grown

The README.md

Latest Stable Version ![GitHub stars][github stars] Total Downloads ![GitHub Workflow Status][github workflow status] ![Scrutinizer code quality][code quality] Type Coverage ![Code Coverage][code coverage] ![License][license] ![Donate!][donate paypal], (*1)

PHP Memoize

Description

Memoizer class for callable., (*2)

From wikipedia:, (*3)

In computing, memoization is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again., (*4)

This library help you to memoize callable or closures., (*5)

Features

  • Provides a Memoizer class.
  • Immutable.
  • Stateless.

Installation

With composer:, (*6)

composer require loophp/memoize, (*7)

Usage

<?php

declare(strict_types=1);

namespace App;

include 'vendor/autoload.php';

use Closure;
use Generator;
use loophp\memoize\Memoizer;

$fibonacci = static function (int $number) use (&$fibonacci): int {
    return (1 >= $number) ?
        $number :
        $fibonacci($number - 1) + $fibonacci($number - 2);
};

$fibonacciMemoized = static function (int $number) use (&$fibonacciMemoized): int {
    return (1 >= $number) ?
        $number :
        $fibonacciMemoized($number - 1) + $fibonacciMemoized($number - 2);
};
$fibonacciMemoized = Memoizer::fromClosure($fibonacciMemoized);

function bench(Closure $closure, ...$arguments): array
{
    $eval = static function (Closure $closure, ...$arguments): Generator {
        yield microtime(true);
        yield $closure(...$arguments);
        yield microtime(true);
    };

    $result = iterator_to_array($eval($closure, ...$arguments));

    return [
        $result[1],
        $result[2] - $result[0],
    ];
}

var_dump(sprintf('[return: %s] [duration: %s]', ...bench($fibonacci, 30))); // ~3 seconds
var_dump(sprintf('[return: %s] [duration: %s]', ...bench($fibonacciMemoized, 30))); // ~0.0003 seconds

Code style, code quality, tests and benchmarks

The code style is following PSR-12 plus a set of custom rules, the package drupol/php-conventions is responsible for this., (*8)

Every time changes are introduced into the library, Github CI run the tests and the benchmarks., (*9)

The library has tests written with PHPSpec. Feel free to check them out in the spec directory. Run composer phpspec to trigger the tests., (*10)

PHPInfection is used to ensure that your code is properly tested, run composer infection to test your code., (*11)

Contributing

See the file CONTRIBUTING.md but feel free to contribute to this library by sending Github pull requests., (*12)

The Versions