@ 2017-11-18T03:38:43Z <?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' => ''
]);
// tests
$tests = ['original' => $original, 'simple' => $simple, '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.12 0.010 0.030 15.32 7.3.11 0.003 0.033 15.38 7.3.10 0.003 0.036 15.25 7.3.9 0.013 0.027 15.25 7.3.8 0.007 0.031 15.44 7.3.7 0.010 0.027 15.21 7.3.6 0.000 0.034 15.27 7.3.5 0.009 0.025 15.43 7.3.4 0.000 0.037 15.31 7.3.3 0.010 0.029 15.30 7.3.2 0.000 0.038 17.03 7.3.1 0.010 0.029 16.74 7.3.0 0.003 0.032 17.01 7.2.24 0.003 0.028 15.55 7.2.23 0.009 0.028 15.15 7.2.22 0.003 0.034 15.57 7.2.21 0.007 0.033 15.53 7.2.20 0.014 0.025 15.48 7.2.19 0.011 0.025 15.35 7.2.18 0.006 0.029 15.56 7.2.17 0.003 0.031 15.34 7.2.16 0.003 0.030 15.30 7.2.15 0.013 0.026 16.99 7.2.14 0.006 0.029 17.16 7.2.13 0.006 0.032 16.98 7.2.12 0.006 0.028 16.97 7.2.11 0.003 0.030 17.21 7.2.10 0.012 0.025 17.33 7.2.9 0.007 0.030 17.00 7.2.8 0.003 0.030 17.23 7.2.7 0.007 0.030 17.41 7.2.6 0.013 0.029 17.29 7.2.5 0.006 0.029 17.10 7.2.4 0.007 0.030 17.28 7.2.3 0.003 0.037 17.24 7.2.2 0.003 0.029 16.95 7.2.1 0.007 0.031 17.05 7.2.0 0.014 0.024 17.13 7.1.33 0.010 0.027 16.00 7.1.32 0.000 0.042 16.21 7.1.31 0.003 0.039 16.33 7.1.30 0.007 0.033 15.95 7.1.29 0.010 0.033 16.20 7.1.28 0.007 0.033 16.01 7.1.27 0.010 0.030 16.21 7.1.26 0.012 0.028 16.10 7.1.25 0.013 0.025 16.29 7.1.11 0.016 0.043 16.61 7.1.10 0.031 0.064 16.83 7.1.9 0.034 0.055 16.97 7.1.8 0.032 0.074 16.77 7.1.7 0.019 0.048 15.84 7.1.6 0.033 0.046 33.31 7.1.5 0.034 0.044 33.07 7.1.4 0.034 0.041 32.65 7.1.3 0.027 0.050 32.77 7.1.2 0.030 0.052 32.85 7.1.1 0.020 0.043 15.23 7.1.0 0.022 0.044 15.38 7.0.25 0.020 0.042 16.48 7.0.24 0.022 0.040 16.62 7.0.23 0.024 0.047 16.51 7.0.22 0.019 0.048 16.47 7.0.21 0.029 0.049 15.16 7.0.20 0.026 0.043 15.28 7.0.19 0.020 0.039 15.48 7.0.18 0.016 0.044 14.99 7.0.17 0.025 0.037 14.86 7.0.16 0.020 0.043 14.98 7.0.15 0.023 0.042 15.05 7.0.14 0.106 0.043 15.23 7.0.13 0.030 0.066 15.18 7.0.12 0.017 0.055 15.03 7.0.11 0.015 0.047 15.17 7.0.10 0.013 0.048 14.87 7.0.9 0.020 0.041 14.95 7.0.8 0.019 0.054 14.87 7.0.7 0.025 0.079 14.89 7.0.6 0.026 0.045 14.76 7.0.5 0.021 0.043 14.86 7.0.4 0.013 0.049 14.96 7.0.3 0.023 0.043 15.04 7.0.2 0.021 0.046 14.93 7.0.1 0.022 0.044 15.03 7.0.0 0.023 0.062 15.09
preferences:dark mode live preview ace vim emacs key bindings
21.39 ms | 403 KiB | 5 Q