Optimising php-faker to be 10x faster!
In a link trail following the sequence of helper callbacks debacle yesterday, I stumbled across an interesting php library for generating random data - php-faker, which is a port of Perl's Data::Faker.
At first glance it looked pretty handy, but after testing it a few times I noticed it would repeat the same "random" data in a sequence, over and over again. I think it must have been a problem with my wamp installation, however it made me peruse the code to discover an explanation.
What I immediately noticed was the inefficiency of the class over many iterations. Changing a few simple things enabled the code to execute 10x faster in my test environment.
What I changed
- Moved arrays from inside methods to static variables. This gave a massive speed boost, especially for the larger datasets.
- Cached instances of the classes instead of creating a new object for each request.
- Optimised the core methods random(), rand_num(), rand_letter() to use mt_rand() and simplified the code.
- Replaced unneccessary create_function() call with a more efficient alternative.
There is still a fair bit that could be optimised as I left the actual logic intact, but the above is simple, simple stuff.
Download my version
I probably should have installed git and created a patch, however forking the project was easier for me since I could edit the files online (thanks github!). Caius has already integrated my changes into his project, so happy days, just get it from him!
Git repository: http://github.com/caius/php-faker/tree/master