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

68,647

Solution 1

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

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

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

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

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.

Share:
68,647
7wp
Author by

7wp

Twitter: 7wp

Updated on July 05, 2022

Comments

  • 7wp
    7wp about 2 years

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

  • Andrei Rînea
    Andrei Rînea almost 15 years
    "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.
  • Tion
    Tion about 14 years
    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.
  • Lordn__n
    Lordn__n about 14 years
    @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?
  • Nestor
    Nestor over 13 years
    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
  • magallanes
    magallanes over 11 years
    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);
  • Luis
    Luis about 10 years
    @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
  • uygar.raf
    uygar.raf about 9 years
    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.
  • Drellgor
    Drellgor about 9 years
    What's the performance difference when HHVM is used for PHP? What does the code look like for both tests?
  • Craig Tullis
    Craig Tullis almost 9 years
    @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
    Craig Tullis almost 9 years
    To be precise, C#/.NET performed this test 42 times faster than PHP.
  • Luis
    Luis almost 9 years
    @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
  • Craig Tullis
    Craig Tullis almost 9 years
    @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!!!!).
  • aimme
    aimme almost 9 years
    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?
  • MÇT
    MÇT over 8 years
    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.
  • Shaiju T
    Shaiju T about 8 years
    Update on .NET Core check this and this post, it says .NET Core is faster than PHP, is that true ?
  • twigg
    twigg almost 8 years
    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
  • Craig Tullis
    Craig Tullis about 7 years
    @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.
  • Paul Cristea
    Paul Cristea over 6 years
    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.
  • irfandar
    irfandar over 6 years
    What about memory
  • TheLegendaryCopyCoder
    TheLegendaryCopyCoder over 5 years
    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
  • AmirHossein Rezaei
    AmirHossein Rezaei over 4 years
    "ASP.NET Development is faster".why do you think so?
  • Jacques
    Jacques over 4 years
    .net core changes this in terms of being able to deploy to any server
  • Jacques
    Jacques over 4 years
    @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
    Jacques over 4 years
    Check out 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
  • Reuel Ribeiro
    Reuel Ribeiro over 4 years
    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#....'
  • Geo Halkiadakis
    Geo Halkiadakis almost 4 years
    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"?
  • JonathanC
    JonathanC over 3 years
    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.
  • Lucas Resende
    Lucas Resende almost 3 years
    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
  • Michael Käfer
    Michael Käfer over 2 years
    This answer is old and definitly outdated in 2021. PHP and its ecosystems (like Symfony) improved a lot.
  • MeSo2
    MeSo2 over 2 years
    @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.