Guest User

Untitled

a guest
Aug 24th, 2013
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.16 KB | None | 0 0
  1. <?php
  2. // Benchmarks of: http://stackoverflow.com/q/18359729/1928023
  3.  
  4.  
  5. /**
  6.  * Answer 1 using regexes
  7.  *
  8.  * Changed: Do nod read input from disk for performance measuring
  9.  *
  10.  * @autor hek2mgl
  11.  * @link http://stackoverflow.com/a/18406873/1928023
  12.  */
  13. function answer_1($input) {
  14.   $record = array();
  15.   //foreach(file('input.txt') as $line) {
  16.   foreach($input AS $line) {
  17.     if(preg_match('~^(HD|BY|PD|LP|TD) ?(.*)?$~', $line, $matches)) {
  18.       $currentKey = $matches[1];
  19.       $record[$currentKey] = $matches[2];
  20.     } else {
  21.       $record[$currentKey] .= str_replace("\n", ' ', $line);
  22.     }  
  23.   }
  24.   return $record;
  25. }
  26.  
  27.  
  28. /**
  29.  * Answer 2 not using regexes
  30.  *
  31.  * Changed: Do nod read input from disk for performance measuring
  32.  *
  33.  * @autor Emo Mosley
  34.  * @link http://stackoverflow.com/a/18406908/1928023
  35.  */
  36. function answer_2($lines) {
  37.   $fields = array('HD', 'BY', 'PD', 'LP', 'TD');
  38.    # load file as array of lines
  39.   //$lines = file($filename);
  40.    if($lines === false)
  41.       return(null);
  42.    $sections = array();
  43.    $code = "";
  44.    $str = "";
  45.    # examine each line to build section array
  46.   for($i=0; $i<sizeof($lines); $i++) {
  47.       $line = trim($lines[$i]);
  48.       # check for special field codes
  49.      $left = substr($line, 0, 2);
  50.       if(in_array($left, $fields)) {
  51.          # field code detected; first, finish previous section, if exists
  52.         if($code) {
  53.             # store the previous section
  54.            $sections[$code] = trim($str);
  55.          }
  56.          # begin to process new section
  57.         $code = $left;
  58.          $str = trim(substr($line, 2));
  59.       } else if($code && $line) {
  60.          # keep a running string of section content
  61.         $str .= " ".$line;
  62.       }
  63.    } # for i
  64.   # check for no data
  65.   if(!$code)
  66.       return(null);
  67.    # store the last section and return results
  68.   $sections[$code] = trim($str);
  69.    return($sections);
  70. }
  71.  
  72.  
  73. /**
  74.  * Answer 3 using regexes
  75.  *
  76.  * @autor anubhava
  77.  * @link http://stackoverflow.com/a/18407529/1928023
  78.  */
  79. function answer_3($s) {
  80.   $match = array(); // will hold final output
  81.   if (preg_match_all('~(^|[A-Z]{2})\s(.*?)(?=[A-Z]{2}\s|$)~s', $s, $arr)) {
  82.       for ( $i = 0; $i < count($arr[1]); $i++ )
  83.          $match[ trim($arr[1][$i]) ] = str_replace( "\n", "", $arr[2][$i] );
  84.   }
  85.   return $match;
  86. }
  87.  
  88.  
  89. /**
  90.  * Answer 4 not using regexes
  91.  *
  92.  * @autor jgb
  93.  * @link http://stackoverflow.com/a/18410733/1928023
  94.  */
  95. function answer_4(array $parts, $str, $eol=PHP_EOL) {
  96.   $ret=array_fill_keys($parts, '');
  97.   $current=null;
  98.   foreach(explode($eol, $str) AS $line) {
  99.     $substr = substr($line, 0, 2);
  100.     if (isset($ret[$substr])) {
  101.       $current = $substr;
  102.       $line = trim(substr($line, 2));
  103.     }
  104.     if ($current) $ret[$current] .= $line;
  105.   }
  106.   return $ret;
  107. }
  108.  
  109.  
  110.  
  111. $str="HD Alcoa Earnings Soar; Outlook Stays Upbeat
  112. BY By James R. Hagerty and Matthew Day
  113. PD 12 July 2011
  114. LP
  115.  
  116. Alcoa Inc.'s profit more than doubled in the second quarter.
  117. The giant aluminum producer managed to meet analysts' forecasts.
  118.  
  119. However, profits wereless than expected
  120.  
  121. TD
  122. Licence this article via our website:
  123.  
  124. http://example.com";
  125. $array = explode(PHP_EOL, $str);
  126.  
  127. $iterations=100000;
  128.  
  129. /*
  130. var_dump(answer_1($array));
  131. var_dump(answer_2($array));
  132. var_dump(answer_3($str));
  133. var_dump(answer_4(array('HD', 'BY', 'PD', 'LP', 'TD'), $str));
  134. exit;
  135. */
  136.  
  137. // Start benchmarking
  138. echo "Answer 1 by: hek2mgl     ";
  139. $benchmark_start = microtime(true);
  140. for($i=0;$i<$iterations;$i++) answer_1($array);
  141. echo round(microtime(true) - $benchmark_start, 3) . " seconds (regexp)\n";
  142.  
  143. echo "Answer 2 by: Emo Mosley  ";
  144. $benchmark_start = microtime(true);
  145. for($i=0;$i<$iterations;$i++) answer_2($array);
  146. echo round(microtime(true) - $benchmark_start, 3) . " seconds\n";
  147.  
  148. echo "Answer 3 by: anubhava    ";
  149. $benchmark_start = microtime(true);
  150. for($i=0;$i<$iterations;$i++) answer_3($str);
  151. echo round(microtime(true) - $benchmark_start, 3) . " seconds (regexp)\n";
  152.  
  153. echo "Answer 4 by: jgb         ";
  154. $benchmark_start = microtime(true);
  155. for($i=0;$i<$iterations;$i++) answer_4(array('HD', 'BY', 'PD', 'LP', 'TD'), $str);
  156. echo round(microtime(true) - $benchmark_start, 3) . " seconds\n";
Advertisement
Add Comment
Please, Sign In to add comment