Skip to content

Instantly share code, notes, and snippets.

@alexandr-parkhomenko
Created May 29, 2017 15:32
Show Gist options
  • Save alexandr-parkhomenko/36e393af8e8910d67471a1958c1e004d to your computer and use it in GitHub Desktop.
Save alexandr-parkhomenko/36e393af8e8910d67471a1958c1e004d to your computer and use it in GitHub Desktop.
Array vs Object comparison
<?php
$time = microtime(true);
$memory = memory_get_usage(true);
$collection = [];
$randSrtring = hash_hmac('md5',rand(0, 1000), 'some key');
for ($i = 0; $i <= 1000000; $i++) {
$region = [];
$region['city'] = $randSrtring;
$region['city_code'] = $randSrtring;
$region['country'] = $randSrtring;
$region['country_code'] = $randSrtring;
array_push($collection, $region);
}
$resultTime = @(microtime(true) - $time);
$resultMemory = @round((memory_get_usage(true) - $memory) / (1024 * 1024));
echo sprintf(
"Time: %s, Memory: %sMB",
$resultTime,
$resultMemory
);
<?php
$time = microtime(true);
$memory = memory_get_usage(true);
$collection = [];
$randSrtring = hash_hmac('md5',rand(0, 1000), 'some key');
for ($i = 0; $i <= 1000000; $i++) {
$region = [
'city' => $randSrtring,
'city_code' => $randSrtring,
'country' => $randSrtring,
'country_code' => $randSrtring
];
array_push($collection, $region);
}
$resultTime = @(microtime(true) - $time);
$resultMemory = @round((memory_get_usage(true) - $memory) / (1024 * 1024));
echo sprintf(
"Time: %s, Memory: %sMB",
$resultTime,
$resultMemory
);
<?php
class Region
{
protected $city;
protected $country;
protected $cityCode;
protected $countryCode;
/**
* @return mixed
*/
public function getCity()
{
return $this->city;
}
/**
* @param mixed $city
*/
public function setCity($city)
{
$this->city = $city;
}
/**
* @return mixed
*/
public function getCountry()
{
return $this->country;
}
/**
* @param mixed $country
*/
public function setCountry($country)
{
$this->country = $country;
}
/**
* @return mixed
*/
public function getCityCode()
{
return $this->cityCode;
}
/**
* @param mixed $cityCode
*/
public function setCityCode($cityCode)
{
$this->cityCode = $cityCode;
}
/**
* @return mixed
*/
public function getCountryCode()
{
return $this->countryCode;
}
/**
* @param mixed $countryCode
*/
public function setCountryCode($countryCode)
{
$this->countryCode = $countryCode;
}
}
$time = microtime(true);
$memory = memory_get_usage(true);
$collection = [];
$randSrtring = hash_hmac('md5',rand(0, 1000), 'some key');
for ($i = 0; $i <= 1000000; $i++) {
$region = new Region();
$region->setCity($randSrtring);
$region->setCityCode($randSrtring);
$region->setCountry($randSrtring);
$region->setCountryCode($randSrtring);
array_push($collection, $region);
}
$resultTime = @(microtime(true) - $time);
$resultMemory = @round((memory_get_usage(true) - $memory) / (1024 * 1024));
echo sprintf(
"Time: %s, Memory: %sMB",
$resultTime,
$resultMemory
);
PHP: 7.1.2
1 mln records
-DTO-
Time: 0.26642107963562, Memory: 148MB
-Array with dynamic adding keys-
Time: 0.2154221534729, Memory: 390MB
-Array with predefined keys-
Time: 0.18599009513855, Memory: 390MB
10 mln
-DTO-
Time: 3.2697010040283, Memory: 1726MB
-Array with dynamic adding keys-
Time: 4.7362878322601, Memory: 4106MB
-Array with predefined keys-
Time: 3.8427510261536, Memory: 4106MB
PHP: 7.0.11
1 mln
-DTO-
Time: 0.3204300403595, Memory: 148MB
-Array with dynamic adding keys-
Time: 0.36689400672913, Memory: 390MB
-Array with predefined keys-
Time: 0.34257411956787, Memory: 390MB
10 mln
-DTO-
Time: 7.8174288272858, Memory: 1726MB
-Array with dynamic adding keys-
Time: 17.430032014847, Memory: 4106MB
-Array with predefined keys-
Time: 19.937174081802, Memory: 4106MB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment