75

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.)

7wp
  • 12,505
  • 20
  • 77
  • 103
  • 38
    In case you don't know, StackOverFlow is built on ASP.NET MVC. So here is a fine example of an awesome project using ASP.NET MVC. – Umair Khan Jadoon Sep 03 '11 at 04:37
  • Source please?. – mercury Nov 09 '21 at 05:37
  • @HossamMaher It's common knowledge if you have been following along Jeff Atwood's podcasts since the begging of it's inception. But here is a link: https://stackoverflow.blog/2008/09/21/what-was-stack-overflow-built-with/ – 7wp Dec 15 '21 at 17:28

11 Answers11

72

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.

Aurélien Gasser
  • 3,043
  • 1
  • 20
  • 25
cletus
  • 616,129
  • 168
  • 910
  • 942
  • 6
    "As a bytecode interpreted platform, ASP.NET" - there is no interpretation for Java or .NET whatsoever. There is a 2-stage compilation : 1-source code to bytecode / IL, 2-bytecode/IL to native code. In the end only native code is executed. – Andrei Rînea Sep 14 '09 at 21:07
  • 12
    Excellent and insightful comparison. But I have to point out one thing "...flexibility of PHP in this regard is a good thing. Being able to deploy a script without bouncing your server is great." You certainly do NOT need to bounce your server in ASP.NET to release code changes. – Tion May 26 '10 at 18:17
  • @Tion I'm less familiar with ASP.NET hot deploys than some other frameworks but in Java for example you can drop in JSP files and *sometimes* can reload classes but in a lot of cases you simply need to restart. How does that compare? – cletus May 26 '10 at 21:58
  • 7
    All comparison made in this reply seems to be PHP vs .NET (web forms). About that I can say PHP is faster than .NET in real world and practical scenarios... reason for what I switched to MVC... I still want to see some comparisin PHP vs .NET MVC – Nestor Jan 06 '11 at 22:33
  • 1
    **Update on .NET Core** check [this](http://www.c-sharpcorner.com/news/essence-of-dotnetconf-keynote) and [this](http://web.ageofascent.com/asp-net-core-exeeds-1-15-million-requests-12-6-gbps/) post, it says .NET Core is faster than PHP, is that true ? – Shaiju T Jun 13 '16 at 13:42
  • @stom ASP.NET is compiled code. Not "bytecode" interpreted, but compiled to native code by an optimizing compiler. PHP is interpreted script. There are some optimizations that help, but it still isn't compiled code. In terms of raw performance benchmarks, yes .NET is faster than PHP. – Craig Tullis May 15 '17 at 02:20
  • What about memory – irfandar Feb 13 '18 at 06:32
33

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.

xdazz
  • 158,678
  • 38
  • 247
  • 274
  • Sheesh, mentioning LINQ says everything. Regular LINQ is not more than a regular foreach/if that works fine but it just could save a couple of line of code while sacrify flexibility. But, most of the time, we talk about the (mostly hated) LINQ to SQL, that is fine for small projects but it is useless for anything complex. A simple and encapsulated DAO is way cleaner than a complex LINQ.And it is valid for .net and for PHP. For example (after the DAO layer is done), to get a user object is so as simple as to call c#: User=UserDAO.Get(id_user); and in php $User=$UserDAO->Get($id_user); – magallanes Jan 20 '13 at 14:31
  • 4
    @magallanes LINQ is much more than a regular foreach/if, depending on the implementation of the LINQ provider. Check Entity Framework for instance. A simple encapsulated DAO with no typed checked queries, with potential SQL injections because the developer missed the morning coffee is not way cleaner, IMO is the opposite – Luis Apr 14 '14 at 15:14
  • @Luis There is absolutely no denying, however, that LINQ to SQL (like any ORM) adds substantial overhead in the majority of cases and negatively impacts performance. StackOverflow is a case in point, having used a lot of LINQ, but then having backed a lot of it out for performance reasons and replacing it with their own homegrown micro-ORM. – Craig Tullis Aug 05 '15 at 01:17
  • @Craig when you talk about LINQ you need to specify what is the underlying ORM or DAO engine, some micro-ORMs have LINQ providers. I personally dont like EF and prefer something like a micro-ORM (if they allow for typed checked queries, like SQL Fu or ORMlite) but it does not have to do with performance but with transparency and readability – Luis Aug 05 '15 at 05:28
  • @Luis I don't believe it's LINQ so much as it's the nature of most ORMS. So in the case of LINQ2SQL the bottleneck is actually Enterprise Frameworks. LINQ itself is generally fine to outstanding. It was the next super-obvious evolution once Microsoft got extension methods into C# (thank you Objective-C!!!!). – Craig Tullis Aug 05 '15 at 15:04
  • This entire answer is full of incorrect facts, PHP does HAVE persistent connections, PHP isn't reloaded on every request due to opcaching and configs being loaded on every request are a feature of symfony not PHP. I can't believe such in accurate information has so many up votes, the only thing worse than your completely false 'facts' is your level of English – twigg Jul 26 '16 at 20:08
  • "ASP.NET Development is faster".why do you think so? – AmirHossein Rezaei Oct 24 '19 at 04:07
  • This answer is old and definitly outdated in 2021. PHP and its ecosystems (like Symfony) improved a lot. – Michael Käfer Dec 22 '21 at 12:49
23

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).

ChristopheD
  • 112,638
  • 29
  • 165
  • 179
18

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#............

Mohammad Ayoub Khan
  • 2,422
  • 19
  • 24
Hitesh Modha
  • 2,740
  • 5
  • 28
  • 47
  • 5
    What's the performance difference when HHVM is used for PHP? What does the code look like for both tests? – Drellgor Jun 02 '15 at 17:19
  • 8
    To be precise, C#/.NET performed this test 42 *times* faster than PHP. – Craig Tullis Aug 05 '15 at 01:19
  • 2
    Ok the server will produce the result soon.see the size ASPx 285Bytes and Php 49Bytes So, In this case if the user is with a slower connection guess what will happen? or think about the internet bill of the user visiting the site? – aimme Aug 28 '15 at 19:01
  • 3
    OP wants a comparison for .NET MVC, not .NET WebForms. It is clear by examining size of both outputs that the test.aspx page is coded with .NET WebForms, not .NET MVC. By using .NET MVC, the output can be the same size as PHP. Therefore, connection speed or internet bill of the user will be irrelevant. – MÇT Jan 29 '16 at 09:52
  • 4
    I'd rewrite as: 'The given output proves that, for god knows under which conditions this test has been performed and who knows how the code has been written, php is slower than C#....' – Reuel Ribeiro Jan 19 '20 at 23:25
  • This answer gives no value at all; the approach is simplistic and childish; no code provided; no setup information provided; just 2 screenshots and you call this "proof"? – Geo Halkiadakis Aug 07 '20 at 10:36
  • 1
    there are several settings for performance in php and probably c# as well. The test is very flawed because it doesn't specify any basic configuration like php version. php 8 is infinitely more performative than 5. And not code example – Lucas Resende Jul 08 '21 at 14:06
  • @LucasResende and was that test using PHP using IIS or PHP via Apache. And what about the OS? I would like to see that same test run on Ubuntu. – MeSo2 Jan 15 '22 at 03:57
15

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.

bbedward
  • 6,368
  • 7
  • 36
  • 59
14

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.

Michel Ayres
  • 5,891
  • 10
  • 63
  • 97
Christian
  • 437
  • 1
  • 4
  • 13
  • 6
    Until you provide some reasonable metrics or some links to actual articles sir, all I can say in response to this answer is this: According to my experience (which includes 15 years of web application and application server development in practically any runtime ranging from PHP to Python to .NET) PHP is by far the slowest runtime. Could you possibly be confusing ASP with ASP.NET MVC? Because the question was actually about comparing web apps developed in PHP (and I believe it includes the assumption of using a PHP MVC framework, but that's not necessary) and web apps developed in ASP.NET MVC. – uygar.raf Apr 20 '15 at 15:26
  • 1
    Thanks for posting this 'answer', it definitely adds valuable information and presents other areas to be taken into consideration. What people don't realize is that they should take any response with a grain of salt, rather than defacto answer. – Paul Cristea Nov 15 '17 at 07:05
  • 2
    Good feedback. Too bad the MS fanboys down voted your answer. I personally find php development to be faster, easier, simpler than .Net. Also the response times of the application in a live environment are very impressive on severely limited server hardware. I can now see why Php is so popular – TheLegendaryCopyCoder Nov 24 '18 at 11:15
  • @TheLegendaryCopyCoder I don't think downvoting occurred because they're MS fanboys. It's because Christian is comparing the wrong thing. ASPX is much slower than Asp.net MVC and has far bigger payloads - it's older technology. It doesn't make sense comparing almost outdated .net technology with modern PHP environments? – Jacques Dec 18 '19 at 15:44
  • I too have experience building larger applications in php and c#. There is a big difference. .net is considerably slower to develop on and execution is slower. However there are also vastly different experiences when you build with php5 or 7, c# with EF or your own dal. Ie. Your code can have a dramatic impact. – JonathanC Jan 20 '21 at 17:43
7

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.

pbreitenbach
  • 11,261
  • 3
  • 33
  • 24
3

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.

Tracker1
  • 19,103
  • 12
  • 80
  • 106
1

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/!

Nestor
  • 1,969
  • 4
  • 25
  • 30
1

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.

Beep beep
  • 18,873
  • 12
  • 63
  • 78
-3

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++.

Mike
  • 68
  • 6
  • 2
    Check out https://stackoverflow.com/help/how-to-answer to make sure you provide good answers and avoid downvotes. In this case the downvotes were no doubt because you strayed from the PHP vs Asp.net MVC basis of the question. C++ might very well be a viable alternative, but is outside the scope of the question. Hope that helps – Jacques Dec 18 '19 at 15:53