Why You Should Use C With Php

A few months ago, I calculated the top Magic: The Gathering cards in the games modern format ... It took 30 minutes in PHP. How long would it take in C?
2 Minutes
Published: January 7, 2014

A few months ago, I calculated the top Magic: The Gathering cards in the games modern format [LINK_BROKEN]. This was accomplished using pure PHP. In total it took about 30 minutes to calculate. With a 30 minute runtime, I wanted to find a faster way to crunch the numbers. What better way than to use C.

As I did not want to spend the time rewriting code if it did not make a difference, I thought I would use a simple application for this experiment: Calculating the nth term of the fibonacci sequence.


To start, let’s look at the big picture. the graph below shows the runtime in micro seconds of executing pure PHP & PHP using C from term 0 to 100,000 tested every 1000 terms.

fibonacci-100000.png [Image Missing]

As can been seen, there is a huge difference in runtime between the 2 applications. With lots of calculations, using pure PHP does not stand a chance against its counterpart of using C to do the heavy lifting.

Although there is a huge difference when calculating a large term, let’s drill down a little deeper. This next graph shows the runtime of the nth term being calculated from 0 to 1000 tested every term.

fibonacci-1000.png [Image Missing]

It looks like there is a grace period where pure PHP out performs PHP using C. When calculating the terms 0 to 728 using pure PHP is faster. But, you are only saving, at most, 6000 microseconds (0.006 Seconds).


Although using C to do the heavy lifting of calculations is overall faster, it may be overkill for some situations. But when it comes down to brass tacks, I am going to use C.


Source Code: kernelcurry/fibonacci-php-c

Computer: All tests were completed on a Macbook Air.

Data Collected: All data was collected using xhprof

Data Points: Tests were run 4 times at each data point.

UPDATE [06-23-2017]: Most of the link and images on this page are broken. Please stay tuned for a follow-up blog post on this topic.