Created
December 20, 2019 12:49
-
-
Save gglnx/b95b237b3790cfe2ed5b90afa4540456 to your computer and use it in GitHub Desktop.
containsMb4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Download the bible from here: https://bereanbible.com/bsb.txt | |
ini_set('memory_limit', -1); | |
require_once 'helpers.php'; | |
echo('Start: ' . convert(memory_get_usage()) . "\r\n"); | |
$string = file_get_contents('bsb.txt'); | |
echo('After File Read: ' . convert(memory_get_usage()) . "\r\n"); | |
function containsMb4(string $str): bool | |
{ | |
echo('After containsMb4 Start: ' . convert(memory_get_usage()) . "\r\n"); | |
$str_splited = str_split($str); | |
echo('After str_split: ' . convert(memory_get_usage()) . "\r\n"); | |
$str_splited_mapped = array_map('ord', $str_splited); | |
echo('After array_map: ' . convert(memory_get_usage()) . "\r\n"); | |
$max = max($str_splited_mapped); | |
echo('After max: ' . convert(memory_get_usage()) . "\r\n"); | |
return $max >= 240; | |
} | |
$executionTime = new ExecutionTime(); | |
$executionTime->start(); | |
var_dump(containsMb4($string)); | |
$executionTime->end(); | |
echo $executionTime . "\r\n"; | |
echo('After containsMb4: ' . convert(memory_get_usage()) . "\r\n"); | |
echo('Peak: ' . convert(memory_get_peak_usage()) . "\r\n"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Download the bible from here: https://bereanbible.com/bsb.txt | |
ini_set('memory_limit', -1); | |
require_once 'helpers.php'; | |
echo('Start: ' . convert(memory_get_usage()) . "\r\n"); | |
$string = file_get_contents('bsb.txt'); | |
echo('After File Read: ' . convert(memory_get_usage()) . "\r\n"); | |
function containsMb4Fixed(string $str): bool | |
{ | |
echo('After containsMb4Fixed Start: ' . convert(memory_get_usage()) . "\r\n"); | |
$length = strlen($str); | |
echo('After strlen: ' . convert(memory_get_usage()) . "\r\n"); | |
for ($i = 0; $i < $length; $i++) { | |
if (ord($str[$i]) >= 240) { | |
return true; | |
} | |
} | |
echo('After for: ' . convert(memory_get_usage()) . "\r\n"); | |
return false; | |
} | |
$executionTime = new ExecutionTime(); | |
$executionTime->start(); | |
var_dump(containsMb4Fixed($string)); | |
$executionTime->end(); | |
echo $executionTime . "\r\n"; | |
echo('After containsMb4Fixed: ' . convert(memory_get_usage()) . "\r\n"); | |
echo('Peak: ' . convert(memory_get_peak_usage()) . "\r\n"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ExecutionTime | |
{ | |
private $_startTime; | |
private $_endTime; | |
public function start() | |
{ | |
$this->_startTime = getrusage(); | |
} | |
public function end() | |
{ | |
$this->_endTime = getrusage(); | |
} | |
private function runTime($ru, $rus, $index) | |
{ | |
return ($ru["ru_$index.tv_sec"]*1000 + intval($ru["ru_$index.tv_usec"]/1000)) | |
- ($rus["ru_$index.tv_sec"]*1000 + intval($rus["ru_$index.tv_usec"]/1000)); | |
} | |
public function __toString() | |
{ | |
return 'This process used ' . $this->runTime($this->_endTime, $this->_startTime, 'utime') . | |
" ms for its computations\nIt spent " . $this->runTime($this->_endTime, $this->_startTime, 'stime') . | |
' ms in system calls'; | |
} | |
} | |
function convert($size) | |
{ | |
$unit = ['b', 'kb', 'mb', 'gb', 'tb', 'pb']; | |
return @round($size / pow(1024, ( $i = floor(log($size, 1024)))), 2) . ' ' . $unit[$i]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment