PHP阵列的性能 [英] PHP array performance

查看:126
本文介绍了PHP阵列的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的#2的第一个问题,请多多包涵。

this is my first question on Stackoverflow, please bear with me.

我测试了二维装箱的算​​法,我已经选择了PHP嘲笑它,因为它是我的面包和奶油的语言时下。

I'm testing an algorithm for 2d bin packing and I've chosen PHP to mock it up as it's my bread-and-butter language nowadays.

你可以在<一见href=\"http://themworks.com/pack_v0.2/oopack.php?ol=1\">http://themworks.com/pack_v0.2/oopack.php?ol=1它的工作原理pretty很好,但你需要等待10-20秒,100矩形收拾。对于一些难以处理组,将打PHP的30岁运行限制。

As you can see on http://themworks.com/pack_v0.2/oopack.php?ol=1 it works pretty well, but you need to wait around 10-20 seconds for 100 rectangles to pack. For some hard to handle sets it would hit the php's 30s runtime limit.

我做了一些分析,这表明,大多数的时候我的剧本要通过它0和1的小二维数组的不同部分。它如果某些细胞等于0/1要么支票或将其设置为0/1。它可以做这样的操作百万次,每次时间需要几微秒。

I did some profiling and it shows that most of the time my script goes through different parts of a small 2d array with 0's and 1's in it. It either checks if certain cell equals to 0/1 or sets it to 0/1. It can do such operations million times and each times it takes few microseconds.

我想我可以在一个静态类型语言中使用布尔值数组,事情会更快。甚至使1位值的数组。我想整个事情转化为一些编译语言。 PHP是只是不为它好?

I guess I could use an array of booleans in a statically typed language and things would be faster. Or even make an array of 1 bit values. I'm thinking of converting the whole thing to some compiled language. Is PHP just not good for it?

如果确实需要将其转换为假设C ++,多么好自动转换器?我的脚本只是很多与基本循环数组和对象操作。

If I do need to convert it to let's say C++, how good are the automatic converters? My script is just a lot of for loops with basic arrays and objects manipulations.

感谢您!

编辑。这个函数被调用比任何其他。它读取一个非常简单的对象的几个属性,并经过一个短小阵列的一小部分,以检查是否有不等于0的任何元素

Edit. This function gets called more than any other. It reads few properties of a very simple object, and goes through a very small part of a smallish array to check if there's any element not equal to 0.

function fits($bin, $w, $h, $x, $y) {

    $w += $x;
    $h += $y;

    for ($i = $x; $i < $w; $i++) {

        for ($j = $y; $j < $h; $j++) {

            if ($bin[$i][$j] !== 0) {
                return false;
            }
        }
    }

    return true;    
}

更新:我已经使用一维数组,而不是二维的答案之一建议尝试。因为我需要掌握最新斌宽度访问,我决定来包装对象的一切。另外,现在在每个循环中的索引需要被计算。现在脚本会花费更长的时间来运行。其他技术并没有带来多大的性能提升,而取得code的可读性。时间街舞,我猜。

Update: I've tried using 1d array instead of 2d as one of the answers suggested. Since I needed to always have current bin width accessible, I decided to wrap everything in the object. Also, now in every loop the index needs to be calculated. Now the script takes even more time to run. Other techniques didn't bring much performance boost, rather made code less readable. Time for HipHop I guess.

更新:因为HIPHOP PHP只能运行在Linux上,我也没有,我决定重写了整个事情在C ++中。这是很好的梳洗旧的技能。另外,如果我做找到一种方法,用HIPHOP,这将是有趣的比较手写C ++ code和一个HIPHOP会产生。

Update: since hiphop php only runs on linux, and I don't have one, I've decided to rewrite the whole thing in C++. It's nice to freshen up the old skills. Also, if I do find a way to use hiphop, it'll be interesting to compare hand-written C++ code and the one hiphop would generate.

更新:我重写了C ++这个东西,平均它的工作速度快20倍,并使用更少的内存。让我看看,如果我可以让它更快。

Update: I rewrote this thing in c++, on average it works 20 times faster and uses much less memory. Let me see if I can make it even faster.

推荐答案

在PHP数组访问肯定会很慢。 PHP使用哈希表来实现阵​​列,即要想在它具有计算散列并遍历链表数组访问的元素。使用与真正的数组编译语言肯定会提高性能,因为有直接存储器存取而成。对于感兴趣的:code用于字符串和的散列访问= http://lxr.php.net/xref/PHP_5_3/Zend/zend_hash.c#zend_hash_index_find\">with整数

Array access in PHP can certainly be slow. PHP uses hash tables to implement arrays, i.e. in order to access an element in an array it has to calculate a hash and traverse a linked list. Using a compiled language with real arrays will definitely improve performance, because there a direct memory access is made. For the interested: Code for hash access with string and with integer.

关于你code,有几点我想优化:

Concerning your code, there are several points I would optimize:


  • 收益直接,不要两次。

  • $文件 - &GT; get_width() $文件 - &GT; get_height 成简单的变量。我假设高度或宽度不整个过程中发生改变。记住:在PHP函数是慢

  • 使用一维数组,而不是嵌套的数组。你救一个哈希查找每次迭代的方式。其实一维数组是只稍快,甚至稍微慢一些。 比较节省关于性能和内存使用数据的几种方法。

  • return directly, don't break twice.
  • put $file->get_width() and $file->get_height into simple variables. I assume that the height or width doesn't change throughout the process. Remember: Functions in PHP are slow.
  • Use a one-dimensional array, instead of nested arrays. You save one hash lookup per iteration that way. Actually a one-dimensional array is only marginally faster or even slightly slower. Comparison of several ways of saving the data concerning performance and memory usage.

function fits($bin, $x, $y, $w, $h) {
    $w += $x;
    $h += $y;

    for ($i = $x; $i < $w; ++$i) {
        for ($j = $y; $j < $h; ++$j) {
            if ($bin[$i][$j] !== 0) {
                return false;
            }
        } 
    }

    return true;   
}

虽然我不知道,为什么你添加 $ X $宽度 / $ Y $高度。难道你不希望从当前坐标进行迭代的图像边界?

Though I'm not sure, why you add $x to the $width / $y to the $height. Don't you want to iterate from the current coordinates to the image boundaries?

这篇关于PHP阵列的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆