C++与Python在特定问题上的比较

huangapple go评论103阅读模式
英文:

C++ vs Python on a specific issue

问题

我在这个网站上看到了很多关于比较C++和Python的辩论,但我想稍微改变一下问题的角度。在性能方面,这两种语言是否有相等的应用场景呢?

我正在编写一个小应用程序,它建立一个套接字连接,读取传入的消息(大约每秒5000条),对每一行进行一些解析,然后将数据记录到文本文件中。我曾经让一个开发人员用C++编写了一个程序,它运行得很好。然而,我想在原始想法的基础上自己扩展,而不依赖那个开发人员。我尝试学习C++,但感觉有些困难。我也尝试学习Python,感觉对我来说更加“合适”。我的问题是,对于那些吹嘘C++在性能方面优于Python的文章,是否有可能在我刚刚描述的特定需求下,Python能够像C++一样胜任工作呢?

另外,我也了解了一下Go语言,它看起来是一个有趣的中间选择。它是一种编译语言,语法更容易上手。这是否可以作为一种替代方案呢?

谢谢。

英文:

I have seen many debates on this site which compare C++ to Python however I wanted to try to spin the question a little. Are there any processes where the two are equal in performance?

I am writing a small application that makes a socket connection, reads the incoming messages (about 5000 per second) does some parsing on each line and then logs the data to a text file. I had a developer build a C++ program to do this and it works quite well. However, I'd like to expand upon the original idea myself, without said developer. I have tried learning C++ and it just feels over my head. I have tried learning python and it feels "right" to me. My question is, for all the articles that boast C++ performance over Python, is it possible that, based on my particular needs that I just described, Python would do the job just as fine as C++.

On a separate note, I have looked into golang and it looks like an interesting middle ground. Compiled language with easier syntax to pick up. Would this be an alternative?

Thanks.

答案1

得分: 2

Python通常比C++慢,但它并不像大多数人一开始认为的那样慢——多年来已经进行了一些优化,并且还有一些技巧可以使用。有一些资源可以帮助你在Python中提高速度,并尽可能地使其变得更快。

最终,要确定是否可以通过比较获得可接受的速度,唯一的方法就是实际制作你所说的程序,并进行测试。为每种语言找一个好的代码分析器,并将它们的结果进行比较,以了解它们相对于彼此的表现如何。

英文:

Python is, in general, definitively slower than C++, but it's not as slow as most people think at first - there have been a number of optimizations over the years, and there are also tricks you can do. There are some resources out there that can help you squeeze that extra speed out of Python, and really try to get it to be as fast as possible.

In the end, the only real way to find out if you can get acceptable speed by comparison is to actually make the program you speak of, and test it. Find a good code profiler for each language and compare their results to see exactly how well each does relative to each other.

huangapple
  • 本文由 发表于 2013年10月1日 21:41:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/19117777.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定