-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
61 lines (45 loc) · 1.33 KB
/
index.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
<?php
/* Simple include for this script, no need for PSR-4 autoloader */
include './src/InitData.php';
include './src/FindArea.php';
/* Start benchmark time */
$t1 = microtime(true);
/**
* Get CLI arguments
* 0: Script name
* 1: Data type (array, generate, file)
* [!] Please note, the file type won't need further arguments.
* 2: Number of array items (rows)
* 3: Number of characters in string (cols)
*/
$type = (!empty($argv[1]) ? (string) $argv[1] : 'generate');
$rows = (!empty($argv[2]) ? (int) $argv[2] : 100);
$cols = (!empty($argv[3]) ? (int) $argv[3] : 100);
$data = new MaxSquare\InitData($rows, $cols);
/**
* Get input data in different ways:
* a) Allocate full array into memory
* b) Generate array values
* c) Read stream / file
*/
if ($type === 'array') {
$input = $data->store();
} elseif ($type === 'file') {
$readFile = function ($file) {
$handle = fopen($file, 'rb');
if (!$handle) {
throw new \Exception("Cannot read file.");
}
while (feof($handle) !== true) {
yield fgets($handle);
}
fclose($handle);
};
$input = $readFile('data.txt');
} else {
$input = $data->generate();
}
$area = new MaxSquare\FindArea($input);
echo "MAX square area: {$area->get()}".PHP_EOL;
$t2 = microtime(true);
echo $area->stats([$t1, $t2]);