Skip to content

Instantly share code, notes, and snippets.

@vlakoff
Created November 24, 2016 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vlakoff/2569a9de8b33b05a203f6993ec39e69b to your computer and use it in GitHub Desktop.
Save vlakoff/2569a9de8b33b05a203f6993ec39e69b to your computer and use it in GitHub Desktop.
<?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;
}
}
<?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