结合 C# 和 C 的优点/缺点

发布于 2024-12-09 16:41:53 字数 1437 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

从﹋此江山别 2024-12-16 16:41:53

和 C 来提高性能

您可以使用纯托管代码编写性能良好的应用程序。以同一个网站为例。这真是太快了,你不觉得吗?

我的看法如下:只有当你真的是某种性能狂人(试图解决 Google 所做的扩展问题的人)或者你有一些现有的 C 代码库但你无法使用时,才混合这两种技术立即移植到 .NET 并且您必须与之进行互操作。

在其他情况下,托管代码更安全。如果您对性能感到厌倦,请不要忘记,在许多情况下,托管和非托管代码之间的封送成本将高于从纯非托管代码中获得的性能。此外,JITter 在框架的每个版本中都变得越来越智能,也许有一天它会生成几乎与非托管代码一样高效的代码。

所以,是的,选择托管,把 C 留给疯子:-)

and C to increase performance

You could write pretty well performing applications using pure managed code. Take for example this very same site. It's pretty darn fast, don't you think?

The way I see things is the following: mix those two technologies only if you are really some kind of a performance maniac (someone that tries to solve the scaling problems that Google does for example) or if you have some existing C codebase that you cannot port to .NET immediately and that you have to interoperate with.

In other cases, managed code is safer. If you are sick about performance don't forget that in many cases the cost of marshaling between managed and unmanaged code will be higher than the performance you would gain from pure unmanaged code. Also the JITter gets smarter and smarter with each version of the framework and maybe one day it will generate almost as efficient code as unmanaged code.

So, yeah, go for managed and leave C to the maniacs :-)

⊕婉儿 2024-12-16 16:41:53

对于您要做的事情,我不太确定 C 会比 C# 更快。即时代码非常快。此外,如果您正在进行大量计算,通过使用 c#/.net,您可能会利用任务并行库,这将大大简化计算的并行化。事实上,对于所有多核机器,我认为如果您可以利用多核,您将获得更多收益(当然,直接 C 也可以线程,但 .net 中的 TPL 4 比基本 WinApi 生产力更高)

您可能会考虑的另一件事是,我是根据我自己的经验来发言的,所以一定要进行自己的研究:我听说 F# 是编写科学论文的一个非常好的选择集/计算库。我自己没有写过一行 F#,但据我了解,该语言设计支持以一种使计算高度可并行化的方式编写计算。

(当然,现在你有一个新问题——学习 F# ;)

这里有一篇文章你可以查看:科学计算中的 F# 性能

I would not be so sure that C will be faster than C# for what you are going to do. The jitted code is extremely fast. Also, if you are doing lots of calculations, by using c#/.net, you could potentially take advantage of the Task Parallel Library, which would greatly ease parallelizing your calculations. And really, with all of the multi-core machines, I think that you will get a lot more bang for your bucks if you can take advantage of multiple cores (of course, straight C can thread as well, but the TPL in .net 4 is much more productive than the base WinApi)

One other thing that you might consider, and I am speaking from outside of my own experience, so definitely do your own research: I have heard that F# can be a very good choice for writing scientific sets/calculation libraries. I haven't written a line of F# myself, but from what I understand, the language design supports writing the calculations in a way that makes them extremely parallelizable.

(of course, now you have a new problem -- learning F# ;)

there is a post here that you might check out: F# performance in scientific computing

嘿看小鸭子会跑 2024-12-16 16:41:53

获取固定的内存位置以及将索引转换为 C 指针可能会产生负面影响。您确实可以尝试安全和不安全的代码,并尝试查看结果,但我的猜测是安全的代码最终会表现得更好。当然,你的计算也是重要因素。

Getting fixed memory locations, and also converting your indexes to C pointers could have negative side effects. You can try both safe and unsafe code for sure and try to see the results but my guess is that safe code will perform better at the end. For sure, your computation is also important factor.

相守太难 2024-12-16 16:41:53

我过去曾这样做过,我想说,正如其他人提到的,编组成本不是免费的,因此在这是一个有效的选项之前,您需要一些非常专业的需求。

我给你的唯一建议是从最适合的语言开始,如果你想拥有托管代码和 C# 提供的其他功能,那就是 C#。然后,如果性能是一个问题,请在权衡利弊后在 C 中添加一些代码并仔细进行一些分析和优化,并考虑是否可以通过重新设计高级算法而不是在较低级别进行优化来获得一些优化。

例如,如果您要编写科学代码,您将使代码成为多线程的,或者在尝试其他类型的较低级别优化之前检查是否无法重用以前的计算,这将对您的效率产生更大的影响,而不是追逐微小的问题。

I have done this in the past, I would say that as other mentioned the cost of marshalling is not free so you need some pretty specialised need before this is a valid option.

My only advice to you would be to start in the most adapted language, which would be C# if you want to have managed code and other features that C# provide. And then if performance is an issue and do some profiling and optimise carefully by adding some code in C after weighting the pro/cons and considering whether you can get some optimisation by reworking the high level algorithm rather than optimising at a lower level.

For example, if you were to write scientific code, you would make your code multithreadable or checking that you cannot reuse previous computations before to attempt other kind of lower level optimisation, this will have a much larger impact on your efficiency rather than chasing tiny problems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文