All posts by wimg

Finally, the rush begins…

Somewhere in december, I promised to publish articles and other interesting things on a regular basis. So far, not much of that has happened.

Not that I was short of ideas, but due to time-contraints, I never got around to doing that.

In the past few weeks, I finally found the time to put some of those ideas into practice. So starting this week, you’ll regularly see some articles and smaller tidbits posted here.

A small preview of what’s the come :

  • An extension to PHPUnit, which allows you to verify the types of variables on several levels during the test run
  • How to build a dynamic, cacheable, reflection-based ACL system in Zend Framework
  • Parents are bad (or : how to build a proper tree)
  • How to analyze SQL queries in MySQL – how to get the most out of your database by writing the right queries
  • How to detect and block request floods
  • and a lot more 😉

For those of you who want to know when a new article is added, you can subscribe to the feed or follow me on Twitter (@wimgtr)

Getting uniques from an array (with speed in mind)

Important : the following technique doesn’t work for any arrays where the values are :

  • boolean (true/false)
  • null
  • objects
  • resources

Suppose you get data from some source (an XML file, a CSV file, …) and you put it into an array. Now suppose this data is full of duplicates. For example, you have :

array(
0 => 'horse',
1 => 'pig',
2 => 'pig',
3 => 'cow',
4 => 'horse',
...

How can you get the unique values from this array ?

The standard way would be to do :

$a = array_uniques($a);

Works fine, except that it’s extremely slow for large arrays.

A better way would be :

$a = array_keys(array_flip($a));

But marginally faster is :

$a = array_flip(array_flip($a));

So how big is the speed difference ? For large arrays, a double array_flip can easily be 20 times faster.

For reference, here’s a small benchmark :

$a = array();
for ($x=0; $x < 1000000; $x++) { $a[] = rand(0,1000); } $starttime = microtime(true); $b = array_unique($a); echo (microtime(true) - $starttime) . "\n"; $starttime = microtime(true); $b = array_keys(array_flip($a)); echo (microtime(true) - $starttime) . "\n"; $starttime = microtime(true); $b = array_flip(array_flip($a)); echo (microtime(true) - $starttime) . "\n";

The result :

2.06489086151
0.101167201996
0.0999970436096

Autoloading in Zend Framework – how (not) to do it

Over the past 2 years I’ve used Zend Framework for several projects and played with the autoload functionality of Zend_Loader, as well as using standard include statements.

Ever since Zend released its first version of Zend Framework, discussions have been going on about what the best way to autoload files/classes is. Some people even claim autoloading is simply a bad idea and we should use include/include_once/require/require_once (another never ending debate there).
Continue reading Autoloading in Zend Framework – how (not) to do it

Just another blog ?

So I finally got around to writing a professional blog. I’ve been planning on starting one for a long time, but never got around to actually doing it, mostly due to time constraints.

This blog serves just one purpose : to publish the ideas that sometimes pop into my head, often while working on a technical solution, sometimes while thinking about a business idea or just while browsing the Web.

It will be a collection of long, technical or business oriented articles, often mixed with short and succinct items… mostly thoughts about IT, business or anything else that I may find interesting.

My main goal is for this blog to serve as a historical record for myself… if by any chance someone else enjoys reading it, it’ll have served more than its purpose !