Answer the question
In order to leave comments, you need to log in
PHP: which is faster - arrays or objects?
Good afternoon! There is a highly loaded application that acts as an intermediary: it receives data on the socket, processes it in some way, and sends it further to the socket.
The amount of incoming data in critical situations reaches tens of thousands of elements per second, so every thousandth and ten thousandth of a second counts.
In addition to processing incoming data, the application also takes data from the database and performs similar operations with it.
Historically, this whole scheme works on arrays, i.e. each data element is an associative array that you have to work with in dozens of classes, while remembering all its keys, and remembering the operations that are applied to this data.
This is catastrophically inconvenient, and any refactoring (for example, adding another key to the array) leads to the fact that you have to pick half the project to make a simple edit.
The question was repeatedly raised about how to implement this scheme using objects that would store data and methods for working with them. However, this proposal is always rejected on the grounds that arrays are faster and objects are slower.
My question is, do you know of any research that compares the performance of arrays and objects in PHP? Preferably with examples and tests.
Thanks a lot.
Answer the question
In order to leave comments, you need to log in
And here I tested a little.
<?php
$test_iteration = 50000000;
function get_rand_data() {
return [
'a' => mt_rand(1, 1000),
'b' => mt_rand(1, 1000),
'c' => mt_rand(1, 1000),
'd' => mt_rand(1, 1000),
'e' => mt_rand(1, 1000),
];
}
function prepare_data(&$data) {
return ($data['a'] * $data['b'] / ( $data['c'] * $data['d'] )) / $data['e'];
}
class Data {
protected $data = [];
public function __construct(array &$data = []) {
$this->data = $data;
}
public function run() {
return ($this->data['a'] * $this->data['b'] / ( $this->data['c'] * $this->data['d'] )) / $this->data['e'];
}
public function setData(array &$data) {
$this->data = $data;
return $this;
}
public function setDataNotReturn(array &$data) {
$this->data = $data;
}
}
class StaticData {
public static function run(&$data) {
return ($data['a'] * $data['b'] / ( $data['c'] * $data['d'] )) / $data['e'];
}
}
function show_delta($end, $start, $text) {
return $text.': '.round(($end-$start), 5). " seconds\n";
}
$start = microtime(1);
for($i =0; $i<$test_iteration; ++$i) {
$data = get_rand_data();
prepare_data($data);
}
echo show_delta(microtime(1), $start, 'Array mode');
$start = microtime(1);
for($i =0; $i<$test_iteration; ++$i) {
$data = get_rand_data();
$result = ($data['a'] * $data['b'] / ( $data['c'] * $data['d'] )) / $data['e'];
}
echo show_delta(microtime(1), $start, 'Array run body for mode');
$start = microtime(1);
for($i =0; $i<$test_iteration; ++$i) {
$data = get_rand_data();
(new Data($data))->run();
}
echo show_delta(microtime(1), $start, 'Object mode');
$start = microtime(1);
$runner = new Data();
for($i =0; $i<$test_iteration; ++$i) {
$data = get_rand_data();
$runner->setData($data)->run();
}
echo show_delta(microtime(1), $start, 'Object mode (v2)');
$start = microtime(1);
$runner = new Data();
for($i =0; $i<$test_iteration; ++$i) {
$data = get_rand_data();
$runner->setDataNotReturn($data);
$runner->run();
}
echo show_delta(microtime(1), $start, 'Object mode (v3)');
$start = microtime(1);
for($i =0; $i<$test_iteration; ++$i) {
$data = get_rand_data();
StaticData::run($data);
}
$delta = microtime(1) - $start;
echo show_delta(microtime(1), $start, 'Static object mode');
I don’t remember such comparisons in terms of performance, but there are from memory:
https://habrahabr.ru/post/161629/
https://habrahabr.ru/post/141093/
In general, only tests will help, both in your environment and on your data.
Try to look at it all with a profiler, look for bottlenecks. Try accelerators, php 7, hhvm.
Is it possible to parallelize processing?
Maybe it’s worth throwing everything away and writing a demon in C, or, as it’s fashionable now, in Go, for example?
faster than SPL - use them, not "arrays or objects". the whole problem is the universality of arrays in php. google the article on habré
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question