PHP vs ColdFusion – a test against time
Posted on January 29, 2011 by Phil 56 comments
This article has been edited based on the feedback given in the comments section by a large number of Coldfusion professionals–mainly on the use of cftimer effecting the results. My original text stays in place with amendments made based on alterations needed to make the tests a fairer and less PHP bias comparison
Where I work we use ColdFusion but for personal projects I always code in PHP, mainly because ColdFusion is pricey (although Railo and BlueDragon are freely available) and I don’t like coding in it. We were talking about performance at work the other week and I boasted that PHP was much faster than ColdFusion without any real evidence to back it up, just stuff I had read about how badly ColdFusion deals with instantiating objects. Might I add, for a scripting language built on Java, a truly OO language, this is totally lame. I thought I would create a few simple tests to show people just how much better PHP is as a language a comparison between the two languages for which the results are outlined in this article.
The test environment
- OS: Windows 7 x64
- CPU: AMD Phenom II X2 550 3.10 GHz @ 3.83 GHz
- MEMORY: 4GB OCZ DDR3
- ColdFusion: 9 x64 EDIT: with all debugging features disabled
- PHP: 5.3.0
- Apache: 2.2.11
Test one: Speed test on looping
This test involves two identical objects that simply calculate and build an array of the prime numbers between 2 and 10,000 using a simple for loop containing another nested for loop. Below is the code for each (unfortunately ColdFusion is so popular that there is no syntax highlighter for it isn’t supported by syntax highlighter so I had to use the java colouring).
The PHP Primes object
class Primes {
public function __construct () {}
public function calc_primes ($to) {
$primes = array(2,3);
$primes_len = 2;
for ($i = 5; $i <= $to; ++$i) {
$is_prime = true;
for ($j = 0; $j < $primes_len && $is_prime; ++$j) {
if ($i % $primes[$j] == 0) $is_prime = false;
}
if ($is_prime) {
array_push($primes, $i);
++$primes_len;
}
}
}
}
The ColdFusion Primes object
Original code sample available in download
component {
public function calcPrimes (to) {
var primes = [2,3];
var primesLen = 2;
var isPrime = true;
var i = 5;
var j = 1;
for ( ; i <= to; i++) {
isPrime = true;
for (j = 1; j <= primesLen && isPrime; j++) {
if (i % primes[j] == 0) isPrime = false;
}
if (isPrime) {
ArrayAppend(primes, i);
++primesLen;
}
}
}
}
Looping test results
I wasn’t sure whether PHP was up to the task on this one as the Java layer below ColdFusion is pretty good with handling large Arrays but PHP came out just on top. Coldfusion came out slightly better off on the re-run tests
PHP: 128ms, 122ms, 126ms 108ms average
ColdFusion: 128ms, 115ms, 135ms 84ms average
One tip to note here for PHP programmerscount() is exponentially slow on large Arrays. Using the below variation on the calc_primes method actually took 283ms to 10,000, 908ms to 20,000, 1818ms to 30,000 and 4522 to 50,000!!! ColdFusion, using arrayLen, managed it in 1929ms to 50,000.
public function calc_primes_count ($to) {
$primes = array(2,3);
for ($i = 5; $i <= $to; ++$i) {
$is_prime = true;
for ($j = 0; $j < count($primes) && $is_prime; ++$j) {
if ($i % $primes[$j] == 0) $is_prime = false;
}
if ($is_prime) array_push($primes, $i);
}
}
Test two: Object creation
In this test I created (around the idea of a bee hive) a Colony object to which we add 10,000 Drone objects through running a while loop with the condition as a method call to the Colony to get it’s Drone count. Again code for each below.
The PHP Colony and Drone objects with test loop
class Colony {
private
$drones = array(),
$colony_name,
$drone_count = 0;
public function __construct ($cn) {
$this->colony_name = $cn;
}
public function add_drone ($d) {
array_push($this->drones, $d);
++$this->drone_count;
}
public function get_drone_count () {
return $this->drone_count;
}
}
class Drone {
private $message;
public function __construct ($m) {
$this->message = $m;
}
public function get_message () {
return $this->message;
}
public function set_message ($val) {
$this->message = $val;
}
}
// Test Object creation with while
$times = array();
for ($i = 0; $i < 100; ++$i) {
$start = microtime(true);
$colony = new Colony ('PHP Object Colony');
while ($colony->get_drone_count() < 10000) {
$colony->add_drone(new Drone ("I am drone " . $colony->get_drone_count()));
}
array_push($times, microtime(true) - $start);
}
echo '<p>Object creation test took an average time of ' . array_sum($times)/count($times). ' to run over 100 iterations</p>';
The ColdFusion Colony and Drone objects with test while loop
Original code sample available in download
//Colony
component {
public function init (cn) {
drones = arrayNew(1);
droneCount = 0;
colonyName = cn;
return this;
}
public function addDrone (drone) {
arrayAppend(drones, drone);
}
public function getDroneCount () {
return arrayLen(drones);
}
}
//Drone
cfcomponent {
public function init (m) {
message = m;
return this;
}
public function getMessage () {
return message;
}
public function setMessage (m) {
message = val;
}
}
// Test Object creation with while
times = [];
for (i = 0; i < 1; ++i) {
start = getTickCount();
colony = new Colony9("ColdFusion object Colony");
while (colony.getDroneCount() < 10000)
colony.addDrone(new Drone9("I am drone" & colony.getDroneCount()));
arrayAppend(times, getTickcount() - start);
}
writeOutput("<p>Object creation test took an average time of " & arrayAvg(times) & "ms to run over 100 iterations</p>");
Object creation test results
I had a good idea about how this test would turn out and I wasn’t disappointed. ColdFusion is ridiculously slow at creating objects. PHP could actually create 500,000 objects quicker than ColdFusion could do the 10,000. Shame on you ColdFusion, shame on you.
PHP: 42ms, 42ms, 42ms 50ms average
ColdFusion: 7924ms, 7524ms, 7339ms – Oh Dear!! 844ms, 569ms, 647ms – see comments! 337ms average
Test three: Output buffer speed
For this test I simply took a 140,000 character long string of Lorem Ipsum text and got both PHP and ColdFusion to write it to a variable from the output buffer. Again code for each below but with the string omitted.
The PHP output buffer code
//Test output buffer speed
$txt = file_get_contents("string.txt");
$times = array();
for ($i = 0; $i < 100; ++$i) {
$start = microtime(true);
ob_start();
echo $txt;
$out = ob_get_clean();
array_push($times, microtime(true) - $start);
}
echo '<p>Output buffer test took an average time of ' . array_sum($times)/count($times). ' to run over 100 iterations</p>';
The ColdFusion output buffer code
Original code sample available in download
<cfset txt = fileRead(expandPath("string.txt"))>
<cfset times = []>
<cfloop from=1 to=100 index="i">
<cfset start = getTickCount()>
<cfsavecontent variable="out">
<cfoutput>#txt#</cfoutput>
</cfsavecontent>
<cfset arrayAppend(times, getTickCount() - start)>
</cfloop>
<p>Output buffer test took an average time of <cfoutput>#arrayAvg(times)#</cfoutput>ms to run over 100 iterations</p>
Output buffer test results
140,000 characters isn’t too massive a string to deal with but PHP still came out way on top by a pretty sizeable margin.
PHP: 0.2ms, 0.1ms, 0.2ms 0.14ms
ColdFusion: 2ms, 2ms, 2ms 1.39ms
Rounding up
So it’s pretty obvious that if you are looking for performance then stay away from ColdFusion, especially if you are planning on building object oriented applications ColdFusion obviously has issues with performance in dealing with objects which should be considered if using the language in applications where the use of coarse and fine grained objects are needed. On top of that, I might add that ColdFusion fails to offer certain object oriented features such as class level methods and attributes which is so I find annoying . If however you have money to burn and want to get an application that talks to If you are building an enterprise level application that, for instance, needs to interact with Microsoft Exchange built in a day then maybe ColdFusion is for you… Maybe?
I’d be interested to see results of these tests on other systems and server configurations so if you have the time download the files and let us now the results in the comments. I will run these tests on my Linux box later and on a Windows Server box in work on Monday and post the results.
Related Posts
No related posts.
56 Responses So Far
-
-
Pingback: Tweets that mention PHP vs ColdFusion – a test against time | Phil Parsons -- Topsy.com
-
Justin Carter January 29, 2011 at 12:50 pm
Phil, it looks like you’re running your code with request debugging turned on. Debug mode affects performance so you can’t really run benchmarks with it turned on as it will skew your results. The whole point of debugging is that ColdFusion will log extra information about all your code that is being executed so of course it will be inherently slower. If you turn it off the object creation should be much faster – it takes around 560ms on my system with your code.
The other tests (up to 10,000 primes and the long string) take 95ms and 0ms respectively on my system (a modest Core i5).
-
Mike Henke January 29, 2011 at 2:48 pm
Can you run the same tests on railo?
Also for ur tests on ACF were you using the built in jrun/web server? You may try to run it on tomcat/apache to be more fair. You maybe be seeing a difference between web and application server differences.
Thanks
-
Brian Kotek January 29, 2011 at 3:35 pm
I’m also not sure if you’re just deliberately trying to show the ugliest version of the CF code that you can? Because this:
arrayAppend(drones, drone);
++droneCount;Could just be written as:
public function addDrone( drone )
{
arrayAppend( drones, drone );
++droneCount;
} -
Brian Kotek January 29, 2011 at 3:36 pm
Whoops, make that:
arrayAppend(drones, drone);
++droneCount;Could be written as:
public function addDrone( drone )
{
arrayAppend( drones, drone );
++droneCount;
}
-
Mike Henke January 29, 2011 at 4:13 pm
Phil, Both on apache for the web server but what is the application server you are using for ACF? I’d guess it is jrun which I have always thought is slow unless optimized.
Here is the railo download. It should be easy to install and run your tests against. http://www.getrailo.org/index.cfm/download
To be fair, you should strikeout your comments about “money to burn” and/or specifically say Adobe ColdFusion and mention Railo and OpenBD are free.
Also for a coldfusion syntax highlighter, it might be your choice of syntax highlighter is lame. I know http://pygments.org/ has CF which is used by github.
-
Aaron West January 29, 2011 at 4:31 pm
Phil, this is an interesting post; thanks for taking the time to put it together. I enjoyed seeing the comparisons, but there are places where your comparisons could be better / more direct. For instance, to test the output buffer speed you use the cftimer and cfsavecontent tags in ColdFusion. There’s nothing inherently wrong with cftimer, but since you’re testing speed, your code would be better with two GetTickCount() calls for start and end time, and a simple statement. The cfsavecontent stuff is unnecessary if you’re just setting a string.
All-in-all, comparisons like this are always interesting regardless of what technologies or languages are included. But I’ve never read one that appeared impartial as people tend to write better code in the languages they prefer. Whether that is intentional or not, I don’t know. Cheers.
-
Mike Brunt January 29, 2011 at 4:47 pm
Phil, why would you make the statement “but yes it is an ugly language” that pretty much casts doubt on your impartiality here. All programming languages could be called “ugly” in some of their constructs and to me this shows that all you wanted to do with this post, is to find a way to broadcast your own bias. You did that in a manner that appears to be technically objective and that could be misleading. I have spent 11 years on server-side engineering, focussed more on Java and I can state categorically that ColdFusion can be incredibly performant.
-
Brian Kotek January 29, 2011 at 4:48 pm
Not a valid excuse, Phil. It’s disingenuous to not show the most optimal version of the code.
Now you just need to show the CF and PHP versions of generating graphs, PDFs, full-text indexing, and pulling a user’s Exchange calendar. Or really, any of the huge number of things that CF can do in one line of code.
CF might actually be slower than PHP when executing certain code. But Adobe’s focus with CF is not on pure speed. No one is ever going to notice the difference between .2 ms and 2 ms. VERY few people are generating 140,000 character strings or need to generate 10,000 object instances in a single request.
Adobe’s focus is on making DEVELOPMENT faster. So instead of trying to wring some extra ms out of object instantiation, they added the ability to read and write binary Excel files and other Office types, interact with Exchange and Sharepoint, generate JSON, SOAP, XML, and AMF data without changing any code, integrate directly with JSR 168 and JSR 286 portlet engines, and a lot more. And that’s all stuff that is exponentially more difficult and time consuming to do in PHP.
In short, while PHP may execute code in a shorter time, CF allows developers to build entire applications in a shorter time. Which adds up to a huge ROI when it shaves weeks or months off the development time of a large application.
-
Dave Watts January 29, 2011 at 5:20 pm
The problem with tests like these is that what they test is artificial. Web application performance testing needs to be parallel, not serial. Unless you have a single script creating thousands of objects, testing the performance of a single script creating thousands of objects tells you … how well a single script can create thousands of objects. It doesn’t tell you how well object creation actually works in an application.
You want to compare two things? Coding technique X vs coding technique Y, language X vs language Y, etc – it doesn’t matter. To get useful, meaningful values, you need to test real uses in parallel. This means using load testing tools, not writing a single script and running it.
So, if you want a meaningful test, take your CF application and rewrite it in PHP or vice-versa, then load test both of them. I’ve done load testing for many years, and in many, many cases, performance problems don’t show up when you simply run one script – that script might run very well when run serially, but not in parallel.
-
Dave Ferguson January 29, 2011 at 5:27 pm
Phil, You bring up some interesting points here. However, I don’t put much weight into tests like these. This type of test is usually done to prove someones point. They are rarely objective and don’t even test real work scenarios.
I have to question the validity of what you are stating. As others have posted, there are better and more efficient ways to write the sample ColdFusion code.
You stated that you believed that PHP was faster and I think you set out to prove that. Also, when you make a comment like this: “I didn’t make ColdFusion ugly Brian, but yes it is an ugly language.” it is hard not see the bias.
–Dave
-
Jason Dean January 29, 2011 at 6:27 pm
You said
“I don’t expect anyone to choose either language based on pointless tests”
but then in your post you made such ignorant and invalid claims like
“So it’s pretty obvious that if you are looking for performance then stay away from ColdFusion, especially if you are planning on building object oriented applications. ”
You have more than a little bias against ColdFusion, and you went into your experiment with a preconceived notion and a predetermined result. Your goal was to prove PHP was faster, that is an unscientific hypothesis and makes your results invalid.
Hell, for all we know, you cooked the books to prove your invalid hypothesis.
A valid experiment would have an unbiased hypothesis and would have involved research on how to write a performant ColdFusion application. You clearly don’t know anything about ColdFusion performance.
Your tests are invalid, and the unfortunate part is, unlike a good scientist, you are too proud (or lazy) to correct them, so the unfortunate reader who puts stock in your post without reading the correcting comments will be left with the mistake impression that they cannot write performant applications in ColdFusion.
Your hypothesis is flawed, your methods are flawed, your results are flawed, and your resulting theory is flawed. Some of the worst “testing” I have every seen.
-
Grant Shepert January 29, 2011 at 6:58 pm
As a matter of interest, I ran your object creation test on my lowly little 2-year old MacBook Pro with Apache/Railo, and the times were: 178ms, 184ms, 222ms. It seems these gaps of yours are become incrementally smaller, aren’t they?
That said, as has been mentioned numerous time in these comments already the value of this is rather dubious. CF developers use these comparisons in Adobe ColdFusion vs Railo shootouts, and even here the results are tainted by bias and rapidly lose value in the real world.
RAD, coding efficiency, operating costs, support, frameworks, documentation, community, training and resources are just a few of the real world benchmarks. Since both languages have passed (and maintained) critical mass in these areas long ago (using quality as the measure), short of hiring Forrester all you are left with is “he who holds the bias wins” comparisons.
Since the performance difference is vanishingly small given real world application, the only way to pay respect to a subject like this is to demonstrate I could create quality, real world code faster and more profitably in PHP than I could in CF. I’m very confident that post isn’t going to come any time soon.
-
Scott Stroz January 29, 2011 at 8:16 pm
‘…I set out to prove PHP was faster, stated that and that was the theme of this article.’
That pretty much says it all. You were determined to prove PHP was ‘faster’ and I have to imagine you wrote code to make sure that would be the result.
You went into this ‘comparison’ knowing how you wanted it to turn out, so from the start, your methods are suspect.
-
Jared Rypka-Hauer January 29, 2011 at 10:20 pm
I wonder how your examples would do run on PHP as a Java ScriptEngine under ColdFusion?
-
Jared Rypka-Hauer January 29, 2011 at 10:35 pm
Guilty as charged. We pay attention, we communicate with each other and we know what we’re talking about. So yeah, a bunch of us are ACPs. So what? If PHP had such a thing I’m sure they’d be all over the posts that cut on PHP. And PHP would probably look like a more professional platform as a result.
When you try to spread lies (or, at best, misleading half-truths), you get called on it. Especially by people who are experts in their field. The misleading who-hah in question here is:
So it’s pretty obvious that if you are looking for performance then stay away from ColdFusion, especially if you are planning on building object oriented applications. On top of that, I might add that ColdFusion fails to offer certain object oriented features such as class level methods and attributes which is so annoying . If however you have money to burn and want to get an application that talks to Microsoft Exchange built in a day then maybe ColdFusion is for you… Maybe?
I mean, if you really want a good example of real-world, production-class performance, turn on CF’s component cache and template cache (does PHP have those?) and then pound the PHP code and the CF code with 500 successive hits from 30,000 simultaneous users using some sort of load test tool. Get your average response times that way.
Then maybe we’ll have something actually interesting to discuss.
-
Aaron West January 29, 2011 at 10:48 pm
Phil, my comments weren’t trying to stir up trouble and I was genuinely interested in your post. But you are correct that I’m an Adobe Community Professional so I’m not a big fan of seeing ColdFusion cast in an incorrect light.
I’d be perfectly okay with your post if you discussed something wrong with ColdFusion and you used the best possible code (speaking specifically about the cftimer, cfsavecontent section) to demonstrate the flaw. I’d take your results from a post like that and put them in front of the Adobe CF team to try and affect positive change. Honestly, I would.
But it seems like so many people want to start their writing with the conclusion and then work toward it. That might work for a novelist, but not a technical author. I suppose it’s your site/blog and you can do and write what you want. But a blog post like this one isn’t helpful to people who want an honest evaluation of ColdFusion. And I’m afraid that’s how the post comes across (to me at least).
That’s all I’m after, is to try and encourage people to be more objective in a post that is intimated as such. And if they can’t be objective, then it might be a good idea to simply not write the post or to be clear at the beginning that you aren’t objective. Cheers.
-
Scott Stroz January 29, 2011 at 11:20 pm
I don’t see anyone ‘flaming’ you. Merely pointing out that your ‘research’ may be flawed.
-
Jared Rypka-Hauer January 30, 2011 at 3:32 am
Honestly, Phil, if you’re going to present quibbles as an argument as to why PHP is superior to ColdFusion… here’s a counter-quibble:
A variation on the Prime Number example.
And before you scoff… the results might surprise you.
Nah, not really. But if you’re really being fair about this you’ll let the comment through anyway.
-
Jared Rypka-Hauer January 30, 2011 at 9:23 am
Just for the sake of consistency, I’ll post the URLs to my other 2 counter-posts here:
And there’s the full set.
-
Steve Bryant January 30, 2011 at 5:10 pm
Like many others that have commented here, I am a die hard ColdFusion fan. Unlike most comments, however, I have to commend your post.
You have a bias, but you stated it openly. Most of us are biased in some way. I would rather read something where the bias is known than have someone hide their bias in an attempt to seem fair. Your approach allows the reader to better decide how much weight to give to your tests.
You have responded well to the criticism that has been levied (fairly, I think) against your test and updated your results accordingly. Moreover, I like that you allow others to download and run your tests.
Even if your original results had withstood criticism, that wouldn’t have greatly concerned me – for mostly the reasons Dave Watts gave about the lack of significance of these kinds of performance tests (though, I think many of us would prefer if ColdFusion were faster at object instantiation).
In any event, it was a fun experiment to see and I am glad that you were open about your bias and responsive to criticism.
-
Andy K January 30, 2011 at 6:56 pm
+1 Steve Bryant.
I think a friendly coding contest would be kind of interesting! Come up with a dozen mutually agreed upon common real world scenarios and allow for the differing communities to offer their best/cleanest/performant/etc. code and see how they compare in a vacuum and under load.
If nothing else, it would educational for all to see how other people/languages/communities come up with different methods to achieve the same end result.
-
Steve Bryant January 31, 2011 at 6:15 pm
Andy K,
I have thought about this from time to time. I think weighting different parameters would be significant.
For example, I think performance should be given relatively low weight. In fact, I think it should be pass/fail. So long as an application is able to achieve the desired performance at the desired load then it is fast enough.
Clean code is, of course, a bit of a matter of opinion.
The real test would be to give a team a set of requirements and measure man-hours to completion. Follow that up with another set of requirements and repeat. Follow that up with giving *another* team the same app with new requirements.
It would have to be done in person, however, as otherwise it would be too easy to fudge the man-hours.
Interesting, but – I suspect – not worth the trouble. I would love the challenge though! I think ColdFusion would rock that kind of contest!
-
-
Shazzad Hoshien February 9, 2011 at 1:33 am
owo!! Awesome helpful & fantastic article. Thanks for sharing…..:)
-
Pal June 15, 2011 at 8:03 pm
I have programmed in both and I can find examples in each direction whereby I can make create an example where one is faster than the other. If you only need as a development language is to parse large strings and arrays then PHP wins. If on the other hand you need to perform filtering against a live dataset CF would make PHP look like it is standing still. Useless comparisons in most cases unless you need those things specifically.
For me the elegance of CFML and the CF platform are worth it to me to have. I still get paid to program in PHP but I will not use PHP on my personal projects as I prefer to have more time for fun and family rather than love of coding. That’s again just an opinion.





Thank you for summing this up. On a first glance this looks like a well prepared and good documented benchmark, but I regret having to say, that with ColdFusion debugging enabled, this post about the benchmark is nice to read, but of no real value. Enabling debugging on ColdFusion affects performance significantly. It is advised that you do not enable debugging on a production server and the same is true, if you try to compare application servers on a fair basis.
Re your “pricey” statement above: Meanwhile with Railo (www.getrailo.org) and Open BlueDragon there are two very good Open Source CFML Application Servers.
Anyway, thank you very much your post and your efforts.
Andreas