| 1 |  |  | <?php | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Returns the list of scripts to compare. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | function scripts() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |         __DIR__ . '/scripts/imperative.php', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |         __DIR__ . '/scripts/functional.php', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |         __DIR__ . '/scripts/object-oriented.php', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * Returs the list of tests to run. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | function tests() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     static $tests = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     if (null == $tests) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $tests = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |             (object) [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |                 'input' => __DIR__ . '/tests/1K-words.input.txt', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |                 'output' => trim(file_get_contents(__DIR__ . '/tests/1K-words.output.txt')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             (object) [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |                 'input' => __DIR__ . '/tests/10k-words.input.txt', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |                 'output' => trim(file_get_contents(__DIR__ . '/tests/10k-words.output.txt')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             (object) [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |                 'input' => __DIR__ . '/tests/100k-words.input.txt', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |                 'output' => trim(file_get_contents(__DIR__ . '/tests/100k-words.output.txt')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     return $tests; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  * Runs the scripts with the tests and shows the results. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  * @return void | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 46 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  | function main() { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     foreach (scripts() as $path) { | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         echo nameOf($path), ":\n"; | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         foreach (tests() as $test) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |             echo "  ", nameOf($test->input), ": "; | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             if (!passes($path, $test)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |                 echo "Failed!\n"; | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |             } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |                 echo averageTime(3, $path, $test), "\n"; | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  * Gets the name of a file without extension from its path. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  * @param  string $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | function nameOf($path) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     $name = substr($path, strrpos($path, '/') + 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     return substr($name, 0, strpos($name, '.')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  * Runs the given script `$n` times and returns the average running time. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  * @param  int $n | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  * @param  string $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  * @param  object $test | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | function averageTime($n, $path, $test) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     $s = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     for ($i=0; $i < $n; $i++) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $s =+ execute($path, $test->input); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     return $s / $n; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  * Checks if running a PHP script with some input returns the expected output. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  * @param  string $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  * @param  array $test | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | function passes($path, $test) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     return $test->output == trim(shell_exec("php {$path} < \"{$test->input}\"")); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  * Runs a PHP file with specific standard input | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  * and returns its running time in miliseconds. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  * @param  string $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | function execute($path, $input) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     $start = microtime(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     shell_exec("php {$path} < \"{$input}\""); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     return 1000 * (microtime(true) - $start); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 112 |  |  | main(); | 
            
                                                        
            
                                    
            
            
                | 113 |  |  |  | 
            
                        
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.