Skip to content

Instantly share code, notes, and snippets.

@Crell

Crell/benchmark.php

Forked from olleharstedt/benchmark.js
Last active Dec 30, 2020
Embed
What would you like to do?
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