PHP Array and ArrayObject

后端 未结 6 1850
鱼传尺愫
鱼传尺愫 2021-02-02 08:03

which one should be used to manipulating data, Array or Array Object? Like search,sort and other array manipulations.

6条回答
  •  遇见更好的自我
    2021-02-02 08:28

    In term of performance, you won't notice a real difference between an array and a ArayObject. I run a simple test. The idea was to create arrays using array() and new ArrayObject, and fill them with an increasing number of values.

    
    

    Results

    method             lines     average (µs)    difference between methods (µs)
    array                  1           2.470         -1.044
    array                 10           8.452         +0.315
    array                100          71.862        +10.719
    array              1,000         773.826       +141.962
    array             10,000       7,868.731       -675.359
    array            100,000      76,954.625    -17,665.510
    array          1,000,000     801,509.550    -84,356.148
    ArrayObject            1           3.514         +1.044
    ArrayObject           10           8.137         -0.315
    ArrayObject          100          61.142        -10.719
    ArrayObject        1,000         631.864       -141.962
    ArrayObject       10,000       8,544.090       +675.359
    ArrayObject      100,000      94,620.135    +17,665.510
    ArrayObject    1,000,000     885,865.698    +84,356.148
    

    The average is the average time of the 100 tests for each method and each number of lines. The difference between methods is quite insignificant (84 microseconds when you deal with a million rows...)

    I've run this test many times, and because the differences are always a question of microseconds, sometimes a method is more efficient during one test, then less efficient during the next test...

    The choice will depend of your needs:

    • if you deal with simple arrays, and do a loop like foreach() or calculate an average, an array is quite enough,
    • if you need more complex iterations, sorting, filtering, ... it's easier to expand the ArrayObject class with your own iterator, methods...

提交回复
热议问题