Skip to content

Instantly share code, notes, and snippets.

@divinity76
Last active September 11, 2020 21:48
Show Gist options
  • Save divinity76/b0471cfa6596c92dd6aba2078ea0fde8 to your computer and use it in GitHub Desktop.
Save divinity76/b0471cfa6596c92dd6aba2078ea0fde8 to your computer and use it in GitHub Desktop.
php benchmarking script
#!/usr/bin/php
<?php
declare(strict_types = 1);
function method1(string $domain)
{
$domain = str_replace('https://', '', $domain);
$domain = str_replace('http://', '', $domain);
$domain = str_replace('www.easy-ad.no', 'ads.easy-ads.com', $domain);
return $domain;
}
function method2(string $domain)
{
return strtr($domain, array(
'https://' => '',
'http://' => '',
'www.easy-ad.no' => 'ads.easy-ads.com'
));
}
for ($i = 0; $i < 100000; ++ $i) {
// heat up cpu (if it was powersaving-running-low-cus-nothing-was-happening, most modern cpus try to do it by default)
}
$data = [
"method1" => null,
"method2" => null
];
$time = microtime(true);
for ($i = 0; $i < 1000000; ++ $i) {
method1("https://www.easy-ad.no");
}
$data["method1"] = microtime(true) - $time;
$time = microtime(true);
for ($i = 0; $i < 1000000; ++ $i) {
method2("https://www.easy-ad.no");
}
$data["method2"] = microtime(true) - $time;
if ($data["method1"] === $data["method2"]) {
$data["winner"] = "ITS A DRAW! wow!";
} else {
$data["winner"] = ($data["method1"] > $data["method2"]) ? "method2" : "method1";
}
$data["diffrence_milliseconds"] = abs($data["method1"]-$data["method2"]) * 1000;
print_r($data);
echo "\n";
@divinity76
Copy link
Author

divinity76 commented Dec 26, 2017

warning, not well tested, might be bugs reconstructing the commandline from $argv, i don't know. (specifically, im not sure if $cmd.=$tmp.' '; should actually be $cmd.=escapeshellarg($tmp).' '; or not )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment