Faster Arrays
Arrays, long considered the work horse of PHP have one flaw: they can be incredibly slow. There is however an alternative — at least, for a small subset of use cases. SplFixedArray.
You use SplFixedArray like so:
The SplFixedArray class provides a super-fast, fixed size array implementation. There are some limitations however, first you must use numeric keys and secondly you cannot use anonymous assignment (i.e. $array[] = 'value';).
You’ll notice one requirement was missing, that it should have a fixed size. While having a fixed size is what will bring you the speed increase it’s actually not a requirement that the size be fixed. Though you must specify a size to the constructor, you can change it (and lose most — if not all — speed benefits) at any time using SplFixedArray->setSize().
So, what sort of speed increase are we talking about? In my testing of arrays 100, 100… 1,000,000 elements, you will see a speed increase about 20-25%; for arrays smaller than 100, it will actually be slower by 25-40%.
The benchmarking was very simple, a comparison of a read and write iteration for both normal and fixed arrays of different sizes like so:
Additionally, the memory usage to run the benchmarks for array vs SplFixedArray is significantly different, regular arrays clock in at 198MB while SplFixedArray uses a mere 83MB, that’s a 59% memory saving.
In practical terms, you’re only going to be worried about the speed of arrays when you’re dealing with larger arrays anyway, so the speed loss for the lower digits isn’t a big concern… but where exactly could this be useful?
There is one common scenario where you will commonly be dealing with large numerically indexed arrays of data: Your database result sets. Using PDO, you can tell how many results you have before you retrieve the row data using PDOStatement->rowCount().
Unfortunately, it is not possible to set the result set container for PDOStatement->fetchAll() to use SplFixedArray — however, if someone wants to help (that is, someone who knows internals and… well, C), I’ve got an opening for a coach!
At the urging of my co-worker Helgi, I threw the arrays into a FilterIterator and got some pretty interesting results. Using similar code to the first benchmark, but instead of just reading out the array, we created and used a custom FilterIterator:
For regular arrays, we must first create an iterator:
For the SplFixedArray, we passed it straight into the EvenFilterIterator, otherwise the code is the same.
Even with the extra overhead of creating the ArrayIterator, the SplFixedArray is only marginally (1%) faster till it reaches the 10000 elements mark, and then it starts to become marginally slower (again 1%). So, I guess the take-away is: use with caution.
Netbeans for PHP: Continues to Impress
It seems that I don’t blog much unless IDE’s are concerned; there is a good reason for this: IDEs are an integral part of my development process and when they suck, development sucks.
The story so far:
- Boy meets ZDE 2.5
- ZDE grows up to 5.5
- ZDE gets replaced by new eclipse-based ZSfE/PDT
- ZDE keeps going, until one day, Boy upgrades OSX
- Boy hacks OSX, but ZDE is running on a donut
- OSX update kills ZDE for good
- Boy cries
- Boy finds Netbeans
This is the continuation of that story. In the last installment Netbeans 6.7 was a nightly build, it had gotten it’s OSX look and feel, and it was starting to get it’s remote debugging up and running.
Now, 6.8 has been out for almost 2 months, and things are really starting to gather steam. With the death of ZDE5.5 finally a reality, and PHP 5.3 code starting to become part of my work-day, I finally jumped 100% to Netbeans.
And let me tell you, Netbeans 6.8 is nothing short of amazing. Debugging with xdebug is now almost as easy as ZDE, it works instantly on 90% of my remote machines, but I have 1 cluster for which Netbeans simply *cannot* find the local source file, making it impossible to debug.
Watches, breakpoints (though, I haven’t figured out conditional breakpoints, if they are there), callstack and local variables work as you would expect (though watches/variables sometimes refuse to populate larger vars, I think this is xdebug config related). In addition, Netbeans supports arbitrary breakpoint groupings; these can be enabled and disabled as a group — very neat.
In addition, it has path mapping to help with remote/local file correlation; so it can find the local file to show the source during debugging — this stops the problem ZDE has where two files have the same basename() and it’s unable to choose the correct one.
However, a fully functional debugger is a minimum requirement. Netbeans 6.8 also has great support for PHP 5.3 (though it has some syntax support bugs), again another minimum.
So where does Netbeans shine? The single biggest answer to that, is PHPUnit support. Netbeans lets you specify your test folder, and abstracts it out of the project, so your tests are separated visually; this is a great minor addition. In addition, Netbeans can generate unit tests (this utilizes phpunit’s built-in functionality), and has a great UI for running tests.
You can run a single unit test by simply right clicking on the test and choosing Run, or you can test a whole project by right clicking on the project and choosing Test. Doing this will bring up the Test Results pane:
As you can see, it shows the number of tests, the test suite, and it’s test status; this can then be expanded to show individual test methods.
Further to this, you can have Netbeans capture code coverage information, if you have the xdebug extension installed locally. This then manifests visually in two ways; the first, is a summary:
The second, more impressive/useful way, is visually within each file:
You will also notice that this adds a set of buttons below the code, which can be used to run the test for just the current file (based on the typical phpunit file/test naming structure, I assume) and to re-run the entire test suite.
To me, this integration is phenomenal, and is changing the way I work. This is a great example of an IDE conforming to your workflow, and proving new ways to do things; rather than fighting you and requiring you to change to it’s needs and ideals.
Other things of note, Netbeans 6.8 has Symfony project integration, and 6.9 is including Zend Framework integration, if those things appeal to you — I have yet to play with either, so can’t comment on their usefulness.
I can, without doubt, confidently say, that despite the few bugs, and some still immature minor things, Netbeans is my recommendation for an IDE.
- Davey
BAM! I just unlocked the White Hat cap on @mojolive! http://t.co/GWVUht8R
@dshafik [2 weeks ago]
@rdohms @weierophinney especially here in Florida.
@dshafik [2 weeks ago]
@Arkantoze yeah, but NOT RIGHT OUTSIDE THE FRIGGEN DOOR
@dshafik [2 weeks ago]
@dshafik cool looking spider web!
@Arkantoze [2 weeks ago]
@dshafik Very pretty… and remember, spiders eat other insects…
@weierophinney [2 weeks ago]


















