Skip to content
This repository was archived by the owner on Oct 9, 2024. It is now read-only.

Commit 53f58fa

Browse files
committed
fix: Update README with proper example.
1 parent f9d75a5 commit 53f58fa

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

README.md

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,23 +53,20 @@ $fibonacci = static function (int $number) use (&$fibonacci): int {
5353
$fibonacci($number - 1) + $fibonacci($number - 2);
5454
};
5555

56-
$sleep = static function (int $second): int
57-
{
58-
sleep($second);
59-
60-
return $second;
56+
$fibonacciMemoized = static function (int $number) use (&$fibonacciMemoized): int {
57+
return (1 >= $number) ?
58+
$number :
59+
$fibonacciMemoized($number - 1) + $fibonacciMemoized($number - 2);
6160
};
62-
63-
$fibonacci = Memoizer::fromClosure($sleep);
61+
$fibonacciMemoized = Memoizer::fromClosure($fibonacciMemoized);
6462

6563
function bench(Closure $closure, ...$arguments): array
6664
{
67-
$eval = static function(Closure $closure, ...$arguments): Generator
68-
{
69-
yield microtime(true);
70-
yield $closure(...$arguments);
71-
yield microtime(true);
72-
};
65+
$eval = static function (Closure $closure, ...$arguments): Generator {
66+
yield microtime(true);
67+
yield $closure(...$arguments);
68+
yield microtime(true);
69+
};
7370

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

@@ -79,8 +76,8 @@ function bench(Closure $closure, ...$arguments): array
7976
];
8077
}
8178

82-
var_dump(sprintf('[return: %s] [duration: %s]', ...bench($fibonacci, 10))); // ~10 seconds
83-
var_dump(sprintf('[return: %s] [duration: %s]', ...bench($fibonacci, 10))); // ~3.9e-5
79+
var_dump(sprintf('[return: %s] [duration: %s]', ...bench($fibonacci, 30))); // ~3 seconds
80+
var_dump(sprintf('[return: %s] [duration: %s]', ...bench($fibonacciMemoized, 30))); // ~0.0003 seconds
8481
```
8582

8683
## Code style, code quality, tests and benchmarks

0 commit comments

Comments
 (0)