Created
November 24, 2016 17:15
-
-
Save vlakoff/2569a9de8b33b05a203f6993ec39e69b to your computer and use it in GitHub Desktop.
Benchmarking https://github.com/laravel/framework/pull/16500
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 | |
namespace App; | |
class Article extends \Illuminate\Database\Eloquent\Model | |
{ | |
protected function getAttributeFromArray($key) | |
{ | |
global $BENCHMARK_1; | |
if (! empty($BENCHMARK_1)) { | |
if (isset($this->attributes[$key])) { | |
return $this->attributes[$key]; | |
} | |
} else { | |
if (array_key_exists($key, $this->attributes)) { | |
return $this->attributes[$key]; | |
} | |
} | |
} | |
public function hasCast($key, $types = null) | |
{ | |
global $BENCHMARK_2; | |
if (! empty($BENCHMARK_2)) { | |
if (isset($this->getCasts()[$key])) { | |
return $types ? in_array($this->getCastType($key), (array) $types, true) : true; | |
} | |
} else { | |
if (array_key_exists($key, $this->getCasts())) { | |
return $types ? in_array($this->getCastType($key), (array) $types, true) : true; | |
} | |
} | |
return false; | |
} | |
} |
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 | |
global $BENCHMARK_1, $BENCHMARK_2; | |
$nb = 1000; | |
$article = \App\Article::first(); | |
$t1 = microtime(true); | |
$BENCHMARK_1 = false; | |
$BENCHMARK_2 = false; | |
for ($i = $nb; $i--; ) { | |
$article->id; | |
} | |
$t2 = microtime(true); | |
$BENCHMARK_1 = true; | |
$BENCHMARK_2 = true; | |
for ($i = $nb; $i--; ) { | |
$article->id; | |
} | |
$t3 = microtime(true); | |
echo $t2 - $t1; | |
echo PHP_SAPI == 'cli' ? "\n" : '<br>'; | |
echo $t3 - $t2; | |
echo PHP_SAPI == 'cli' ? "\n\n" : '<br><br>'; | |
echo $t2 - $t1 == 0 ? 'N/A' : ($t3 - $t2) * 100 / ($t2 - $t1); | |
exit; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment