php - 如何最好地将元素添加到 PHP 中任意索引处的数组?

标签 php arrays performance interpreter

如何编写一个解决方案,使当前的 PHP 解释器 (5.4) 足够智能,只需执行大约 3-5 个副本,而不是完整的逐项数组排序?

注意,我知道一些将元素插入索引数组的方法。然而这并不能满足我的理解。例如C++ ,您可以使用 std::copy 执行某些操作,或者将结构或联合作为多元素数组游标。

所以我想知道我是否以某种方式遵守 PHP 规则,可以使用什么语法在幕后拥有更接近的东西

Copy the [range of elements from some index to the end of A] into temp C

Copy B into A[Index],

Copy C into A[Index+count(B)]

比这个...

$MasterItemList = $Page[$CurrentPage]->GetItems();   /* Returns an array with 512 Items.         */
$UpdateList = GetUpdatePage();                       /* Returns multi-dimensional array such that: 
                                                        $result[][0]=an index and 
                                                        $result[][1]=a list of items             */

foreach($UpdateList as $Update)
{ foreach($Update as $cursor => $ItemList)
  {
    $cursor=$cursor+0;  //to int..
    $numitems=count($ItemList);

    if($ItemList[0]->NewAddition)
    {
      $BeforeUpdate=array_splice($MasterItemList,0, $cursor, true);
      $AfterUpdate=array_splice($MasterItemList, $cursor+$numitems, 0);
      $MasterItemList=array_merge($BeforeUpdate,$ItemList,$AfterUpdate);

      $Page[$CurrentPage]->OffsetCorrection+=$numitems;
    }
    else
    {
      $i=0;
      foreach($ItemList as $LineItem)
      {
        $MasterItemList[$cursor+$i] = $LineItem;
        $i++;
      }
    }
  }
}

如果我记下的内容有一些错误,请原谅我,请告诉我,我会更正它们。

也就是说,我认为解释器无法使用正确的引用和范围来直接使用此方法执行逻辑。这看起来已经是一件非常昂贵的事情了。如何才能以“正确的方式”为 PHP 做到这一点?

Examples:

// An Update List

Array(
    [0] => Array(
        [0] => 31
        [1] => Array(
            [1] => stdClass Object 
                (
                    [NewAddition] => false
                    [Name] => "********"
                    [Date] => 1364920943
                    [Active] => 1
                    .
                    .
                    .
                )

            [2] => stdClass Object 
                (
                    [NewAddition] => false
                    [Name] => "********"
                    [Date] => 1364920943
                    [Active] => 1
                    .
                    .
                    .
                )

            [3] => stdClass Object 
                (
                    [NewAddition] => false
                    [Name] => "********"
                    [Date] => 1364920943
                    [Active] => 1
                    .
                    .
                    .
                )

        )                
    )
)

MasterItemList 只是这些相同对象的数组(class Item)。

需要注意的几点:

  • 仅在对该脚本重要的任何地方以纯顺序方式访问此数据。
  • 在脚本的这一部分中,仅需要检查新插入集中的第一项是否有更新。该集合中的所有后续项目将始终是新的。
  • 尾随超过 512 的项目会自动调整到下一页加载。我可以调整页面大小以在数组排序性能和数据获取性能(异步缓冲)之间进行权衡。

最佳答案

首先,PHP数组不是数据结构意义上的“数组”;它们实际上是哈希表和双向链表合二为一。当您对数组进行索引时,例如对 $list[$i]$i 进行哈希处理以查找对应的元素;这不是简单的算术,例如C++。

此外,由于数组也是链表,因此 array_splice 的实现比看起来要高效得多,至少在被删除的部分足够小时(散列新项通常很快,并且在链表的某个位置插入项是常数时间)。

当然,这意味着 PHP 数组比“纯”数组消耗更多的内存,并且如果您只想进行基于索引的访问,它们也会变慢。在这些情况下,SPL 提供 SplFixedArray这是数据结构意义上的数组的实现。

在您的特定情况下,array_splice 应该是您的第一选择;您只需一次调用即可插入数组 block :

array_splice($MasterItemList, $cursor, 0, $ItemList);

关于php - 如何最好地将元素添加到 PHP 中任意索引处的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15806366/

相关文章:

c++ - 编译器优化 : g++ slower than intel

java - Java中的ResultSet到整数数组

java - 如何在我的方法中返回数组?

javascript - 无法通过ajax将字符串参数传递给php函数

php - 覆盖组件/com_users/models/forms/login.xml 添加类 - Joomla 3

c++ - 在 C 中使用数组

python - 寻找下一个素数

mysql - 随着我的 mysql 数据库的增长,我有什么选择

php - Symfony2 - 更改现有用户的编码器

php - 无法可靠地确定 MacBook 服务器的完全限定域名