php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #55845 Iterator and ArrayAccess Interface poor perfomance than native array
Submitted: 2011-10-04 15:28 UTC Modified: 2011-10-05 00:42 UTC
From: support at freelancecode dot cz dot cc Assigned:
Status: Not a bug Package: Performance problem
PHP Version: 5.3SVN-2011-10-04 (SVN) OS: Ubuntu Natty
Private report: No CVE-ID: None
 [2011-10-04 15:28 UTC] support at freelancecode dot cz dot cc
Description:
------------
Hi there,

I want to report that implements native interface for Iterator and ArrayAccess, seriously damage cpu perfomance (compared to native array: absolutely huge).
Use my attached-class for testing for 300 array records, then perfomance suffered.

Test script:
---------------
<?php

namespace Entity;

abstract class ListArray implements \Iterator, \ArrayAccess, \Countable, \Serializable
{
    private
    
    $container = array();
    
    final public function __construct($data = array())
    {
        $this->doConstruct($data);
    }
    
    function __clone() { }
    
    protected function doConstruct($data)
    {
        $this->container = array_merge((array) $data, $this->container);
    }
    
    function __set($key, $val)
    {
        $this->container[$key] = $val;
    }
    
    public function rewind()
    {
        reset($this->container);
    }
    
    public function current()
    {
        return current($this->container);
    }
    
    public function key()
    {
        return key($this->container);
    }
    
    public function next()
    {
        next($this->container);
    }
    
    public function valid()
    {
        return (key($this->container) !== NULL);
    }
    
    public function offsetGet($offset)
    {
        return isset($this->container[$offset])? $this->container[$offset] : NULL;
    }
    
    public function offsetSet($offset, $value)
    {
        if(is_null($offset)) 
        {
            $this->container [] = $value;
        }
        else
        {
            $this->container [$offset] = $value;
        }
    }
    
    public function offsetExists($offset)
    {
        return isset($this->container[$offset]);
    }
    
    public function offsetUnset($offset)
    {
        unset($this->container[$offset]);
    }
    
    public function count()
    {
        static $count;
        return ++$count;
    }
    
    public function filter($typ)
    {
        $result = array();
        $this->rewind();
        
        while($this->valid())
        {
            if(stripos($this->key(), $typ) !== FALSE) $result[$this->key()] = $this->current();
            $this->next();
        }
        
        return $result;
    }
    
    public function serialize()
    {
        $props = array();
        foreach($this as $key => $val)
        {
            $props [$key] = $val;
        }
        
        return serialize(array($this->container, $props));
    }
    
    public function unserialize($data)
    {
        list($this->container, $props) = unserialize($data);
        foreach($props as $key => $val)
        {
            $this->$key = $val;
        }
    }
}

Expected result:
----------------
Significant perfomance like native-array

Actual result:
--------------
Poor perfomance, absolutely different from native-array 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-10-05 00:42 UTC] aharvey@php.net
-Status: Open +Status: Bogus
 [2011-10-05 00:42 UTC] aharvey@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Yep, it is.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Fri Sep 18 13:01:24 2020 UTC