Skip to content

Instantly share code, notes, and snippets.

@Crell
Forked from olleharstedt/benchmark.js
Last active December 30, 2020 20:32
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 Crell/848568124e25c8c83fc4da5455063bab to your computer and use it in GitHub Desktop.
Save Crell/848568124e25c8c83fc4da5455063bab to your computer and use it in GitHub Desktop.
Clone benchmark
<?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));
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