@ 2017-11-18T04:13:53Z <?php
$charmap = [
'NUL' => "\x00", // NULL (U+0000)
'SOH' => "\x01", // START OF HEADING (U+0001)
'STX' => "\x02", // START OF TEXT (U+0002)
'ETX' => "\x03", // END OF TEXT (U+0003)
'EOT' => "\x04", // END OF TRANSMISSION (U+0004)
'ENQ' => "\x05", // ENQUIRY (U+0005)
'HT' => "\x09", // HORIZONTAL TAB (U+0009)
'LF' => "\x0a", // LINE FEED (U+000A)
'VT' => "\x0b", // VERTICAL TAB (U+000B)
'CR' => "\x0d", // CARRIAGE RETURN (U+000D)
'ETB' => "\x17", // END OF TRANSMISSION BLOCK (U+0017)
'SP' => "\x20", // SPACE (U+0020)
'ZWS' => "\xe2\x80\x8b", // ZERO WIDTH SPACE (U+200B)
'MSBS' => "\xf0\x9d\x85\xb7", // MUSICAL SYMBOL BEGIN SLUR (U+1D177)
'MSBP' => "\xf0\x9d\x85\xb9", // MUSICAL SYMBOL BEGIN PHRASE (U+1D179)
];
foreach ($charmap as $k => $v) {
define($k, $v);
}
$strings = [
'user6003859' =>
LF .
LF .
LF .
'a' . SP . 'b'. SP . SP . 'c' . SP . SP . SP . LF .
SP . 'd' . SP . SP . SP . SP . 'e' . LF .
MSBS . 'f' . MSBS . 'g' . MSBS . MSBS . 'h' . MSBS . MSBS . ZWS . ZWS . MSBP . MSBP . 'i' . MSBP . SP . MSBP . SP . 'j' . LF .
LF .
LF .
LF .
LF .
'k' . SP . 'l' . SP . 'm' . SP . 'n' . SP . SP . SP . LF .
MSBP . 'o' . MSBP . MSBP . 'p' . LF .
LF .
LF .
LF,
'mickmackusa' =>
NUL . LF .
LF .
SOH . LF .
CR . LF .
VT .
ETB . 'a' . SP . 'ab' . CR . LF .
HT . HT . CR .
CR . LF .
'cà' . SOH . 'ê߀' . NUL . NUL . 'abcbc'. SP . SP . SP . 'd' . LF .
LF .
HT . CR . LF .
ENQ . SP . SP . SP . 'e' . STX . LF .
ETX . LF .
EOT . LF
];
function display($str, $charmap) {
$converter = array_map(function ($i) { return '{'.$i.'}'; }, array_flip($charmap));
$handle = fopen("data:text/plain,$str", 'r');
while ( false !== $line = fgets($handle) ) {
echo strtr($line, $converter), PHP_EOL;
}
fclose($handle);
}
class Replacements {
const FUNC = 0;
const REGEX = 1;
protected $patterns;
protected $replacements;
protected $func;
protected $typeRegex;
public function __construct($arg) {
if ( is_array($arg) ) {
$this->type = self::REGEX;
$this->patterns = [];
$this->replacements = [];
$this->addPatterns($arg);
} elseif ( is_callable($arg) ) {
$this->type = self::FUNC;
$this->addFunction($arg);
} else throw new Exception('invalid argument type');
}
protected function addPatterns($replacements) {
foreach($replacements as $pattern => $replacement) {
$this->patterns[] = $pattern;
$this->replacements[] = $replacement;
}
}
protected function addFunction($func) {
$this->func = $func;
}
public function execute($str) {
if ( $this->type === self::REGEX )
return preg_replace($this->patterns, $this->replacements, $str);
return call_user_func_array($this->func, [&$str]);
}
};
$original = new Replacements([
'~\R~u' => "\n",
'/(?:^((\pZ)+|((?!\n)\pC)+)(?1)*)|((?1)$)|(?:((?2)+|(?3)+)(?=(?2)|(?3)))/um' => '',
'/(\pZ+)|((?!\n)\pC)/u' => ' ',
'/(^\n+)|(\n+$)|(\n(?=\n{2}))/u' => ''
]);
$simple = new Replacements([
'~\A[\pZ\pC]+|[\pZ\pC]+\z~u' => '', # trim the string
'~\R~u' => "\n", # normalize newlines
'~\pZ+|[^\n\PC]+~u' => ' ', # replace Z and C with space
'~^ +| +$| \K +~m' => '', # trim lines, delete consecutive spaces
'~\n\n\K\n+~' => '' # removes more than 2 consecutives newlines
]);
$optimized = new Replacements([
'~\r\n?|\x0b|\f|\xe2\x80[\xa8\xa9]~S' => "\n",
'~
[^\pZ\pC]+ \K
\pZ* (?:[^\PC\n]+\pZ*)*
(?: (\n) \pZ*+ (?:[^\PC\n]+\pZ*)*+ (?: (\n) [\pZ\pC]* )?+ (?!\z) | [\pZ\pC]+ )?
|
[\pZ\pC]+
~Aux' => '$1$2 ',
'~ (?:$|(?<=^ ))~m' => ''
]);
$func = new Replacements(function (&$str) {
$parts = preg_split('~^[\pC\pZ]+|[\pC\pZ]+$|\R(?:[\pC\pZ]*?(\R)[\pC\pZ]*)?~u', $str, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
return implode("\n", array_map(function($i) { return trim(preg_replace('~[\pC\pZ]+~u', ' ', $i));}, $parts));
});
// tests
$tests = ['original' => $original, 'simple' => $simple, 'function' => $func, 'optimized' => $optimized];
$str = $strings['user6003859'] . $strings['mickmackusa'];
$str = str_repeat($str, 10);
$res = [];
foreach ($tests as $k=>&$test) {
$res[$k] = $test->execute($str);
}
echo 'same result: ', var_dump(count(array_unique($res)) === 1);
$names = array_keys($tests);
$times = array_fill_keys($names, 0);
define('REPETITIONS', 100);
for ($i=0; $i < REPETITIONS; $i++) {
shuffle($names);
foreach ($names as $name) {
$start = microtime(true);
$tests[$name]->execute($str);
$stop = microtime(true);
$times[$name] += $stop - $start;
}
}
foreach($times as $k=>$v) {
printf("%-12s: %.2es\n", $k, $v/REPETITIONS);
}
// display($res['optimized'], $charmap);
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 7.3.3 0.003 0.047 15.06 7.3.2 0.065 0.023 15.47 7.3.1 0.071 0.024 15.43 7.3.0 0.062 0.030 15.47 7.2.16 0.007 0.056 15.25 7.2.15 0.066 0.025 15.54 7.2.14 0.066 0.049 15.67 7.2.13 0.077 0.050 15.47 7.2.12 0.046 0.030 15.51 7.2.11 0.054 0.048 15.41 7.2.10 0.053 0.053 15.61 7.2.9 0.081 0.033 15.50 7.2.8 0.072 0.036 15.42 7.2.7 0.070 0.040 15.55 7.2.6 0.059 0.030 15.40 7.2.5 0.064 0.023 15.59 7.2.4 0.052 0.029 15.61 7.2.3 0.055 0.044 15.69 7.2.2 0.052 0.039 15.51 7.2.1 0.057 0.047 15.56 7.2.0 0.049 0.029 15.55 7.1.27 0.063 0.030 14.25 7.1.26 0.058 0.036 14.55 7.1.25 0.054 0.044 14.41 7.1.24 0.048 0.051 14.13 7.1.23 0.054 0.037 14.45 7.1.22 0.065 0.034 14.52 7.1.21 0.074 0.036 14.29 7.1.20 0.060 0.066 14.53 7.1.19 0.079 0.049 14.24 7.1.18 0.071 0.040 14.27 7.1.17 0.063 0.054 14.45 7.1.16 0.068 0.043 14.44 7.1.15 0.076 0.033 14.41 7.1.14 0.071 0.050 14.50 7.1.13 0.072 0.049 14.33 7.1.12 0.067 0.039 14.44 7.1.11 0.043 0.048 15.64 7.1.10 0.040 0.052 15.48 7.1.9 0.040 0.056 15.75 7.1.8 0.043 0.043 15.76 7.1.7 0.034 0.057 14.99 7.1.6 0.035 0.051 24.01 7.1.5 0.048 0.055 23.84 7.1.4 0.043 0.052 23.54 7.1.3 0.047 0.050 23.51 7.1.2 0.038 0.057 23.51 7.1.1 0.038 0.059 14.78 7.1.0 0.049 0.071 14.98 7.0.33 0.067 0.045 14.05 7.0.32 0.141 0.051 13.85 7.0.31 0.058 0.058 14.11 7.0.30 0.063 0.052 14.05 7.0.29 0.064 0.035 14.08 7.0.28 0.071 0.047 13.61 7.0.27 0.057 0.034 13.80 7.0.26 0.070 0.047 14.13 7.0.25 0.050 0.063 15.32 7.0.24 0.048 0.066 15.25 7.0.23 0.064 0.051 15.00 7.0.22 0.036 0.049 15.29 7.0.21 0.066 0.043 14.62 7.0.20 0.034 0.049 14.67 7.0.19 0.039 0.048 14.69 7.0.18 0.037 0.056 14.42 7.0.17 0.042 0.043 14.40 7.0.16 0.035 0.054 14.43 7.0.15 0.035 0.052 14.56 7.0.14 0.038 0.053 14.47 7.0.13 0.039 0.054 14.63 7.0.12 0.042 0.052 14.61 7.0.11 0.040 0.046 14.53 7.0.10 0.036 0.050 14.61 7.0.9 0.036 0.053 14.61 7.0.8 0.034 0.046 14.41 7.0.7 0.036 0.050 14.47 7.0.6 0.034 0.051 14.35 7.0.5 0.037 0.054 14.58 7.0.4 0.036 0.056 14.60 7.0.3 0.045 0.066 14.63 7.0.2 0.054 0.045 14.71 7.0.1 0.043 0.045 14.69 7.0.0 0.046 0.055 14.62 5.6.40 0.030 0.151 14.89 5.6.39 0.014 0.150 14.71 5.6.38 0.022 0.148 14.65
preferences:dark mode live preview
47.05 ms | 400 KiB | 5 Q