简体   繁体   English

代码执行速度:ASP.NET-MVC与PHP

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

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. 我对此同事的看法很友好,我个人的观点是,与用PHP编写的相同项目相比,ASP.NET-MVC编译的Web应用程序可以更高效/更快地运行。 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. 为此,我尝试向Google寻求答案,以尝试找到证据证明他是错误的,但是大多数时候,辩论都转向开发哪种平台,成本,安全性等方面更好。为此,论证我真的不在乎任何。

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? 我想知道堆栈溢出社区对使用MVC在ASP.NET中开发的网站与使用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 SO) (我意识到对于你们中的某些人来说,这很可能是无关紧要的,也许是愚蠢的论点,但这是一个论点,我仍然想听听这里的优秀人士的回答)

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. 我喜欢的PHP,以其最基本的形式加载了每个请求,被解释然后丢弃。 It is very much like CGI in this respect (which is no surprise considering it is roughly 15 years old). 在这方面,它非常类似于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. 但是,PHP脚本基本上还是暂时的。 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. 会话信息通常是基于文件的,并且是互斥的(session_start()阻止其他脚本访问同一用户会话,直到session_commit()或脚本完成),而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). 除了会话数据外,在ASP.NET(或者Java,ASP.NET与之更相似)中的应用程序上下文中存在对象也很容易(并且很正常)。

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. 作为字节码解释平台,ASP.NET从理论上讲速度更快,但是PHP可以执行的操作限制如此之高,以至于对大多数人而言都是无关紧要的。 4 of the top 20 visited sites on the internet are PHP for example. 例如,互联网上访问量最高的20个站点中有4个是PHP。 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. 请记住,.Net具有原始类型,类型安全性以及诸如此类的事物,这些事物会使代码比PHP运行它更快。 If you want to do a somewhat unfair test, sort an array of one million random 64 bit integers in both platforms. 如果要进行不公平的测试,请在两个平台上对一百万个随机的64位整数数组进行排序。 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). ASP.NET将杀死它,因为它们是原始类型,并且简单数组将比PHP的关联数组更有效(并且PHP中的所有数组最终都是关联的)。 Plus PHP on a 32 bit OS won't have a native 64 bit integer so will suffer hugely for that. 另外,在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. 还应该指出,ASP.NET是预编译的,而PHP是即时解释的(不包括操作码缓存),这可以有所作为,但是PHP在这方面的灵活性是一件好事。 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. 最终,尽管我认为您是在争论真正无关紧要的细节。

ASP.NET runs faster. ASP.NET运行速度更快。 ASP.NET Development is faster. ASP.NET开发速度更快。 Buy fast computer, and enjoy it if you do serious business web applications 购买快速的计算机,如果您做认真的商业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代码在发布模式下进行构建,优化,缓存等时,其执行速度要比PHP快得多。但是,对于网站(大型播放器(如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. 在连接数据库方面,ASP.NET更好-在asp.net中,我们通常使用LINQ,它将我们的对象查询转换为SQL Server数据库中的存储过程。 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. 相比之下,PHP无法在请求之间保持sql服务器连接,它无法连接,从db抓取数据并销毁,而在重新连接数据库时通常占页面呈现时间的20%至30%。

Also whole web application config is reloaded in php on each request, where in asp.net it persist in memory. 同样,每次请求时,整个Web应用程序配置都会重新加载到php中,在asp.net中,它会持久存储在内存中。 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. 在大型企业框架(如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. ASP.NET可以将对象保存在应用程序内存中的缓存中-在php中,您必须将其写入文件,或使用像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). 使用内存缓存会大量处理并发和危险问题(在文件中存储缓存也有并发性的问题-每个请求都会启动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. 现在有关开发速度的问题:ASP.NET具有为其设计的两个主要框架(Webforms和MVC),并随环境一起安装,在PHP中必须获得一个开源框架。 There is no standard framework in php like in asp.NET. 像asp.NET一样,php中没有标准框架。

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. 与C#语法相比,PHP语言语法还没有完成,不够固定和裸露。 Strong types in C# and many flexible language features can speed up your development and make your code less buggy. C#中的强类型和许多灵活的语言功能可以加快开发速度,并减少代码的错误。

In my (non-hardbenchmarked) experience Asp.Net can certainly compete (and in some areas surpass) PHP in terms of raw speed. 以我的经验(非硬性基准测试),Asp.Net的原始速度肯定可以与PHP竞争(在某些方面甚至超过PHP)。 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) 有x语言的慢速越野车网站(无论是PHP还是Asp.Net)
  • There are great, fast sites in language x (be it PHP or 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). 确实,“整体速度”比较没有多大意义,因为除非您处于非常特殊的专业领域(您尚未告知我们),否则两者都可能以某种方式相互追赶。

I have done performance test. 我已经做过性能测试。

Program : Sum of 10000000 Numbers 程序:10000000个数字的总和

在此处输入图片说明

在此处输入图片说明

Given output proves that php is slower than C#............ 给定的输出证明php比C#慢。

I'd say ASP.net 我会说ASP.net

Things to consider: 注意事项:

  • ASP.net is pre-compiled ASP.net已预编译
  • ASP.net is usually written in C#, which should execute faster than PHP 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. 两者都有优点,我认为PHP易于部署,并且可以在任何服务器上运行,而不仅仅是IIS。 I am quite fond of ASP.net MVC though. 我非常喜欢ASP.net MVC。

Without any optimizations, a .net compiled app would of course run "faster" than php. 如果不进行任何优化,.net编译的应用程序当然比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. 但是您是正确的,它是一个愚蠢且无关紧要的论点,因为除了吹牛的权利之外,它对现实世界没有影响。

I am a developer expert on both technologies (ASP.Net c# and PHP5). 我是这两种技术(ASP.Net c#和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. 首先,不能对它们进行比较,从而形成一个值1.000.000相加的循环,这不是真实情况。

  • 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. 例如:在开发中,ASP.Net默认情况下不使用IIS,请使用具有不同优化功能的内部开发服务器。 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) 循环1.000.000次c#会更快。(无意义)

  • Serving a real page, that access DB, shows images, has forms etc.... ASP.Net is slower than PHP. 服务于访问DB,显示图像,具有表格等的真实页面。ASP.Net比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页面的重量比PHP重10倍,因此最终用户要等待更多时间来获取页面。

  • ASPX is slower to develop than PHP, this is important because at the end is money. ASPX的开发速度比PHP慢,这很重要,因为最终是金钱。 We develop a 35% faster in PHP than ASP.Net, because of having to compile and restart every time u want to check smthg. 我们在PHP中开发的速度比ASP.Net快35%,因为每次您要检查smthg时都必须编译并重新启动。

  • In big projects, ASP.Net in long term is better for avoiding errors and have a complex architechture. 在大型项目中,长期使用ASP.Net可以更好地避免错误并具有复杂的体系结构。

  • Because of Windows Servers, IIS, .... at the end u need a powerfull server to hold the same amount of users on ASP than PHP. 由于Windows Servers,IIS,...。最后,您需要一个功能强大的服务器来在ASP上容纳与PHP相同数量的用户。 Eg: We serve with ASP.net arround 20.000 concurrent users and in PHP, the same server can get arround 30.000 users. 例如:我们为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. 希望能帮助到你。

Generally ASP.Net will perform better on a given hardware than PHP. 通常,ASP.Net在给定的硬件上将比PHP表现更好。 ASP.Net MVC can do better still (can being the operative word here). ASP.Net MVC仍然可以做得更好(在这里可以作为执行词)。 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). 可测试的代码,关注点分离等。ASP.Net中的许多膨胀来自页面内的对象堆栈(嵌套控件)。 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). 使用基于Web窗体的视图引擎(其他可用),MVC倾向于减少嵌套。

Where the biggest slowdowns in web applications happen tends to be remote services, especially database persistence. Web应用程序出现最大减慢的地方通常是远程服务,尤其是数据库持久性。 PHP is programmed without the benefit of connection pooling, or in-memory session state. PHP编程时没有连接池或内存中会话状态的好处。 This can be overcome with memcached and other, more performant service layers (also available to .Net). 可以使用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. 这个站点恰好在相当适中的硬件上运行MVC。 A similar site under PHP would likely fall under its own weight. PHP下的类似站点可能会自行承担费用。 Other things to consider. 其他要考虑的事情。 IIS vs. Apache vs LightHTTPD etc. Honestly the php vs asp.net is much more than raw performance differences. IIS,Apache,LightHTTPD等。老实说,php与asp.net的关系远不止原始性能差异。 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. PHP不能很好地适合大型,复杂的应用程序,就像asp.net mvc一样,它是如此简单...它本身与VS + SCC有更多关系。

Need to note that question is .NET MVC vs PHP, not .NET (Web Forms) vs PHP. 需要注意的是,问题是.NET MVC与PHP,而不是.NET(Web窗体)与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). 我没有事实,但是总体感觉是PHP网站的运行速度比.NET Web表单网站快(并且我仅运行.NET)。 .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. .NET Web表单尽管经过编译与解释,但PHP的运行速度通常较慢,因为由.NET引擎自动生成的所有代码块都可以为设计模式下使用的每个<asp:control>呈现HTML。 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 Web表单在速度上与PHP竞争是一个完全的恶心,它始于设置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! 现在,MVC是一个不同的故事,我使用.NET MVC2创建了两个网站,感觉不错,您现在可以感觉到速度了! and code is as clean as any PHP website. 而且代码就像任何PHP网站一样干净。 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允许您像PHP一样编写干净的代码,并且MVC是针对PHP解释编译的,它只能导致一件事,即MVC比PHP快……时间将证明,通常意义上是“ MVC网站运行比PHP更快”,那么我们今天在这里所说的话将是正确的。

see/you/! 再见/!

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? 我倾向于同意您的观点(ASP.NET MVC更快),但是为什么不与您的朋友下注并分享结果呢? Create a really simple DYNAMIC page, derived from a MySQL database, and load the page many times. 创建一个非常简单的从MySQL数据库派生的DYNAMIC页面,并多次加载该页面。

For example, create a table with 1,000,000 rows containing a sequential primary key, and then a random # in the second column. 例如,创建一个包含1,000,000行的表,其中包含一个顺序的主键,然后在第二列中包含一个随机#。 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. 您的每个网站都可以接受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. 如果您也可以在常规ASP中构建这个简单的小应用程序,那就更好了。 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. 认真地说-在这里表达您的兴趣,我会给您发送电子邮件。

C++... Right now the fight will be between PHP and ASP.NET. C ++ ...现在,战斗将在PHP和ASP.NET之间进行。 PHP will win on ease of use, ASP.NET will win on performance ( in a windows server ecosystem). PHP将在易用性上取胜,ASP.NET将在Windows Server生态系统中的性能上取胜。 A lot of the larger websites that started with php have graduated to C++. 许多以php开头的较大的网站都已升级为C ++。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM