PHP vs .NET performance #1

I've spent a great many years developing quite large scale websites using Microsoft ASP and ASP.NET based content management systems. In the last couple of years I've also built some sites with PHP based CMSs.

Like many people, I've been curious about the relative performance of PHP vs .NET but never seemed able to find any conclusive benchmarks, since much of the information is distorted by the religious fervour of those programmers strongly in the PHP or .NET camps.

For what it's worth, I very much like the idea of open source software but I would much rather know (and speak) the truth regardless if that conflicts with my personal interests.

The benchmark
My benchmark is very simple, measure how long it takes a page running in both C#.NET and PHP to count up to a reasonably large number (in this case 10,000,000) and output the time taken.

Both tests were run on the same machine, an Ubuntu / Vista dual-booted Intel quad core with 8GB RAM, though the hardware spec is irrelevant since it's identical for both tests.

The code

<h1>C# Performance Test</h1>
int x = 0;
DateTime start = DateTime.Now;
for (int i=0; i < 10000000; i++)
x = x + 1;
DateTime end  = DateTime.Now;
TimeSpan total_time = end - start;
Context.Response.Write("Total Time="+total_time.Milliseconds);

<h1>PHP Performance Test</h1>
$start = microtime(true);
for ($i = 0; $i < 10000000; $i++) {
$x = $x + 1;
$end = microtime(true);
$total_time = $end-$start;
print "Total time=".$total_time.";


I must say, I'm shocked and saddened by the result. Yes, real world server performance would be very different, but nonetheless...

PHP: 2450 milliseonds

.NET: 45 milliseconds

Yes, you read that correctly, .NET is 55 times faster than PHP in my simple benchmark !

(Please read the important update below)


.NET is compiled and PHP is interpreted so it was "obvious" that .NET would be faster, right? The reality is not quite so simple (or so I thought). .NET is actually compiled into "byte-code" to be executed by a run-time engine and PHP is parsed into "op-code" to also be executed by a run-time engine. There are differences in these two approaches, but they aren't so very different. .NET is *not* executed as native object code. Also in PHP much of the actual work will be done inside functions written in compiled C code called by the run-time engine.

I was still expecting .NET to be faster but not to such a large extent.

My benchmark is extremely simplistic and doesn't much correspond to a real world website where many other factors such as database speed, memory usage, caching, pipe-lining efficiency etc will all come into play. However, as an approximate measure of raw computing speed I think it's a pretty reasonable test. As a measure of overall server performance, it really won't be that simple.

The vast majority of even quite high profile websites receive relatively modest traffic which can be handled comfortably by a comparatively modest server. For most websites, development time & costs are more relevant than raw speed.

Scaling-out to handle high traffic loads is generally cheaper with PHP (ie open source)  server stacks due to the lack of licence fees. Getting to the point where you need to scale without having had to pay huge licence fees is big plus too.

Nonetheless, whichever way I slice it, .NET is 55 times faster than PHP. That is something worth thinking about !

Faster Still

Worth noting that similar tests that I've run indicate that C++ is 6 times faster again than .NET

Not very many people advocate developing websites in C++. Speed really isn't everything !

I'm going to run some more benchmarks and I'll post the results.

Important update

I've done a bit more testing which has turned up some interesting (and quite heart warming) results.

I've tweaked the benchmark program so that rather than simply incrementing an integer, it performs a string concatenation.

eg changed x = x + 1 to x = x + "x" (and $x = $x."x")

To non-programmers the above statements might look identical but they are very, very different in terms of the amount of work the cpu has to do. The first statement uses the same few bytes of memory over and over again, the second rapidly consumes megabytes of memory. I also had to reduce the number of iterations to 100,000 to prevent script time outs in both languages.

PHP handled the new benchmark in only 648 milliseconds.
.NET claimed to be taking anywhere between 184 milliseconds and 999 milliseconds, yet by counting slowly in my head, I could see that it was consistently taking longer than 5000 milliseconds. Turns out that .NET has known issues with DateTime.Now accurracy when it's under load !!!!

In both cases as the number of interations increased, the benchmark time increased exponentially which is what you would expect given how most languages handle string concatenation. (Some people might argue I should be using a .NET stringbuilder, but that would be missing the point of this benchmark)

The new benchmark still falls short of representing a real world web application but this second example is closer as web apps are all about concatenating many strings of html together.

There is nothing else for it, I'm going to have to set up a proper benchmark and test a few real world CMS in a realistic scenario. (I've been meaning to do this for ages). But for now, I have to sleep!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
To prevent automated spam submissions leave this field empty.