-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfibonacci.php
78 lines (68 loc) · 1.46 KB
/
fibonacci.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
/* Copyright (c) 2013 Michael LeSane
*
* Three implementations of the fibonacci function to
* demonstrate the applications of the future library.
*/
include 'future.php';
function fib_serial($x)
{
return ($x<2)?$x:fib_serial($x-1)+fib_serial($x-2);
}
function fib_parallel($x)
{
if($x<2) return $x;
$a = future::start("fib_parallel",[$x-1]);
$b = future::start("fib_parallel",[$x-2]);
return future::wait($a) + future::wait($b);
}
function fib_future($x)
{
if($x<2) return future::ready($x);
$a = fib_future($x-1);
$b = fib_future($x-2);
return (future::after
(
function($l,$r)
{
return $l+$r;
},
$a,$b
));
}
echo
"------------------
Fibonacci, Serial:
------------------\n";
for($i = 0; $i < 10; $i++)
{
$time_start = microtime(true);
echo "f($i) = ".fib_serial($i)." (";
$time_end = microtime(true);
$diff = $time_end-$time_start;
echo $diff." seconds)\n";
}
echo
"------------------
Fibonacci, Parallel (No Future Operations):
------------------\n";
for($i = 0; $i < 10; $i++)
{
$time_start = microtime(true);
echo "f($i) = ".future::wait(fib_future($i))." (";
$time_end = microtime(true);
$diff = $time_end-$time_start;
echo $diff." seconds)\n";
}
echo
"------------------
Fibonacci, Parallel (Future Operations):
------------------\n";
for($i = 0; $i < 10; $i++)
{
$time_start = microtime(true);
echo "f($i) = ".future::wait(fib_future($i))." (";
$time_end = microtime(true);
$diff = $time_end-$time_start;
echo $diff." seconds)\n";
}