QuickSort: comparing Java to PHP

Standard

QuickSort uses a divide and conquer technique to organize the arrays contents. The algorithm runs in:

worst case: O(n2)
best case: O(n log n) (simple partition)
or O(n) (three-way partition and equal keys)
average: O(n log n)

Motivation:

In the debate of Java vs PHP it was asked why use java when many tasks require less code in PHP. The answer while complex boils down to performance. This is a tiny example to show a performance comparison for each language.

The Algorithms

As a additional angle two separate implementations of the quicksort in Java have been added. One will use arrayLists while the other integer array. Additionally, JDK 7 / 8 will ran separately to analyze platform performance.

The PHP implementation uses a straight integer array.

Each algorithm is fed in X number of random integers. For Java to assure accuracy 50 iterations on the algorithm are performed to determine the run time. PHP is configured for 10 iterations due to the memory limit of 2 gigabytes. While one could argue that the data is inconsistent due to the randomized values, by increasing the iterations it gives a more accurate result.

Results:

I have omitted the PHP 10,000,000 array size test as is such a high number it skews the graph.

Conclusion:

JDK 7 appears to have a slight edge when it comes to arrayLists, however the difference while worth nothing does not conclude performance improvements. Therefore, Java 7 / 8 performance does not differ. As expected PHP performance is considerably worse that Java. PHP is not designed to nest over 100 functions. This can be configured as a variable as described below.

To run your own tests checkout either the PHP or Java repository on Github.

Possible errors:

PHP Fatal error: Maximum function nesting level of ‘100’ reached, aborting!

This error means that you need to increase the allowed number of nested calls. This can be configured by editing the php.ini file.

Tech Specs
Java JDK 8 / 7
PHP 5.5 (2GB memory allocated)
AMD FX 8-core
16gb RAM 1600mhz

Binary search: comparing Java to PHP performance

Standard

Binary search finds the specified key in a given sorted array, then returns it’s position. The algorithm runs in:

worst case: O(log n)
best case: O(1)
average: O(log n)

Motivation:

In the debate of Java vs PHP it was asked why use java when many tasks require less code in PHP. The answer while complex boils down to performance. This is a tiny example to show a performance comparison for each language.

The Algorithms

As a additional angle I have added two separate implementations of the binary search in Java. One will use arrayLists while the other integer array. Additionally, JDK 7 / 8 will ran separately to analyze platform performance.

The PHP implementation uses a straight integer array.

Each algorithm fed in X number of presorted ints. For java to assure accuracy 50 iterations on the algorithm is performed to determine the run time. PHP is configured for 10 iterations due to the memory limit of 2 gigabytes.

Results:

Conclusion:

Java 7 / 8 performance does not differ. An arrayList in has slightly more overhead compared to a pure integer array. As expected PHP performance is considerable worse that Java. PHP is not designed to hold 10 million ints in memory. To make this test work, the memory allocated to php had to be adjusted to 2gb. In conclusion, leave the large data sets to integer arrays in Java, not PHP to have the best performance.

To run your own tests checkout either the PHP or Java repository on Github.

Tech Specs
Java JDK 8 / 7
PHP 5.5 (2GB memory allocated)
AMD FX 8-core
16gb RAM 1600mhz

SocketIO using RequireJS client application

Standard

In this article, we are going to use RequireJS and SocketIO to create a structure to allow for an interactive single page app. Socket.IO is used to help developers create interactive applications. For example SocketIO could be used to create, a chat system, real-time notifications, or a polling app.

To use RequireJS and SocketIO together I will include all my libraries via CDN’s to minimize the amount of code in my final project.

The main entry point is conveniently labeled “main.js” is the point at which I load all my external libraries. The final function of this file tells requireJS to continue to load my app.js file.

The app.js in this use case is use to load the router, the router is where all the magic happens.

In router.js I define all my libraries that I have included above. I give them names of my choosing “io” and “$”. I will use these in the initialize method to make a connection to the socket. To make this connection work enter you will need to enter your ip or domain of the socket you wish to connect to. The SOCKET variable is assigned below in the html portion of the code. This of course assumes that you have setup your socketIO server on the other end properly.

The final peice to run this application is to have a webpage to display the output. Using the CDN again I load requireJS and the main entry point file (main.js). If you change your SOCKET variable to a running socket server you should see the log message “I am connected” when the page loads.

A complete download of this project is available on github.

Hex to image using Imagick

Standard

Some software will encode and send images in hex format. This is not very useful for storing. The solution is to use php pack method to convert the data into a binary string. Then using Imagick you can easily read in the result of the pack and set compression, image format, and dimensions before rendering.

Semaphores to avoid critical task collision

Standard

This is a sample from my cakephp shell script that has some critical processing. It is ran every second and sometimes the script can take over a second to process. Thus, I needed a way to avoid the script from accessing the same critical business logic and manipulating data.

The semaphore works by acquiring the key with the given id: 1068. I have set the max number of semaphores to 1 for the key. All other shell scripts that are ran will autorelease when they cannot acquire the semaphore (hence another shell script is still processing).

Forking a running process and returning values

Standard

Using pcntl_fork() allows for forking of a running processing in PHP. In this example I fork my shell script and process data and return the results. This would be useful in a case where you had to make api calls to lets say 4 vendors: amazon, itunes, youtube, flickr. You would assign each child id a task to run. They each would then write there results to a shared memory location for further manipulation. I have written this test in CakePHP shell script.

I would not recommend running this script inside of CakePHP. It may cause issues with the caching.

Couchbase Datasource

Standard

I ran into a problem where I needed to utilize a NoSQL database. Since CakePHP is more fit for relational style database (MYSQL) I had to create a custom datasource (link). The following datasource allows models to have basic functionality of Couchbase Server.

Example Usage:

Setup / Configuration:

To use the couchbaseSource you must specify it in your model. Do so by adding the following.
YourModel.php

You can also programmatically change datasouces / database on the fly. For example, sometimes I want to use my default MYSQL datasource. This can be accomplished like so:

To use be able to use this datasource it must be added to cakePHP list of available datasources.
Database.php

app/Model/Datasource/CouchbaseSource.php

PHP 5.3 Introduction to Anonymous Functions

Standard

New to PHP 5.3 is the introduction of anonymous function. Anonymous functions also known as closures are methods that do not require a specified name to be assign to a function.

Prior to 5.3

Now using anonymous function

As you can see we are accomplishing the same tasks, however the anonymous function method do become useful if you wish to write them for definitions of other methods.

Take for example that we want to filter out all values that do not equal 3 or 4.

The result is as expected.

Memcached optimization

Standard

Swapping out CakePHP’s default file logging for memcache can greatly improve performance. In my case I use couchbase server to manage NoSQL. From there administrative panel you can see every time your app has a page refresh.

Couchbase Server default bucket

Memcached optimization with CakePHP 2.x

To connect CakePHP with memcached place this in your app/Config/core.php

Your application will now benefit from using memcache instead of a standard logging system. In the event that you make major changes to you application you may need to “flush” the memcache bucket to clear the cached schema stored by CakePHP.

Wildcard Subdomain Prerouting

Standard

Ever have a need to have wildcard subdomains on your CakePHP application? This is the solution to that problem. Assume all subdomains have there own template folder, and the subdomains need to be able to be created and modified on the fly.

Create a subdomain model, as well as a table in your database called Subdomains with the following schema

Initially, the URL is used to extract the subdomain and the host name from the clients entry point. The contents are then assigned to an array in a custom variable using the internal cache. The bootstrap is called within the dispatcher stage of the MVC architecture as shown below.

MVC Structure

CakePHP MVC Structure

Bootstrap.php

The subdomain is now passed through the application and can be accessed by configure::read. A custom library called SubdomainRoute writes values to the cache and the router then reads the results. If a template is set after the SubdomainRoute is initialized it assumes the client is accessing our application on a valid subdomain found in the database.

Routes.php

The subdomain is determined by accessing the mysql subdomain table. The results are stored and later used for routing (above), as well as assigning a template folder for a particular subdomain (below).

app/Lib/Routes/SubdomainRoute.php

The final step is to tell cake which template the subdomain is associated too. This assumes that you have created the templates folder in the directory.

AppController.php