Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* How many times the code under test should run in each function */
- define('LOOP', 10000);
- function f1()
- {
- $data = range(0, LOOP);
- for ($value = null, $i = 0; $i < LOOP; ++$i) {
- if (in_array($i, $data, true)) {
- $value = $i;
- } else {
- $value = null;
- }
- }
- }
- function f2()
- {
- $data = range(0, LOOP);
- for ($value = null, $i = 0; $i < LOOP; ++$i) {
- $value = in_array($i, $data, true) ? $i : null;
- }
- }
- $start = microtime(true);
- f2();
- echo "\nSHORT: ".(microtime(true) - $start).' - MEMORY: '.memory_get_usage().' - MEMORY PEAK: '.memory_get_peak_usage();
- $start = microtime(true);
- f1();
- echo "\nLARGE: ".(microtime(true) - $start).' - MEMORY: '.memory_get_usage().' - MEMORY PEAK: '.memory_get_peak_usage();
- echo "\n\n";
- ➜ microoptimizations php -v
- PHP 5.6.4-4ubuntu6.2 (cli) (built: Jul 2 2015 15:29:28)
- Copyright (c) 1997-2014 The PHP Group
- Zend Engine v2.6.0, Copyright (c) 1998-2014 Zend Technologies
- with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2014, by Zend Technologies
- with Xdebug v2.2.6, Copyright (c) 2002-2014, by Derick Rethans
- ➜ microoptimizations php if-vs-shortif.php
- LARGE: 0.35390591621399 - MEMORY: 240720 - MEMORY PEAK: 1734208
- SHORT: 0.33055996894836 - MEMORY: 240816 - MEMORY PEAK: 1734632
- ➜ microoptimizations php if-vs-shortif.php
- SHORT: 0.32460117340088 - MEMORY: 240720 - MEMORY PEAK: 1734208
- LARGE: 0.328537940979 - MEMORY: 240816 - MEMORY PEAK: 1734632
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement