-
-
Save Crell/848568124e25c8c83fc4da5455063bab to your computer and use it in GitHub Desktop.
Clone benchmark
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 | |
const ITERS = 1000000; | |
class QueryBuilder { | |
public $select; | |
public $from; | |
public $where; | |
public function withSelect($sel) { | |
$that = clone $this; | |
$that->select = $sel; | |
return $that; | |
} | |
public function withFrom($from) { | |
$that = clone $this; | |
$that->from = $from; | |
return $that; | |
} | |
public function withWhere($where) { | |
$that = clone $this; | |
$that->where = $where; | |
return $that; | |
} | |
public function setSelect($sel) { | |
$this->select = $sel; | |
} | |
public function setFrom($from) { | |
$this->from = $from; | |
} | |
public function setWhere($where) { | |
$this->where = $where; | |
} | |
} | |
// Ensure all variables are pre-defind, for fairness. | |
$i = $start = $end = 0; | |
$start = microtime(true); | |
for ($i = 0; $i < ITERS; $i++) { | |
$q = (new QueryBuilder())->withSelect('1')->withFrom('table')->withWhere('a = b'); | |
unset($q); | |
} | |
$end = microtime(true); | |
printf("Wither methods: %f\n", number_format($end-$start, 4)); | |
$start = microtime(true); | |
for ($i = 0; $i < ITERS; $i++) { | |
$q = new QueryBuilder(); | |
$q->setSelect('1'); | |
$q->setFrom('table'); | |
$q->setWhere('a = b'); | |
unset($q); | |
} | |
$end = microtime(true); | |
printf("Setter methods: %f\n", number_format($end-$start, 4)); |
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
Wither methods: 2.377100 | |
Setter methods: 1.784800 | |
(Under PHP 7.4.12) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment