Skip to content

Instantly share code, notes, and snippets.

@yitsushi
Last active January 26, 2018 16:08
Show Gist options
  • Select an option

  • Save yitsushi/7b76b91a39be1c09e7e6c5ed6b1fba36 to your computer and use it in GitHub Desktop.

Select an option

Save yitsushi/7b76b91a39be1c09e7e6c5ed6b1fba36 to your computer and use it in GitHub Desktop.
<?php
$with_unsused = false;
define('LIMIT', 1000000);
class MyClass {
public function test() {}
}
function callSomething() {
(new MyClass)->test();
}
function callSomethingWithVariable() {
$var = (new MyClass)->test();
}
// Alocate memory for integer, so it does not has effect on $diff
$mem_before = 0;
$mem_after = 0;
$i = 0;
$i = 0;
$mem_before = memory_get_usage($with_unsused);
for (; $i < LIMIT; $i++) {
callSomething();
}
$mem_after = memory_get_usage($with_unsused);
echo "(new MyClass)->test();", PHP_EOL;
echo "Before: ", $mem_before, PHP_EOL;
echo "After: ", $mem_after, PHP_EOL;
echo "Diff: ", ($mem_after - $mem_before), PHP_EOL;
echo PHP_EOL;
$i = 0;
$mem_before = memory_get_usage($with_unsused);
for (; $i < LIMIT; $i++) {
callSomethingWithVariable();
}
$mem_after = memory_get_usage($with_unsused);
echo '$var = (new MyClass)->test();', PHP_EOL;
echo "Before: ", $mem_before, PHP_EOL;
echo "After: ", $mem_after, PHP_EOL;
echo "Diff: ", ($mem_after - $mem_before), PHP_EOL;
/*
❯ php --version
PHP 7.1.8 (cli) (built: Aug 29 2017 14:16:17) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
❯ php memory.php
(new MyClass)->test();
Before: 377480
After: 377480
Diff: 0
$var = (new MyClass)->test();
Before: 377512
After: 377512
Diff: 0
with:
PHP 5.6.30-0+deb8u1 (cli) (built: Feb 8 2017 08:50:21)
(new MyClass)->test();
Before: 232600
After: 232640
Diff: 40
$var = (new MyClass)->test();
Before: 232688
After: 232728
Diff: 40
*/
@yitsushi
Copy link
Copy Markdown
Author

yitsushi commented Jan 26, 2018

updated with php5.6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment