Speed of code execution: ASP.NET-MVC versus PHP

Phpasp.net MvcArguments

Php Problem Overview


I have a friendly argument going on with a co-worker about this, and my personal opinion is that a ASP.NET-MVC compiled web application would run more efficiently/faster than the same project that would be written in PHP. My friend disagrees.

Unfortunately I do not have any solid data that I can use to back up my argument. (neither does he)

To this, I tried to Google for answers to try and find evidence to prove him wrong but most of the time the debate turned into which platform it is better to develop on, cost, security features, etc... For the sake of this argument I really don't care about any of that.

I would like to know what stack overflow community thinks about the raw speed/efficency of websites in general that are developed in ASP.NET with MVC versus exactly the same website developed with PHP?

Does anyone have any practical examples in real-world scenarios comparing the performance of the two technologies?

(I realize for some of you this may very well be an irrelevant and maybe stupid argument, but it is an argument, and I would still like to hear the answers of the fine people here at S.O.)

Php Solutions


Solution 1 - Php

It's a hard comparison to make because differences in the respective stacks mean you end up doing the same thing differently and if you do them the same for the purpose of comparison it's not a very realistic test.

PHP, which I like, is in its most basic form loaded with every request, interpreted and then discarded. It is very much like CGI in this respect (which is no surprise considering it is roughly 15 years old).

Now over the years various optimisations have been made to improve the performance, most notably opcode caching with APC, for example (so much so that APC will be a standard part of PHP 6 and not an optional module like it is now).

But still PHP scripts are basically transient. Session information is (normally) file based and mutually exclusive (session_start() blocks other scripts accessing the same user session until session_commit() or the script finishes) whereas that's not the case in ASP.NET. Aside from session data, it's fairly easy (and normal) to have objects that live within the application context in ASP.NET (or Java for that matter, which ASP.NET is much more similar to).

This is a key difference. For example, database access in PHP (using mysql, mysqli, PDO, etc) is transient (persistent connections notwithstanding) whereas .Net/Java will nearly always use persistent connection pools and build on top of this to create ORM frameworks and the like, the caches for which are beyond any particular request.

As a bytecode interpreted platform, ASP.NET is theoretically faster but the limits to what PHP can do are so high as to be irrelevant for most people. 4 of the top 20 visited sites on the internet are PHP for example. Speed of development, robustness, cost of running the environment, etc... tend to be far more important when you start to scale than any theoretical speed difference.

Bear in mind that .Net has primitive types, type safety and these sorts of things that will make code faster than PHP can run it. If you want to do a somewhat unfair test, sort an array of one million random 64 bit integers in both platforms. ASP.NET will kill it because they are primitive types and simple arrays will be more efficient than PHP's associative arrays (and all arrays in PHP are associative ultimately). Plus PHP on a 32 bit OS won't have a native 64 bit integer so will suffer hugely for that.

It should also be pointed out that ASP.NET is pre-compiled whereas PHP is interpreted on-the-fly (excluding opcode caching), which can make a difference but the flexibility of PHP in this regard is a good thing. Being able to deploy a script without bouncing your server is great. Just drop it in and it works. Brilliant. But it is less performant ultimately.

Ultimately though I think you're arguing what's really an irrelevant detail.

Solution 2 - Php

ASP.NET runs faster. ASP.NET Development is faster. Buy fast computer, and enjoy it if you do serious business web applications

ASP.NET code executes a lot faster compared to PHP, when it's builded in Release mode, optimized, cached etc etc. But, for websites (except big players, like Facebook), it's less important - the most time of page rendering time is accessing and querying database.

In connecting database ASP.NET is a lot better - in asp.net we typically use LINQ which translates our object queries into stored procedures in SQL server database. Also connection to database is persistent, one for one website, there is no need for reconnecting.

PHP, in comparison, can't hold sql server connection between request, it connect, grab data from db and destroys, when reconnecting the database is often 20-30% of page rendering time.

Also whole web application config is reloaded in php on each request, where in asp.net it persist in memory. It can be easily seen in big, enterprise frameworks like symfony/symfony2, a lot of rendering time is symfony internal processess, where asp.net loads it's once and don't waste your server for useless work.

ASP.NET can holds object in cache in application memory - in php you have to write it to files, or use hack like memcache. using memcache is a lot of working with concurrency and hazard problems (storing cache in files also have it's own problems with concurrency - every request start new thread of apache server and many request can work on one time - you have to think about concurrency between those threads, it take a lot of development time and not always work because php don't have any mutex mechanisms in language, so you can't make critical section by any way).

now something about development speed: ASP.NET have two main frameworks designed for it (Webforms and MVC), installed with environment, where in PHP you must get a open-source framework. There is no standard framework in php like in asp.NET.

ASP.NET language is so rich, standard library has solutions for very much common problems, where PHP standard library is ... naked... they can't keep one naming convention.

.NET has types, where PHP is dynamic, so it means no control about source code until you run it or write unit tests.

.NET has great IDE where PHP IDE's are average or average-good (PHPStorm is still a lot worse than VS+resharper or even without it)

PHP scaffolding in symfony is fired from command line when ASP.NET scaffolding is integrated into environment.

If you have slow computer like my (one core 2,2ghz), developing asp.net pages can be painfull because you have to recompile your project on any change of source code, where PHP code refresh immediately.

PHP language syntax is so unfinished, unsolid and naked compared to C# syntax. Strong types in C# and many flexible language features can speed up your development and make your code less buggy.

Solution 3 - Php

In my (non-hardbenchmarked) experience Asp.Net can certainly compete (and in some areas surpass) PHP in terms of raw speed. But similar with a lot of other language-choice related questions the following statement is (in this case) valid (in my opinion):

  • There are slow, buggy sites in language x (be it PHP or Asp.Net)
  • There are great, fast sites in language x (be it PHP or Asp.Net)

What i'm trying to say: the (talents of the) developer will influence the overall speed more than a choice between two (roughly equivalent in some abstracted extent) technologies.

Really, an 'overall speed' comparison does not make a lot of sense as both can catch up to each other in some way or another unless you're in a very specific specialist niche (which you have not informed us about).

Solution 4 - Php

I have done performance test.

Program : Sum of 10000000 Numbers

enter image description here

enter image description here

Given output proves that php is slower than C#............

Solution 5 - Php

I'd say ASP.net

Things to consider:

  • ASP.net is pre-compiled
  • ASP.net is usually written in C#, which should execute faster than PHP

Granted, the differences are very minor. There's advantages to both, I think PHP is much easier to deploy and can run on any server not just IIS. I am quite fond of ASP.net MVC though.

Solution 6 - Php

I am a developer expert on both technologies (ASP.Net c# and PHP5). After years and years of working and comparing them in real production environments these are my impressions:

  • First of all, cant compare them making a loop of adding values 1.000.000, this is not a real case.

  • Is not the same comparing them in my development environment than a real production env. Eg: In development ASP.Net does not use IIS by default, use a Inner Development server which has different optimizations. In dev, there is no concurrency.

So my opinion is the next:

  • Looping 1.000.000 times c# is going to be faster.(no-sense)

  • Serving a real page, that access DB, shows images, has forms etc.... ASP.Net is slower than PHP.

  • Weight of ASPX pages is x10 heavier than PHP, so this makes the final user to be waiting more time to get the page.

  • ASPX is slower to develop than PHP, this is important because at the end is money. We develop a 35% faster in PHP than ASP.Net, because of having to compile and restart every time u want to check smthg.

  • In big projects, ASP.Net in long term is better for avoiding errors and have a complex architechture.

  • Because of Windows Servers, IIS, .... at the end u need a powerfull server to hold the same amount of users on ASP than PHP. Eg: We serve with ASP.net arround 20.000 concurrent users and in PHP, the same server can get arround 30.000 users.

The only important thing is not if looping which one is faster. The thing is when website is real and is in production, how many users they can hold, how heavy is the page (heavier== more waiting time from users, more net charge of server, more disk charge of server, more memory charge of server). Try the checking times with concurrency and u will see.

Hope it helps.

Solution 7 - Php

Without any optimizations, a .net compiled app would of course run "faster" than php. But you are correct that it's a stupid and irrelevant argument because it has no bearing on the real world beyond bragging rights.

Solution 8 - Php

Generally ASP.Net will perform better on a given hardware than PHP. ASP.Net MVC can do better still (can being the operative word here). Most of the platform is designed with enterprise development in mind. Testable code, separation of concerns etc. A lot of the bloat in ASP.Net comes from the object stack within the page (nested controls). Pre-compiling makes this better performant, but it can be a key issue. MVC tends to allow for less nesting, using the webforms based view engine (others are available).

Where the biggest slowdowns in web applications happen tends to be remote services, especially database persistence. PHP is programmed without the benefit of connection pooling, or in-memory session state. This can be overcome with memcached and other, more performant service layers (also available to .Net).

It really comes down to the specifics of a site/application. this site happens to run MVC on fairly modest hardware quite well. A similar site under PHP would likely fall under its own weight. Other things to consider. IIS vs. Apache vs LightHTTPD etc. Honestly the php vs asp.net is much more than raw performance differences. PHP doesnt lend itself well to large, complex applications nearly so much as asp.net mvc, it's that simple... This itself has more to do with VS+SCC than anything else.

Solution 9 - Php

I'd tend to agree with you (that ASP.NET MVC is faster), but why not make a friendly wager with your friend and share the results? Create a really simple DYNAMIC page, derived from a MySQL database, and load the page many times.

For example, create a table with 1,000,000 rows containing a sequential primary key, and then a random # in the second column. Each of your sites can accept the primary key in a GET, retrieve the random # based on the passed in key, and display the random # in some type of dynamically generated html.

I'd love to know the results ... and if you have a blog or similar, the rest of the world would too (this question gets asked ALL the time).

It would be even better if you could build this simple little app in regular ASP too. Heck, I'd even pay you for these results if the test was well designed. Seriously - just express your interest here and I'll send you my e-mail.

Solution 10 - Php

Need to note that question is .NET MVC vs PHP, not .NET (Web Forms) vs PHP. I don't have the facts, but general feeling is PHP websites run faster than .NET Web form sites (and I do .NET only). .NET web forms despite being compiled vs interpreted PHP is generally slow because all the chunk of code that is autogenerated by the .NET engine to render the HTML for each < asp:control > you use on design mode. Getting a .NET web form to compete in speed with PHP is a complete odisea that starts with setting EnableViewState = false, and can end on using every html control with runat=server... crazy uh?

Now, MVC is a different story, I had made two websites using .NET MVC2 and feeling is good, you can feel the speed now! and code is as clean as any PHP website. So, now, MVC allows you write clean code as PHP does, and MVC is compiled against PHP interpreted, it can only lead to one thing, MVC faster than PHP... time will prove, when the general sense is "MVC websites runs faster than PHP" then we will be right about what I say here today.

see/you/!

Solution 11 - Php

C++... Right now the fight will be between PHP and ASP.NET. PHP will win on ease of use, ASP.NET will win on performance ( in a windows server ecosystem). A lot of the larger websites that started with php have graduated to C++.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Question7wpView Question on Stackoverflow
Solution 1 - PhpcletusView Answer on Stackoverflow
Solution 2 - PhpKamil OrzechowskiView Answer on Stackoverflow
Solution 3 - PhpChristopheDView Answer on Stackoverflow
Solution 4 - PhpHitesh ModhaView Answer on Stackoverflow
Solution 5 - PhpbbedwardView Answer on Stackoverflow
Solution 6 - PhpChristianView Answer on Stackoverflow
Solution 7 - PhppbreitenbachView Answer on Stackoverflow
Solution 8 - PhpTracker1View Answer on Stackoverflow
Solution 9 - PhpBeep beepView Answer on Stackoverflow
Solution 10 - PhpNestorView Answer on Stackoverflow
Solution 11 - PhpMikeView Answer on Stackoverflow