Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Benchmarks of: http://stackoverflow.com/q/18359729/1928023
- /**
- * Answer 1 using regexes
- *
- * Changed: Do nod read input from disk for performance measuring
- *
- * @autor hek2mgl
- * @link http://stackoverflow.com/a/18406873/1928023
- */
- function answer_1($input) {
- $record = array();
- //foreach(file('input.txt') as $line) {
- foreach($input AS $line) {
- if(preg_match('~^(HD|BY|PD|LP|TD) ?(.*)?$~', $line, $matches)) {
- $currentKey = $matches[1];
- $record[$currentKey] = $matches[2];
- } else {
- $record[$currentKey] .= str_replace("\n", ' ', $line);
- }
- }
- return $record;
- }
- /**
- * Answer 2 not using regexes
- *
- * Changed: Do nod read input from disk for performance measuring
- *
- * @autor Emo Mosley
- * @link http://stackoverflow.com/a/18406908/1928023
- */
- function answer_2($lines) {
- $fields = array('HD', 'BY', 'PD', 'LP', 'TD');
- # load file as array of lines
- //$lines = file($filename);
- if($lines === false)
- return(null);
- $sections = array();
- $code = "";
- $str = "";
- # examine each line to build section array
- for($i=0; $i<sizeof($lines); $i++) {
- $line = trim($lines[$i]);
- # check for special field codes
- $left = substr($line, 0, 2);
- if(in_array($left, $fields)) {
- # field code detected; first, finish previous section, if exists
- if($code) {
- # store the previous section
- $sections[$code] = trim($str);
- }
- # begin to process new section
- $code = $left;
- $str = trim(substr($line, 2));
- } else if($code && $line) {
- # keep a running string of section content
- $str .= " ".$line;
- }
- } # for i
- # check for no data
- if(!$code)
- return(null);
- # store the last section and return results
- $sections[$code] = trim($str);
- return($sections);
- }
- /**
- * Answer 3 using regexes
- *
- * @autor anubhava
- * @link http://stackoverflow.com/a/18407529/1928023
- */
- function answer_3($s) {
- $match = array(); // will hold final output
- if (preg_match_all('~(^|[A-Z]{2})\s(.*?)(?=[A-Z]{2}\s|$)~s', $s, $arr)) {
- for ( $i = 0; $i < count($arr[1]); $i++ )
- $match[ trim($arr[1][$i]) ] = str_replace( "\n", "", $arr[2][$i] );
- }
- return $match;
- }
- /**
- * Answer 4 not using regexes
- *
- * @autor jgb
- * @link http://stackoverflow.com/a/18410733/1928023
- */
- function answer_4(array $parts, $str, $eol=PHP_EOL) {
- $ret=array_fill_keys($parts, '');
- $current=null;
- foreach(explode($eol, $str) AS $line) {
- $substr = substr($line, 0, 2);
- if (isset($ret[$substr])) {
- $current = $substr;
- $line = trim(substr($line, 2));
- }
- if ($current) $ret[$current] .= $line;
- }
- return $ret;
- }
- $str="HD Alcoa Earnings Soar; Outlook Stays Upbeat
- BY By James R. Hagerty and Matthew Day
- PD 12 July 2011
- LP
- Alcoa Inc.'s profit more than doubled in the second quarter.
- The giant aluminum producer managed to meet analysts' forecasts.
- However, profits wereless than expected
- TD
- Licence this article via our website:
- http://example.com";
- $array = explode(PHP_EOL, $str);
- $iterations=100000;
- /*
- var_dump(answer_1($array));
- var_dump(answer_2($array));
- var_dump(answer_3($str));
- var_dump(answer_4(array('HD', 'BY', 'PD', 'LP', 'TD'), $str));
- exit;
- */
- // Start benchmarking
- echo "Answer 1 by: hek2mgl ";
- $benchmark_start = microtime(true);
- for($i=0;$i<$iterations;$i++) answer_1($array);
- echo round(microtime(true) - $benchmark_start, 3) . " seconds (regexp)\n";
- echo "Answer 2 by: Emo Mosley ";
- $benchmark_start = microtime(true);
- for($i=0;$i<$iterations;$i++) answer_2($array);
- echo round(microtime(true) - $benchmark_start, 3) . " seconds\n";
- echo "Answer 3 by: anubhava ";
- $benchmark_start = microtime(true);
- for($i=0;$i<$iterations;$i++) answer_3($str);
- echo round(microtime(true) - $benchmark_start, 3) . " seconds (regexp)\n";
- echo "Answer 4 by: jgb ";
- $benchmark_start = microtime(true);
- for($i=0;$i<$iterations;$i++) answer_4(array('HD', 'BY', 'PD', 'LP', 'TD'), $str);
- echo round(microtime(true) - $benchmark_start, 3) . " seconds\n";
Advertisement
Add Comment
Please, Sign In to add comment