代码执行速度:ASP.NET-MVC 与 PHP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/691942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Speed of code execution: ASP.NET-MVC versus PHP
提问by 7wp
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.
我与一位同事就此进行了友好的争论,我个人的观点是,ASP.NET-MVC 编译的 Web 应用程序比用 PHP 编写的同一个项目运行更有效/更快。我朋友不同意。
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?
我想知道堆栈溢出社区如何看待在 ASP.NET 中使用 MVC 开发的网站的原始速度/效率与使用 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.)
(我意识到对于你们中的一些人来说,这很可能是一个无关紧要的,也许是愚蠢的论点,但这是一个论点,我仍然想听听这里的好人的答案)
采纳答案by cletus
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).
我喜欢的 PHP 以其最基本的形式加载每个请求,解释然后丢弃。它在这方面非常像 CGI(考虑到它大约有 15 年的历史,这并不奇怪)。
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).
多年来,已经进行了各种优化以提高性能,最显着的是使用 APC 进行操作码缓存,例如(以至于 APC 将成为 PHP 6 的标准部分,而不是像现在这样的可选模块)。
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).
但是 PHP 脚本基本上是暂时的。会话信息(通常)是基于文件且互斥的(session_start() 阻止其他脚本访问同一用户会话,直到 session_commit() 或脚本完成),而在 ASP.NET 中并非如此。除了会话数据之外,在 ASP.NET(或与 ASP.NET 更相似的 Java)中的应用程序上下文中拥有对象相当容易(并且正常)。
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.
这是一个关键的区别。例如,PHP 中的数据库访问(使用 mysql、mysqli、PDO 等)是暂时的(尽管有持久连接),而 .Net/Java 几乎总是使用持久连接池并在此基础上构建 ORM 框架等,超出任何特定请求的缓存。
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.
作为字节码解释平台,ASP.NET 理论上更快,但 PHP 所能做的限制如此之高,以至于对大多数人来说无关紧要。例如,互联网上访问量最大的 20 个站点中有 4 个是 PHP。当您开始扩展时,开发速度、稳健性、运行环境的成本等往往比任何理论速度差异都重要得多。
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.
请记住,.Net 具有原始类型、类型安全性以及这些使代码运行速度比 PHP 更快的东西。如果您想做一个有点不公平的测试,请在两个平台上对一百万个随机 64 位整数的数组进行排序。ASP.NET 会杀死它,因为它们是原始类型,并且简单数组将比 PHP 的关联数组更有效(并且 PHP 中的所有数组最终都是关联的)。此外,32 位操作系统上的 PHP 不会有本机 64 位整数,因此会因此而遭受巨大损失。
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.
还应该指出的是,ASP.NET 是预编译的,而 PHP 是动态解释的(不包括操作码缓存),这可能会有所不同,但 PHP 在这方面的灵活性是一件好事。能够在不弹跳服务器的情况下部署脚本很棒。只需放入它,它就可以工作。杰出的。但它最终的性能较差。
Ultimately though I think you're arguing what's really an irrelevant detail.
最终,尽管我认为您在争论什么是真正不相关的细节。
回答by Kamil Orzechowski
ASP.NET runs faster. ASP.NET Development is faster. Buy fast computer, and enjoy it if you do serious business web applications
ASP.NET 运行速度更快。ASP.NET 开发速度更快。购买快速的计算机,如果您从事严肃的商业 Web 应用程序,就可以享受它
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.
与 PHP 相比,ASP.NET 代码的执行速度要快得多,当它在发布模式下构建、优化、缓存等时。但是,对于网站(除了大玩家,如 Facebook),它不太重要 - 页面渲染时间的大部分时间是访问和查询数据库。
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.
在连接数据库 ASP.NET 方面要好得多 - 在 asp.net 中,我们通常使用 LINQ,它将我们的对象查询转换为 SQL 服务器数据库中的存储过程。此外,与数据库的连接是持久的,一对一网站,无需重新连接。
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.
相比之下,PHP 无法保持请求之间的 sql server 连接,它连接,从 db 中抓取数据并销毁,重新连接数据库时往往是页面渲染时间的 20-30%。
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.
整个 Web 应用程序配置也会在每次请求时重新加载到 php 中,而在 asp.net 中,它会保留在内存中。在像 symfony/symfony2 这样的大型企业框架中很容易看到,很多渲染时间是 symfony 内部进程,其中 asp.net 加载一次,不要浪费您的服务器进行无用的工作。
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).
ASP.NET 可以在应用程序内存的缓存中保存对象 - 在 php 中你必须将它写入文件,或者使用像 memcache 这样的 hack。使用 memcache 有很多并发和危险问题(将缓存存储在文件中也有它自己的并发问题 - 每个请求都启动 apache 服务器的新线程,许多请求可以一次处理 - 你必须考虑这些之间的并发线程,它需要大量的开发时间并且并不总是有效,因为 php 在语言中没有任何互斥机制,所以你不能以任何方式制作关键部分)。
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 有两个主要的框架(Webforms 和 MVC),安装了环境,在 PHP 中你必须得到一个开源框架。php 中没有像 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.
ASP.NET 语言是如此丰富,标准库对很多常见问题都有解决方案,而 PHP 标准库是……赤裸裸的……他们不能保持一种命名约定。
.NET has types, where PHP is dynamic, so it means no control about source code until you run it or write unit tests.
.NET 具有类型,其中 PHP 是动态的,因此这意味着在运行源代码或编写单元测试之前无法控制源代码。
.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)
.NET 有很棒的 IDE,其中 PHP IDE 的表现一般或一般(PHPStorm 仍然比 VS+resharper 差很多,甚至没有它)
PHP scaffolding in symfony is fired from command line when ASP.NET scaffolding is integrated into environment.
当 ASP.NET 脚手架集成到环境中时,symfony 中的 PHP 脚手架从命令行触发。
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.
如果你有像我这样的慢速计算机(一个核心 2,2ghz),开发 asp.net 页面可能会很痛苦,因为你必须在源代码发生任何变化时重新编译你的项目,PHP 代码会立即刷新。
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.
与 C# 语法相比,PHP 语言语法是如此未完成、不稳固和赤裸裸的。C# 中的强类型和许多灵活的语言功能可以加速您的开发并减少您的代码错误。
回答by ChristopheD
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):
根据我的(非硬基准)经验,Asp.Net 在原始速度方面肯定可以与 PHP 竞争(并且在某些领域超过)PHP。但与许多其他与语言选择相关的问题类似,以下陈述(在这种情况下)是有效的(在我看来):
- 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)
- 使用 x 语言(无论是 PHP 还是 Asp.Net)存在缓慢、有问题的站点
- 语言 x 有很棒的快速站点(无论是 PHP 还是 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).
确实,“整体速度”比较没有多大意义,因为除非您处于非常特定的专业领域(您尚未告知我们),否则两者都可以以某种方式相互追赶。
回答by Hitesh Modha
I have done performance test.
我已经做了性能测试。
Program : Sum of 10000000 Numbers
程序:10000000个数字的总和




Given output proves that php is slower than C#............
给出的输出证明 php 比 C# 慢…………
回答by bbedward
I'd say ASP.net
我会说 ASP.net
Things to consider:
需要考虑的事项:
- ASP.net is pre-compiled
- ASP.net is usually written in C#, which should execute faster than PHP
- ASP.net 是预编译的
- ASP.net 通常是用 C# 编写的,它应该比 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.
当然,差异非常小。两者都有优点,我认为 PHP 更容易部署,并且可以在任何服务器上运行,而不仅仅是 IIS。不过,我非常喜欢 ASP.net MVC。
回答by pbreitenbach
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.
如果没有任何优化,.net 编译的应用程序当然会比 php 运行“更快”。但你是对的,这是一个愚蠢且无关紧要的论点,因为它除了吹牛之外与现实世界没有任何关系。
回答by Tracker1
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).
一般来说,ASP.Net 在给定的硬件上比 PHP 表现得更好。ASP.Net MVC 还可以做得更好(可以是这里的操作词)。大多数平台的设计都考虑到了企业发展。可测试的代码、关注点分离等。ASP.Net 中的很多膨胀都来自页面内的对象堆栈(嵌套控件)。预编译使其性能更好,但这可能是一个关键问题。MVC 倾向于允许较少的嵌套,使用基于 webforms 的视图引擎(其他可用)。
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).
Web 应用程序中最大的减速往往是远程服务,尤其是数据库持久性。PHP 的编程没有连接池或内存中会话状态的好处。这可以通过 memcached 和其他更高性能的服务层(也可用于 .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.
这真的归结为站点/应用程序的细节。这个站点碰巧在相当普通的硬件上运行 MVC 非常好。PHP 下的类似站点可能会受到自身影响。其他需要考虑的事情。IIS 与 Apache 与 LightHTTPD 等。老实说,php 与 asp.net 不仅仅是原始性能差异。PHP 不太适合大型、复杂的应用程序,几乎像 asp.net mvc 一样,就是这么简单……这本身与 VS+SCC 的关系比其他任何事情都多。
回答by Christian
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:
我是这两种技术(ASP.Net c# 和 PHP5)的开发专家。经过多年的工作并在实际生产环境中比较它们,这些是我的印象:
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.
首先,无法比较它们使值相加 1.000.000 的循环,这不是真实情况。
在我的开发环境中比较它们与真正的生产环境不同。例如:在开发中 ASP.Net 默认不使用 IIS,使用具有不同优化的内部开发服务器。在开发中,没有并发。
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.
循环 1.000.000 次 c# 会更快。(无意义)
服务一个真实的页面,访问数据库,显示图像,有表单等...... ASP.Net 比 PHP 慢。
ASPX 页面的重量比 PHP 重 10 倍,因此这使得最终用户需要等待更多时间来获取页面。
ASPX 的开发速度比 PHP 慢,这很重要,因为最终是金钱。我们在 PHP 中的开发速度比 ASP.Net 快 35%,因为每次要检查 smthg 时都必须编译和重新启动。
在大型项目中,ASP.Net 从长远来看更适合避免错误并具有复杂的架构。
由于 Windows 服务器,IIS,.... 最后你需要一个强大的服务器来容纳与 PHP 相同数量的 ASP 用户。例如:我们为 ASP.net 提供大约 20.000 个并发用户,而在 PHP 中,同一台服务器可以获得大约 30.000 个用户。
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.
希望能帮助到你。
回答by Nestor
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?
需要注意的是,问题是 .NET MVC vs PHP,而不是 .NET(Web Forms) vs PHP。我没有事实,但总体感觉是 PHP 网站的运行速度比 .NET Web 表单网站快(而且我只使用 .NET)。.NET web 表单尽管被编译与解释 PHP 通常很慢,因为所有代码块都是由 .NET 引擎自动生成的,用于为您在设计模式下使用的每个 <asp:control> 呈现 HTML。让 .NET web 表单与 PHP 竞争速度是一个完整的 odisea,从设置 EnableViewState = false 开始,然后可以使用 runat=server 的每个 html 控件结束...疯狂呃?
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.
现在,MVC 是另一回事了,我用.NET MVC2 做了两个网站,感觉很好,你现在可以感受到速度了!并且代码和任何 PHP 网站一样干净。所以,现在,MVC 允许你像 PHP 一样编写干净的代码,而 MVC 是针对 PHP 解释编译的,它只能导致一件事,MVC 比 PHP 更快......时间会证明,当一般意义上的“MVC 网站运行比 PHP 更快”,那么我今天在这里所说的就是正确的。
see/you/!
再见/!
回答by Beep beep
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.
我倾向于同意您的观点(ASP.NET MVC 更快),但为什么不与您的朋友友好地打赌并分享结果呢?创建一个非常简单的动态页面,派生自 MySQL 数据库,并多次加载该页面。
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.
例如,创建一个包含 1,000,000 行的表,其中包含一个顺序主键,然后是第二列中的随机 #。您的每个站点都可以接受 GET 中的主键,根据传入的键检索随机 #,并在某种类型的动态生成的 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.
如果您也可以在常规 ASP 中构建这个简单的小应用程序,那就更好了。哎呀,如果测试设计得很好,我什至会为这些结果付钱给你。说真的 - 只要在这里表达您的兴趣,我就会给您发送我的电子邮件。

