如果 C# 比 C++ 慢,为什么要使用 C#?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/787375/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 01:09:28  来源:igfitidea点击:

Why would you want to use C# if its slower than C++?

c#c++performance

提问by Gordon Gustafson

I'm looking for a new language to learn after C++ and Java. I was going to try C#, but a bunch of people say its really slow because its a high level language. So why would anybody use C#? Isn't C++ much faster? Does it make development easier, but just have a slower final product?

我正在寻找一种在 C++ 和 Java 之后学习的新语言。我打算尝试 C#,但是很多人说它真的很慢,因为它是一种高级语言。那么为什么会有人使用 C#?C++ 不是更快吗?它是否使开发更容易,但最终产品的速度较慢?

Also, what can C# be used for? You use it with a lot of .NET stuff on windows, and with ASP.NET, but what are other situations where one would use C#? Will there be a lot of job opportunities for it?

另外,C# 可以用来做什么?您将它与 Windows 上的许多 .NET 内容以及 ASP.NET 一起使用,但是还有哪些其他情况会使用 C#?会不会有很多工作机会呢?

采纳答案by Jon Skeet

Who exactly is this "bunch of people"? What are they comparing it against?

这“一群人”究竟是谁?他们拿什么来比较?

For the vast majority of things, C++ is not"much faster" than C#. It certainly has benefits in various situations, particularly where you want more deterministic memory handling, but in my experience the bottleneck in most applications isn't in places where C++ would help. As spoulson says, a lot of performance is in the design instead of the exact implementation - and there, it helps to be able to try different designs easily.

对于绝大多数事情,C++并不比 C#“快得多”。它当然在各种情况下都有好处,特别是在您想要更多确定性内存处理的情况下,但根据我的经验,大多数应用程序中的瓶颈并不在 C++ 可以提​​供帮助的地方。正如 spoulson 所说,很多性能都在设计中,而不是在确切的实现中 - 在那里,能够轻松尝试不同的设计是有帮助的。

Why would we use C# when it's a bitslower than C++? Because it's generally reckoned (i.e. some disagree :) to be a lot easier to develop in without shooting yourself in the foot.

为什么我们要使用C#当它是一个有点比C ++慢?因为人们普遍认为(即有些人不同意 :) 更容易开发而不用脚射击自己。

As for what C# can be used for... what do you wantto use it for? Unless you want to develop drivers and kernels, it may well be fine for you. (Even OS development has some folks using C#...)

至于C#可以用来做什么……你用来做什么?除非您想开发驱动程序和内核,否则它很适合您。(即使是操作系统开发也有一些人使用 C#...)

Job opportunities? Loads.

工作机会?负载。

Downsides? Well, .NET itself is only available on Microsoft platforms. There's Mono, but it doesn't have quite the same degree of portability as Java (no doubt another "slow" language according to the same bunch of people).

缺点?嗯,.NET 本身仅在 Microsoft 平台上可用。有Mono,但它没有与 Java 完全相同程度的可移植性(根据同一群人的说,这无疑是另一种“慢”语言)。

回答by spoulson

Code written in assembly can be blazingly fast. Why not just write in assembly?

用汇编编写的代码可以非常快。为什么不直接用汇编写?

Don't believe everything you hear. C# has been plenty fast for all my projects. Typically, performance is more a factor of design than raw platform performance.

不要相信你听到的一切。对于我的所有项目,C# 已经足够快了。通常,性能比原始平台性能更多地是设计因素。

回答by Reed Copsey

C# is not always slower - in many cases, it can perform just as well as any language you listed. Usually the algorithm has more to do with the speed than the choice of language.

C# 并不总是较慢 - 在许多情况下,它的性能与您列出的任何语言一样好。通常算更多地与速度有关,而不是语言的选择。

However, C# is very expressive, and has a great base class library to work with, and super-fast compilation. This means that it's very easy to work with, and can allow you to be much more productive than many other languages, especially C++. For example, I just had a small project that I would have budgeted 1 man-week for in C++, and we finished it up in less than a day in C#, mainly because the base class libraries simplified so many of the tasks.

但是,C# 非常具有表现力,并且有一个很棒的基类库可以使用,并且编译速度非常快。这意味着它非常易于使用,并且可以让您比许多其他语言(尤其是 C++)更有效率。例如,我刚刚有一个小项目,我在 C++ 中预算为 1 人周,而我们在 C# 中不到一天就完成了,主要是因为基类库简化了很多任务。

回答by JaredPar

I'd have to say the people you were talking to simply don't know what they're talking about. Plain and simple.

我不得不说,你与之交谈的人根本不知道他们在说什么。干净利落。

Many enterprise level applications are built on top of C# and other .Net languages. There is nothing inherently slow about them. Yes the tendto have slower startup times but that's pretty much where it ends.

许多企业级应用程序建立在 C# 和其他 .Net 语言之上。它们本身并没有什么缓慢的地方。是的,启动时间往往较慢,但这几乎就是它结束的地方。

I noticed you mentioned Java in the list of languages. If you're comfortable with the speed of Java, C# will not present any issues. Generally speaking C# performs at least as well as Java on many different types of bench marks.

我注意到你在语言列表中提到了 Java。如果您对 Java 的速度感到满意,那么 C# 不会出现任何问题。一般来说,在许多不同类型的基准测试中,C# 的性能至少与 Java 一样好。

回答by Frederik Gheysels

Why should it be slow ? Indeed, C# is compiled to 'Intermediate Language', which is JIT'ed at runtime, but this can give you a performance advantage, since the runtime can generate the most optimized for the platform the code is running on ...

为什么要慢?事实上,C# 被编译为“中间语言”,它在运行时被 JIT,但这可以给你带来性能优势,因为运行时可以为代码运行的平台生成最优化的......

Depending on the application that you want to write, the 'speed' of the language will have a minor impact. The performance of your application will mostly be determined by the way you design your application, if you make good uses of the tools / technologies you use , etc ... Sure, C# is not a silver bullet, and there are projects where you shouldn't use it, simply because it is not the right tool for the job, but it will do just fine for most of the business / enterprise app's.

根据您要编写的应用程序,语言的“速度”会产生轻微影响。您的应用程序的性能主要取决于您设计应用程序的方式,是否充分利用了您使用的工具/技术等......当然,C# 不是灵丹妙药,有些项目您应该不要使用它,仅仅因为它不是适合这项工作的工具,但它对于大多数商业/企业应用程序来说都很好。

回答by Dinah

My last company was founded by 5 C++ veterans with 15+ years experience each. They spent over a month building a certain Windows service. One of them found and dabbled in C#. Within a week he'd gotten further than the collective had in their month. Shortly after, they all switched to C#.

我的最后一家公司是由 5 位 C++ 资深人士创立的,每个人都有 15 年以上的经验。他们花了一个多月的时间来构建某个 Windows 服务。其中一位发现并涉足了 C#。一周之内,他比整个月的集体进步得更远。不久之后,他们都转向了 C#。

Why C# if it may perform slower: what price do you put on that degree of rapid development?

如果 C# 可能执行得更慢,那么为什么它:您为这种程度的快速开发付出了什么代价?

回答by SQLMenace

one big reason to use C# over C++.........look at all those buffer overflow problems that C++ has where people take over your machine because they inject executable code in your strings or memory leaks....garbage collector is quite handy IMHO

使用 C# 而不是 C++ 的一个重要原因......看看 C++ 存在的所有缓冲区溢出问题,人们接管了你的机器,因为他们在你的字符串中注入了可执行代码或内存泄漏......垃圾收集器非常方便恕我直言

回答by Lauri Larjo

Even if C++ is faster to run, the difference isn't big on today's computers. That's why on mobile devices where cpu and memory are rather limited, C/C++ is still widely used.

即使 C++ 运行速度更快,但在今天的计算机上差别不大。这就是为什么在 cpu 和内存相当有限的移动设备上,C/C++ 仍然被广泛使用的原因。

But think about development times instead. If it takes 2 months using C++ and 1 month using C#, which would you go for? And when big modifications/refactoring need to be done, a higher level language makes it significantly faster and easier!

但请考虑开发时间。如果使用 C++ 需要 2 个月,使用 C# 需要 1 个月,你会选择哪个?当需要进行大的修改/重构时,更高级的语言会使其变得更快、更容易!

回答by Tracker1

I haven't found many instances where C# isn't a good choice of languages, and .Net (or Mono) isn't a decent platform. Notable exceptions being kernel level development, or drivers. There are plenty of areas where low level, and raw performance is needed. For most, if no all business, or Enterprise applications C# is one of the better choices for development. It's well supported, works with many other systems, libraries, communications channels and components already available not to mention being a fairly nice language (esp. 3.5) to work with.

我还没有发现 C# 不是一个好的语言选择,并且 .Net(或 Mono)不是一个像样的平台的例子。值得注意的例外是内核级开发或驱动程序。有很多领域需要低级别和原始性能。对于大多数业务或企业应用程序来说,C# 是更好的开发选择之一。它得到了很好的支持,可以与许多其他系统、库、通信渠道和已经可用的组件一起使用,更不用说它是一种相当不错的语言(尤其是 3.5)。

ASP.Net wasn't a bad platform, I generally find the object/control stack to be one of the short comings for complex interactions. I think ASP.Net MVC fits better for more scalable web based applications. Just the same, it's better than many other systems I've worked with in the past.

ASP.Net 不是一个糟糕的平台,我通常发现对象/控件堆栈是复杂交互的缺点之一。我认为 ASP.Net MVC 更适合更具可扩展性的基于 Web 的应用程序。同样,它比我过去使用过的许多其他系统都要好。

In terms of service layers, and even GUI development it's pretty nice. I've got a lot more experience in web based applications, and service/communications/business layers than with desktop GUI applications, so can't comment much there. I feel a lot of GUI development is more about the IDE/Toolkit, than the particular language.

在服务层,甚至 GUI 开发方面,它都非常好。与桌面 GUI 应用程序相比,我在基于 Web 的应用程序和服务/通信/业务层方面的经验要多得多,因此无发表太多评论。我觉得很多 GUI 开发更多的是关于 IDE/Toolkit,而不是特定的语言。

As to slow, you specifically list Java, in most instances, C#/.Net is as fast or faster than Java. IMHO development specifically goes smoother with C# (Visual Studio) over Java (Eclipse). For web-based apps, I like ASP.Net MVC (and even ASP.Net) over Swing. That's just me though.

至于慢,你专门列出Java,在大多数情况下,C#/.Net 与Java 一样快或更快。恕我直言,使用 C# (Visual Studio) 比 Java (Eclipse) 开发更顺畅。对于基于 Web 的应用程序,我喜欢 ASP.Net MVC(甚至 ASP.Net)而不是 Swing。不过那只是我。

回答by Adam Jaskiewicz

The people you talked to don't know what they are talking about. C# is a very similar language to Java, all told; it has most of the same benefits and drawbacks. The way it all works is pretty similar (Java/C# is compiled into an intermediate language/bytecode that is interpreted or JIT compiled to native code, with various similar optimizations that you don't need to worry about as a programmer). It's used in a lot of the same situations as Java, and is really aimed for the same market. It's moving a lot faster and bringing in a lot of innovation as a language, but it's (in practice) pretty much Windows-only, if that's a concern of yours. The job market is similar. Both are very popular languages.

与您交谈的人不知道他们在说什么。总而言之,C# 是一种与 Java 非常相似的语言;它具有大部分相同的优点和缺点。它的工作方式非常相似(Java/C# 被编译成中间语言/字节码,然后被解释或 JIT 编译成本机代码,并进行了各种类似的优化,作为程序员无需担心)。它在很多与 Java 相同的情况下使用,并且确实针对相同的市场。它的发展速度要快得多,并且作为一种语言引入了很多创新,但它(实际上)几乎仅适用于 Windows,如果这是您的问题的话。就业市场也类似。两者都是非常流行的语言。

As for a language to learn, I would suggest something DIFFERENT. You say you know C++ and Java; C# shouldn't be hard to pick up. Potential employers will know this. Try Scala or Python. Both will give you some new perspectives on things (C# not so much), and make you a better programmer by teaching you new ways to think, rather than just adding another tool to your box.

至于要学习的语言,我会建议一些不同的东西。你说你知道 C++ 和 Java;C# 应该不难上手。潜在的雇主会知道这一点。试试 Scala 或 Python。两者都会给你一些关于事物的新观点(C# 不是很多),并通过教你新的思考方式让你成为更好的程序员,而不仅仅是向你的盒子添加另一个工具。