php 为什么不将 C 用于 Web 应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3079153/
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
Why do you not use C for your web apps?
提问by Abs
I was having a look at a few different web servers this morning when I came across G-WAN. As I understand, its a web server written in C and you have to make use of it by writing your websites/webapps in C. One clear benefit is speed as the G-WAN site suggests.
今天早上当我遇到G-WAN时,我正在查看几个不同的 Web 服务器。据我了解,它是一个用 C 编写的网络服务器,你必须通过用 C 编写你的网站/网络应用程序来使用它。一个明显的好处是 G-WAN 站点建议的速度。
However, on the forums, the creator of G-WAN asked why not use C for web based apps and I can not think of a single reason apart from it being difficult (for me anyway, I am newbie when it comes to C). There must be more reasons why we all use PHP, Python, Ruby etc apart from it being easy to develop in these languages. I don't see that as a good reason.
但是,在论坛上,G-WAN 的创建者问为什么不将 C 用于基于 Web 的应用程序,我想不出除了困难之外的唯一原因(对我来说,无论如何,我是 C 的新手)。除了易于使用这些语言进行开发之外,我们都使用 PHP、Python、Ruby 等肯定还有更多原因。我不认为这是一个很好的理由。
So I put it to you: Why do you not use C for your web apps?
所以我告诉你:你为什么不在你的网络应用程序中使用 C?
采纳答案by Dave Markle
It takes a great deal of care to get a C program correct and secure. That care means that you need to have really good people writing your programs. That means you pay more.
使 C 程序正确和安全需要非常小心。这种关心意味着您需要有非常优秀的人来编写您的程序。这意味着你支付更多。
Also, C doesn't have the benefit of drawing from an enormous single standard library of functionality as .NET (and the other major web-centric platforms) has. So you may have to either buy components, or perform interop, or roll your own functionality which comes "for free" with a more, shall we say "web-centric" language like PHP or C# or Ruby or whatever. That means you pay more.
此外,C 没有像 .NET(和其他主要的以 Web 为中心的平台)那样从庞大的单一标准功能库中提取的好处。因此,您可能不得不购买组件,或执行互操作,或者推出您自己的“免费”功能,我们可以说“以网络为中心”的语言,如 PHP 或 C# 或 Ruby 或其他什么。这意味着你支付更多。
Add all of that to the fact that single-threaded computational speed just isn't that important on the web. If you need more scalability, most organizations can economically just throw more cores at the problem and be fine. This is not true for everyone, of course. I'd imagine that the core of Google's engine is written in C or a similar language not only for speed, but to save real money in power costs.
将所有这些添加到单线程计算速度在网络上并不那么重要的事实。如果您需要更高的可扩展性,大多数组织可以经济地将更多内核用于解决问题并解决问题。当然,这并不适用于所有人。我想谷歌引擎的核心是用 C 或类似语言编写的,不仅是为了速度,而且是为了节省电力成本。
回答by Pierre
Hum...
哼...
It seems that I am a bit late in this discussion - but I just discovered it now. And I am grateful to all of you for so much input.
似乎我在这个讨论中有点晚了 - 但我现在才发现它。我很感谢你们所有人提供了这么多的意见。
I am G-WAN's author, which makes it clear that I have seriously worked on the matter: G-WAN is both faster than all other Web Servers (no processing) andall other Web Application Servers (any processing you can imagine).
我是 G-WAN 的作者,这清楚地表明我已经认真地处理了这个问题:G-WAN 比所有其他 Web 服务器(无处理)和所有其他 Web 应用程序服务器(您可以想象的任何处理)都快。
Yes, ANSI C also made it possible to process more static content - with less powerful CPUs (ANSI C is not only about making dynamic contents fly).
是的,ANSI C 还使处理更多静态内容成为可能 - 使用功能较弱的 CPU(ANSI C 不仅仅是让动态内容飞起来)。
By the way, G-WAN uses C scripts (no C compiler and linker needed) so the compiling/linking cycle/delay does not exist.
顺便说一下,G-WAN 使用 C 脚本(不需要 C 编译器和链接器),因此不存在编译/链接周期/延迟。
In the process of comparing G-WAN to .NET Java and PHP, I wrote similarapplications in all 4 languages: http://gwan.ch/source/
在比较 G-WAN 与 .NET Java 和 PHP 的过程中,我用所有 4 种语言编写了类似的应用程序:http: //gwan.ch/source/
And, to my dismay, the modern scripting languages were noteasier to use.
而且,令我沮丧的是,现代脚本语言并不容易使用。
One part of the job which is especially frustrating is to desperately searchfor the 'magic' API call that will do what you want to do.
工作中特别令人沮丧的部分是拼命搜索可以执行您想做的事情的“神奇”API 调用。
Think about how to do 'pretty thousands' in:
想一想如何在以下方面做“相当多”:
C#
C#
String.Format("{0:n}"...
Java
爪哇
new DecimalFormat("0.00"); ...
PHP
PHP
number_format($amount, 2); ...
ANSI C
ANSI C
sprintf("%'.2f", amount);
The "..." mean that some pre-configuration, or post processing, is necessary. ANSI C is clearly easier to use and to remember.
“...”表示需要一些预先配置或后处理。ANSI C 显然更易于使用和记忆。
When PHP has more than 5900 API calls (C# and Java not far away), finding the rightAPI call is a challenge on its own. The time wasted to find this (and then to find how badly the nativeAPI call is implemented), the time to learn by hart it for the next time you need it, all this time is depriving you from the time necessary to resolve your application problems.
当 PHP 有超过 5900 个 API 调用(C# 和 Java 不远)时,找到正确的API 调用本身就是一个挑战。浪费时间去发现这个(然后发现原生API 调用的实现有多糟糕),下一次需要的时候通过 hart 来学习,所有这些时间都在剥夺你解决应用程序所需的时间问题。
I have read (above) that PHP is more concise than ANSI C? Why then use "//:: this is a comment ::"rather than "// this is a comment"? Why have a so stupidly complex 'pretty thousands' syntax?
我读过(以上)PHP 比 ANSI C 更简洁?为什么然后使用"//:: this is a comment ::"而不是"// this is a comment"?为什么有一个如此愚蠢复杂的“成千上万”的语法?
The other usual argument is that Java and the like provide dedicated calls for Web applications.
另一个常见的论点是 Java 等为 Web 应用程序提供专用调用。
I was not able to find anything to escape HTML in Java so I wrote my version of it:
我找不到任何可以在 Java 中转义 HTML 的内容,因此我编写了它的版本:
// all litteral strings provided by a client must be escaped this way
// if you inject them into an HTML page
public static String escape_html(String Name) {
int len = Name.length();
StringBuffer sb = new StringBuffer(len);
boolean lastWasBlankChar = false;
int c;
for(int i=0; i<len; i++) {
c = Name.charAt(i);
if(c == ' ') sb.append(" "); else
if(c == '"') sb.append("""); else
if(c == '&') sb.append("&"); else
if(c == '<') sb.append("<"); else
if(c == '>') sb.append(">"); else
if(c == '\n') sb.append("<br/>"); else {
c = c&0xffff; // unicode
if(c < 32 || c > 127) {
sb.append("&#");
sb.append(new Integer(c).toString());
sb.append(';');
} else
sb.append(c);
}
}
return sb.toString();
//szName = sb.toString();
}
Do you really believe that the same code in ANSI C would be more complex? No, it would be both immensely simpler andfaster.
您真的相信 ANSI C 中的相同代码会更复杂吗?不,它会非常简单和快速。
Java (derived from C) is requiringprogrammers to link multi-line strings with a '+'
C# (derived from C) is requiringprogrammers to link multi-line strings with a '+'
PHP (derived from C) is requiringprogrammers to link multi-line strings with a '.'
Java(派生自 C)要求程序员用“+”链接多行字符串
C#(派生自 C)要求程序员用“+”链接多行字符串
PHP(派生自 C)要求程序员用“.”链接多行字符串
ANSI C does not have this now completely stupid (obsolete) requirement.
ANSI C 现在没有这个完全愚蠢(过时)的要求。
So, were is the so obviousprogress claimed by the modern languages? I am still looking for it.
那么,现代语言所宣称的如此明显的进步是真的吗?我还在寻找它。
Sincerely,
真挚地,
Pierre.
皮埃尔。
回答by Quentin
The same reason we don't use C for most programming. The benefits (which are mostly performance) don't outweigh the costs (development time, lack of automatic memory management, lack of automatic protection from buffer overflows, having a compile stage between the edit and test stages, etc).
与我们在大多数编程中不使用 C 的原因相同。好处(主要是性能)不会超过成本(开发时间、缺乏自动内存管理、缺乏缓冲区溢出的自动保护、在编辑和测试阶段之间有一个编译阶段等)。
回答by Paul Tomblin
Most network applications, especially web servers, are far more "I/O bound" - ie they are capable of pumping out data far faster than the network can accept it. Therefore something that is highly CPU efficient is not a huge win, whereas something that is scalable and maintainable is. So there is no reason to accept the drawbacks of C and lose the advantages of a managed environment like Java, .NET, Python, Perl or other languages.
大多数网络应用程序,尤其是 Web 服务器,都更加“受 I/O 限制”——即它们能够以远快于网络接受数据的速度输出数据。因此,CPU 效率高的东西并不是一个巨大的胜利,而可扩展和可维护的东西才是。因此,没有理由接受 C 的缺点并失去托管环境(如 Java、.NET、Python、Perl 或其他语言)的优势。
回答by ChrisW
C isn't a convenient language for manipulating strings.
C 不是一种操作字符串的方便语言。
Compare C#:
比较 C#:
string foo = "foo";
string bar = "bar";
string foobar = foo + bar;
Corresponding C:
对应C:
const char* foo = "foo";
const char* bar = "bar";
char* foobar = (char*)malloc(strlen(foo)+strlen(bar)+1);
strcpy(foobar, foo);
strcat(foobar, foo);
//todo: worry about when/where the foobar memory
//which was allocated by malloc will be freed
回答by Ken
If difficulty and complexity were not an issue at all (ha!), then I wouldn't stop at C. I'd write x86 assembly. It's been years since I've used any web server that wasn't x86, and it's looking less and less likely every day.
如果难度和复杂性根本不是问题(哈!),那么我不会停留在 C。我会编写 x86 程序集。我已经好几年没有使用任何非 x86 的 Web 服务器了,而且它看起来每天都越来越不可能。
To use C (instead of assembly, or something higher-level) is to suggest that C is the sweet spot of programmer efficiency and computer efficiency.
使用 C(而不是汇编或更高级别的东西)意味着 C 是程序员效率和计算机效率的最佳结合点。
For the programs I write, this is not the case: C is a poor match to the kinds of programs I want to write, and the advantages of it over a decent macro assembler just aren't that significant. The program I'm writing right now is not hard in my HLL of choice, but the complexity of it in either assembly or C would be so high that it would never get finished. I acknowledge that a sufficiently smart programmer with enough time could make it run faster in assembly or C, but I am not that programmer.
对于我编写的程序,情况并非如此:C 与我想编写的程序类型不太匹配,而且它相对于体面的宏汇编程序的优势并不那么重要。我现在正在编写的程序在我选择的 HLL 中并不难,但是它在汇编或 C 中的复杂性非常高,以至于永远不会完成。我承认一个足够聪明的程序员有足够的时间可以让它在汇编或 C 中运行得更快,但我不是那个程序员。
回答by Jordan
I know this question has already been answered to death, but there are 2 things not mentioned so far that are extraordinarily important to success in any programming paradigm, and especially in web development where you get a lot of people that aren't necessarily programmers, working with the code.
我知道这个问题已经得到了答案,但是到目前为止,有两件事对于在任何编程范式中的成功都非常重要,尤其是在 Web 开发中,您会遇到很多不一定是程序员的人,使用代码。
- Involved, useful community, aka People That Have Solved My Problem Already. It's pretty easy for even the noobiest of noobs to Google why they're getting "headers already sent" errors in PHP, whereas that information might not be available for a framework or language that is new to the scene, or otherwise doesn't have critical mass.
- Frameworks, so that most programmers can get to solving business problems and not hacking code together.
- 参与的、有用的社区,也就是已经解决了我的问题的人。即使是 Google 的新手也很容易理解为什么他们会在 PHP 中收到“标头已经发送”的错误,而这些信息可能无法用于场景中的新框架或语言,或者没有临界质量。
- 框架,以便大多数程序员可以解决业务问题,而不是一起编写代码。
If I had a critical app that required extreme performance, I would use C, but it would take so much longer to write, that I would never get to market. Until there is either #1 or #2 it's not feasible for me to use it.
如果我有一个需要极端性能的关键应用程序,我会使用 C,但编写需要更长的时间,以至于我永远无法上市。除非有 #1 或 #2,否则我无法使用它。
回答by L??o???n???g???p??o???k???e???
- it's insecure
- it's hard to read
- it's hard to maintain, development time is slower on the order of a magnitude
- most of your web stuff is probably I/O bound, so the would be speedup doesn't even matter, especially when you use a fast language like Java or C#
回答by Artyom
C is quite low level languages for many purposes: no-OOP, lots of manual resource management.
C 是非常低级的语言,有很多用途:无 OOP,大量手动资源管理。
There is limited use of C for web, for example Klone. It is mostly used for low resource embedded application cases.
C 语言在网络上的使用有限,例如Klone。它主要用于低资源嵌入式应用案例。
However there are C++web frameworks like CppCMSthat are used for high performance web applications developments.
但是,有C++Web 框架,如CppCMS,可用于高性能 Web 应用程序开发。
C++ allows you to enjoy high abstraction and fine grained access to what you are doing exactly giving much better option for deploying and developing large applications.
C++ 允许您享受对您正在做的事情的高度抽象和细粒度访问,从而为部署和开发大型应用程序提供更好的选择。
But you use them in case when performance and resource usage is much more critical then time-to-market and development costs as generally web development is faster using good web frameworks for languages like Java, Python or PHP. Also generally there less competent programmers for C++ then Java/P* languages for same salary.
但是,在性能和资源使用比上市时间和开发成本更重要的情况下,您可以使用它们,因为通常使用 Java、Python 或 PHP 等语言的良好 Web 框架进行 Web 开发会更快。同样,同样薪水的 C++ 程序员通常比 Java/P* 语言的程序员能力差。
So it is question of priorities, also there less tools for C++ Web development then for PHP/Python/Perl or Java.
所以这是优先级的问题,C++ Web 开发的工具比 PHP/Python/Perl 或 Java 少。
回答by Pierre
@Joeri Sebrechts
@Joeri Sebrechts
F.U.D. in action:
FUD在行动:
PHP, Python and so on are easy to scale up by throwing hardware at the problem.
PHP、Python 等很容易通过将硬件投入到问题中来扩展。
Well, actually no. They don't scale at all vertically and scale very poorly horizontally. See: http://gwan.ch/en_scalability.htmlwhere it is explained how much trouble is ahead of bad-performers.
嗯,实际上没有。它们在垂直方向上根本无法缩放,而在水平方向上的缩放效果非常差。请参阅:http: //gwan.ch/en_scalability.html,其中解释了表现不佳的人会遇到多少麻烦。
Suppose it costs 1 person 1 year of effort to develop an app in PHP, and it costs them 3 years to do it in C (as C requires more effort to do the same thing).
假设用 PHP 开发一个应用程序需要 1 个人 1 年的努力,而用 C 开发一个应用程序需要花费 3 年的时间(因为 C 需要更多的努力来做同样的事情)。
Wrong again. If PHP libraries have been written in C then they are directly usable from C -instantly providing the 'unique productivity' you are claiming PHP has.
又错了。如果 PHP 库是用 C 编写的,那么它们可以直接从 C 中使用 - 立即提供您声称 PHP 具有的“独特的生产力”。
That means the reduced hardware need of the C code has to represent 2 years worth of wages for C to become attractive. In practice that (almost) never happens.
这意味着 C 代码减少的硬件需求必须代表 2 年的工资才能使 C 变得有吸引力。在实践中(几乎)永远不会发生。
Pure F.U.D. (see the answer above).
纯 FUD(见上面的答案)。
Facebook's scale is so large that hardware is a big enough cost to care. That's why they developed HipHop, which cross-compiles PHP to C++. It brings the best of both worlds: the simplicity of programming in PHP, and the raw performance of C++. Facebook is still developed in PHP, but when you use it, it's all native code.
Facebook 的规模如此之大,以至于硬件成本足以应付。这就是他们开发 HipHop 的原因,它将 PHP 交叉编译为 C++。它带来了两全其美:PHP 编程的简单性和 C++ 的原始性能。Facebook 仍然是用 PHP 开发的,但是当你使用它时,它都是原生代码。
HipHop is much faster than PHP, no doubts on that. But if you compare HipHop with a plain-C implementation you have two layers of overhead:
HipHop 比 PHP 快得多,这一点毫无疑问。但是,如果将 HipHop 与普通 C 实现进行比较,则会有两层开销:
- the PHP to C++ interfaces (which use the bloated C++ libraries);
- the C++ bloat itself (which makes C++ 2-10 times slower than plain C).
- PHP 到 C++ 的接口(使用臃肿的 C++ 库);
- C++ 膨胀本身(这使得 C++ 比普通 C 慢 2-10 倍)。
Plus, HipHop has been written in the clueless inefficient Academic mode (detached from any real-world reality). sure, it can impress PHP coders but show this code to an embedded programmer and he will feel sorry for Facebook.
另外,HipHop 是在毫无头绪的低效学术模式下编写的(脱离了任何现实世界的现实)。当然,它可以给 PHP 程序员留下深刻印象,但如果将这段代码展示给嵌入式程序员,他会为 Facebook 感到难过。
"A language that doesn't have everything is actually easier to program in than some that do" --Dennis M. Ritchie
“一种没有一切的语言实际上比一些有的语言更容易编程”--Dennis M. Ritchie
Unlike (most of the) programming language END-USERS, Dennis knew a couple of things about the matter, it seems.
与(大多数)编程语言最终用户不同,丹尼斯似乎对这件事了如指掌。

