lbk60ox 发表于 2024-8-17 09:17:21

势头强劲的 Python PK 强大的 C++,到底谁更胜一筹?


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">始终</span><span style="color: black;">败兴</span>,在 TIOBE 编程语言排行榜中,简单易用的新贵 Python 和老将 C++ 是强劲的竞争对手,<span style="color: black;">不外</span> C++ 和 Python 都是非常流行的编程语言,<span style="color: black;">针对</span><span style="color: black;">研发</span>者而言,在<span style="color: black;">选取</span>语言利器时<span style="color: black;">到底</span>有何参考标准?本文将基于两种语言的关键性能对比,带领<span style="color: black;">大众</span>深入不<span style="color: black;">同样</span>的 Python 与 C++。</p><img src="https://p3-sign.toutiaoimg.com/pgc-image/RaRYYDGBDZH8Xd~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723889270&amp;x-signature=Afh7Bzy%2FNYAyBIcvRaVak7bmzdY%3D" style="width: 50%; margin-bottom: 20px;">
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">作者 | Farhad Malik</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">译者 | 弯月,责编 | 屠敏</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">以下为译文:</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在编程生涯的<span style="color: black;">初期</span><span style="color: black;">周期</span>,我参与过一款C++数学优化应用程序的<span style="color: black;">研发</span>,这个程序对性能的<span style="color: black;">需求</span>很高。<span style="color: black;">迄今</span>我依然记得那段艰难的经历。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在那个项目中,我遇到了<span style="color: black;">有些</span>概念,<span style="color: black;">例如</span>指针、并发和垃圾回收,刚<span style="color: black;">起始</span>的时候这些概念非常难以理解。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">然而,<span style="color: black;">倘若</span>你问我对C++的看法,<span style="color: black;">那样</span>我会很高兴地向想要构建实时应用程序的人<span style="color: black;">举荐</span>C++。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">C++是一门非常强大的编程语言。</p><img src="https://p3-sign.toutiaoimg.com/pgc-image/RpF3amW8cmtQYg~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723889270&amp;x-signature=jOX6Lo2CCqcanpDs1j12ELpUQUQ%3D" style="width: 50%; margin-bottom: 20px;">
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">近期</span>,我<span style="color: black;">始终</span>在从事<span style="color: black;">相关</span><span style="color: black;">设备</span>学习和数据科学<span style="color: black;">处理</span><span style="color: black;">方法</span>的架构和实现工作。我<span style="color: black;">选取</span>的语言是Python。我承认<span style="color: black;">亦</span>很欣赏这种语言的简单性和强大性。有些程序员认为Python是一种原型编程语言,当然<span style="color: black;">她们</span>有自己的正当理由。<span style="color: black;">然则</span>,我会毫不犹豫地向想要构建数据分析应用程序的人<span style="color: black;">举荐</span>Python。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Python是对<span style="color: black;">研发</span>人员非常友好的编程语言。</p><img src="https://p3-sign.toutiaoimg.com/pgc-image/RpF3amxBvxuZmy~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723889270&amp;x-signature=SXv7z39SlhA%2FmH9KEVriNayeC0E%3D" style="width: 50%; margin-bottom: 20px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/RT4Gwk56bt5iOJ~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723889270&amp;x-signature=VjkiK%2FgPjM3%2Bk8yBxGfu%2F6k9GnA%3D" style="width: 50%; margin-bottom: 20px;">
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">本文的目的</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">C++和Python都是非常流行的编程语言。本文将重点介绍两种语言之间的<span style="color: black;">类似</span>点和差异性,以便<span style="color: black;">咱们</span>理解何时<span style="color: black;">运用</span>哪种语言。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这两种语言有<span style="color: black;">非常多</span>差异和<span style="color: black;">类似</span>之处,但本文只介绍<span style="color: black;">咱们</span><span style="color: black;">必要</span><span style="color: black;">认识</span>的关键性功能。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">首要</span>,两种编程语言有<span style="color: black;">非常多</span><span style="color: black;">类似</span>之处,<span style="color: black;">因此</span>学习起来<span style="color: black;">亦</span>不难。</p><img src="https://p3-sign.toutiaoimg.com/pgc-image/RpF3bO5FtNFsH3~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723889270&amp;x-signature=UjmEM3J1aaiAfvwgohThxz%2FqPA0%3D" style="width: 50%; margin-bottom: 20px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/RT7S2kzFTfre26~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723889270&amp;x-signature=SbVHoxQstCgzPrbbH1Bj%2FPnT860%3D" style="width: 50%; margin-bottom: 20px;">
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">Python是解释型语言,而C++不是</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">C++的工作方式是,<span style="color: black;">首要</span>将写好的代码<span style="color: black;">保留</span>到扩展名为.cpp的文件中。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">而后</span>编译.cpp文件。编译器将C++代码转换为原生代码。<span style="color: black;">而后</span>执行这些<span style="color: black;">设备</span>代码。<span style="color: black;">因此呢</span>,C++非常靠近硬件。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">因此</span>,C++的速度非常快,非常适合实时应用程序。关键在于C++应用是“原生”应用,这<span style="color: black;">显示</span>编译器是依赖操作系统和处理器的组件。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">编译器依赖<span style="color: black;">设备</span>,<span style="color: black;">因此呢</span>C++<span style="color: black;">亦</span>依赖<span style="color: black;">设备</span>。 </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">然则</span>,这<span style="color: black;">寓意</span>着代码只能在与编译代码的编译器相兼容的操作系统(和处理器)上运行。<span style="color: black;">详细</span><span style="color: black;">来讲</span>:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>在Windows<span style="color: black;">设备</span>上用Windows编译器来编译代码,则会生成Windows<span style="color: black;">设备</span>代码。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span><span style="color: black;">运用</span>Linux编译器来编译cpp代码,则编译后的代码只能在Linux<span style="color: black;">设备</span>上运行。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">有时,有些C++模块/软件包并<span style="color: black;">不可</span>与所有操作系统兼容。</p><img src="https://p3-sign.toutiaoimg.com/pgc-image/RpF3bOksuV8zc~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723889270&amp;x-signature=8zNWK7kGsy4OZ5xbfAQuq7%2F7SzU%3D" style="width: 50%; margin-bottom: 20px;">
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">另一方面,Python是一种解释语言。Python的工作方式是,<span style="color: black;">首要</span>在安装Python软件包的位置创建一个虚拟机。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">而后</span>将写好的python代码<span style="color: black;">保留</span>到.py文件中。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">而后</span>再<span style="color: black;">经过</span>CPython,将Python代码编译为Python虚拟机的字节码。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">那样</span>,这个Python虚拟机就像C++<span style="color: black;">同样</span>是依赖于<span style="color: black;">设备</span>的,<span style="color: black;">然则</span>Python代码却不是。 </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">当你想执行字节码时,这些代码就会在运行时被解释。<span style="color: black;">因此呢</span>Python不依赖于<span style="color: black;">设备</span>。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Python字节码依赖于Python虚拟机,而Python不依赖于<span style="color: black;">设备</span>。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">必须</span><span style="color: black;">重视</span>的一点是,<span style="color: black;">咱们</span><span style="color: black;">能够</span>在一个操作系统中编写Python代码,<span style="color: black;">而后</span>将其复制到另一个操作系统中,就<span style="color: black;">能够</span>运行。但C++不行。<span style="color: black;">咱们</span><span style="color: black;">必要</span>在<span style="color: black;">每一个</span>OS上编译C++代码。正是这个<span style="color: black;">原由</span>,与Python相比,C++的运行速度非常快。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">因为</span>Python是一种解释型语言,<span style="color: black;">因此呢</span>它的运行速度比C++慢。</p><img src="https://p3-sign.toutiaoimg.com/pgc-image/RpF3bP9HUqfBTU~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723889270&amp;x-signature=yHdf%2FnrG8L8qI40xCeSzV%2F%2FwuZg%3D" style="width: 50%; margin-bottom: 20px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/RTJXJ1kBqzfCnu~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723889270&amp;x-signature=8KmleHynFY7vYHjU5Wug31XHxcE%3D" style="width: 50%; margin-bottom: 20px;">
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">编程约定</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">首要</span>,<span style="color: black;">咱们</span>来看一看易用性。</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Python是一种高级语言,而C++是一种低级语言。Python<span style="color: black;">拥有</span>可读性、简单、直截了当,<span style="color: black;">况且</span>学习起来很简单。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这是一大<span style="color: black;">优良</span>,<span style="color: black;">由于</span>这<span style="color: black;">寓意</span>着采用Python的<span style="color: black;">研发</span>人员越来越多。<span style="color: black;">况且</span>这个特性还<span style="color: black;">能够</span><span style="color: black;">快速</span>地将Python应用程序推向市场。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">因为</span>易用性和<span style="color: black;">海量</span>的库,Python<span style="color: black;">作为</span>了数据科学和<span style="color: black;">设备</span>学习项目的首选编程语言,<span style="color: black;">况且</span>在Web<span style="color: black;">研发</span>中<span style="color: black;">亦</span>越来越受欢迎。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Python非常适合于测试新概念和想法。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">然而,有时<span style="color: black;">亦</span><span style="color: black;">由于</span>这个<span style="color: black;">原由</span><span style="color: black;">引起</span>程序员将Python视为一种原型语言。但话又说回来,这种观点的理由<span style="color: black;">亦</span>很充分。在实现Python时,<span style="color: black;">咱们</span>始终应该遵循最佳的软件原理和实践。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">针对</span>刚接触编程<span style="color: black;">行业</span>的人<span style="color: black;">来讲</span>,Python是一门伟大的语言。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">另一方面,C++起源于C语言。它是一种功能强大的语言,<span style="color: black;">因此呢</span>很<span style="color: black;">繁杂</span>,<span style="color: black;">由于</span>它<span style="color: black;">能够</span>执行<span style="color: black;">非常多</span>低级任务。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">研发</span>人员<span style="color: black;">必须</span>遵守C++中<span style="color: black;">海量</span>的约定和规则。</p><img src="https://p3-sign.toutiaoimg.com/pgc-image/RpF3bqMJE76JO7~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723889270&amp;x-signature=W4MWHma6TIAnUsVrYFsXb38x80I%3D" style="width: 50%; margin-bottom: 20px;">
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">C++广泛用于游戏<span style="color: black;">研发</span>、后端服务器应用程序以及分布式交易应用程序,这些系统都<span style="color: black;">需求</span>快速执行任务。<span style="color: black;">另外</span>,C++<span style="color: black;">拥有</span>极强的可移植性。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">下面,<span style="color: black;">咱们</span>来谈谈语法。</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1. Python编程<span style="color: black;">非常</span>依赖于空白和制表符(缩进)。一切都是对象。所有类的属性和<span style="color: black;">办法</span>都是公有的。在C++中<span style="color: black;">咱们</span><span style="color: black;">能够</span>利用<span style="color: black;">拜访</span>修饰符<span style="color: black;">隐匿</span>类的属性/<span style="color: black;">办法</span>,Python却做不到。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">我感觉刚<span style="color: black;">起始</span>的时候很难适应Python的缩进和格式。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2. 我想,大<span style="color: black;">都数</span><span style="color: black;">研发</span>人员都<span style="color: black;">晓得</span>Python中<span style="color: black;">无</span>分号或大括号。你<span style="color: black;">能够</span>编写整洁的列表推导。Python中的布尔表达式<span style="color: black;">亦</span><span style="color: black;">无</span>括号。另一方面,C++非常依赖于括号和分号。适应C++的语法<span style="color: black;">必须</span><span style="color: black;">必定</span>的时间。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3. C++是一种强类型语言,<span style="color: black;">每一个</span>变量的类型<span style="color: black;">必须</span>预先声明。而Python是动态类型,<span style="color: black;">咱们</span>不<span style="color: black;">必须</span>指出对象的类型。这种动态特性常常会让<span style="color: black;">咱们</span>遇到意想不到的结果。Python程序员<span style="color: black;">必须</span><span style="color: black;">保准</span>以正确的方式或在正确的上下文中<span style="color: black;">运用</span>对象,<span style="color: black;">因此呢</span>既有积极的一面,<span style="color: black;">亦</span>有消极的一面。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在执行Python代码时,<span style="color: black;">咱们</span>会遇到运行时错误。<span style="color: black;">倘若</span>在生产运行时中遇到类型错误,就很尴尬了!</p><img src="https://p3-sign.toutiaoimg.com/pgc-image/RpF3bqx2CJKmXZ~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723889270&amp;x-signature=LH49Llbhya4beGasxHDN1ssEYz8%3D" style="width: 50%; margin-bottom: 20px;">
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">值得一提的是,为了实现<span style="color: black;">能够</span>在运行之前就<span style="color: black;">发掘</span>问题的Python<span style="color: black;">处理</span><span style="color: black;">方法</span>,许多<span style="color: black;">研发</span>人员花费了<span style="color: black;">海量</span>时间和精力。</p><img src="https://p3-sign.toutiaoimg.com/pgc-image/RTJXJ7YR5xGDl~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723889270&amp;x-signature=eloKHsFx1dDnfjH%2F8BqsifVbfRQ%3D" style="width: 50%; margin-bottom: 20px;">
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">内存管理和并发 </strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在介绍管理内存的方式之前,<span style="color: black;">首要</span><span style="color: black;">咱们</span>应该<span style="color: black;">重视</span>,这两种编程语言都支持面向对象的编程和继承。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">当程序员在程序中创建对象时,这些对象会占用内存。当不<span style="color: black;">运用</span>对象时,程序将<span style="color: black;">经过</span>垃圾回收清除掉这个垃圾。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">垃圾回收的方式工作是不确定的。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">做为</span>Python程序员,<span style="color: black;">咱们</span>无需担心<span style="color: black;">怎样</span>以及何时地清除内存。Python会<span style="color: black;">经过</span>智能垃圾回收自动为<span style="color: black;">咱们</span>处理内存。C++中<span style="color: black;">无</span><span style="color: black;">这般</span>的垃圾回收,你<span style="color: black;">必要</span>自己动手管理内存。</p><img src="https://p3-sign.toutiaoimg.com/pgc-image/RpF3brjjBKZ5n~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723889270&amp;x-signature=qfurEwsdpOgCDn1a9wJ%2FBqRxvyo%3D" style="width: 50%; margin-bottom: 20px;">
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">C++中的内存管理<span style="color: black;">必须</span>手动完成。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">理由很充分。C++的设计主旨<span style="color: black;">便是</span>打造一种高性能的编程语言。负责管理内存并从内存中删除不<span style="color: black;">运用</span>对象的垃圾回收会影响应用程序的性能。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">最重要的是,垃圾回收在本质上是不确定的。你<span style="color: black;">没法</span><span style="color: black;">保准</span>对象删除后<span style="color: black;">是不是</span>仍在内存中。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Python是一种对<span style="color: black;">研发</span>人员非常友好的编程语言,<span style="color: black;">由于</span><span style="color: black;">咱们</span>不必担心忘记释放内存。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">并发</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">针对</span>受I/O或CPU约束的应用程序,程序员常常<span style="color: black;">爱好</span>在代码中创建多个线程,并<span style="color: black;">经过</span><span style="color: black;">同期</span>运行这些线程加快计算的速度。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">不管<span style="color: black;">咱们</span>的计算机中有多少个核心,Python只<span style="color: black;">准许</span><span style="color: black;">咱们</span>一次执行一个线程。这是Python的全局解释器<span style="color: black;">导致</span>的限制。这会给<span style="color: black;">必须</span>多个CPU的应用程序<span style="color: black;">导致</span>问题。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">然则</span>,<span style="color: black;">咱们</span><span style="color: black;">能够</span>在Python中创建多个进程。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">另一方面,C++<span style="color: black;">准许</span><span style="color: black;">咱们</span>实现多线程应用程序。</p><img src="https://p3-sign.toutiaoimg.com/pgc-image/RpF3bsB21mtRRc~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723889270&amp;x-signature=7hhz%2FyCe9hKTlKufdfRbpwrTUCA%3D" style="width: 50%; margin-bottom: 20px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/RTJXJ7uHXU5GZc~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723889270&amp;x-signature=8sYONpXrtQZLPy%2FaoKkMNtQCZb4%3D" style="width: 50%; margin-bottom: 20px;">
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">C++有指针,但Python<span style="color: black;">无</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">谈论C++,怎么能不提指针?</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Python中<span style="color: black;">无</span>指针,<span style="color: black;">最少</span><span style="color: black;">无</span>开箱即用的指针。<span style="color: black;">然则</span>,C++中有指针。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">首先,让<span style="color: black;">咱们</span>来解释一下指针是什么。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">假设有一个变量。这个变量是一个整数,值设置为5。<span style="color: black;">那样</span>,变量与它的值是一个意思,<span style="color: black;">例如</span>x = 5,x是变量的名<span style="color: black;">叫作</span>,值为5。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在C++中,变量是<span style="color: black;">经过</span>值传递给函数的。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">那样</span>问题在于,这些值<span style="color: black;">保留</span>在哪里?</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">变量的<span style="color: black;">每一个</span>值都<span style="color: black;">保留</span>在计算机的内存<span style="color: black;">位置</span>中。你<span style="color: black;">能够</span><span style="color: black;">运用</span>C++的运算符&amp;<span style="color: black;">拜访</span>变量的内存<span style="color: black;">位置</span>。指针是一个变量,指向了值所在的内存<span style="color: black;">位置</span>。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">指针能够<span style="color: black;">加强</span>程序的效率。 </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在标识符之前<span style="color: black;">运用</span>运算符*,就<span style="color: black;">能够</span>声明一个指针。如下所示:</p><span style="color: black;">int</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">* y = &amp;x</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">以上</span>,<span style="color: black;">咱们</span>创建了一个指针变量y,这个变量<span style="color: black;">保留</span>了变量x的内存<span style="color: black;">位置</span>。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">假设你有一个函数接受变量<span style="color: black;">做为</span>输入(参数)。这个值的变更<span style="color: black;">寓意</span>着在函数内部创建一个新变量。请记住,这在C++中会占用<span style="color: black;">海量</span>的内存,变量是<span style="color: black;">经过</span>值传递给函数的。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">详细</span><span style="color: black;">来讲</span>,<span style="color: black;">首要</span><span style="color: black;">咱们</span>创建一个将传入变量加100的函数。</p><span style="color: black;"><span style="color: black;">void</span> <span style="color: black;">a</span></span><span style="color: black;"><span style="color: black;">dd</span></span><span style="color: black;"><span style="color: black;">_hundred</span><span style="color: black;">(<span style="color: black;">int</span> x)</span> </span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>x += <span style="color: black;">100</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>}<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这段代码接受一个变量x,<span style="color: black;">而后</span>将x加100。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在函数内部,任何<span style="color: black;">相关</span>这个值的修改都会针对一个新变量,而不是原来的变量。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">调用<span style="color: black;">以上</span>代码的主函数如下:</p><span style="color: black;"><span style="color: black;">int</span> <span style="color: black;">main</span><span style="color: black;">(<span style="color: black;">void</span>)</span> </span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p><span style="color: black;">int</span> x = <span style="color: black;">2337</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">add_hundred(x);</p><span style="color: black;">return</span> <span style="color: black;">0</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>}<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">即使在执行完函数之后,在函数<span style="color: black;">外边</span>,x的值仍然是2337。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">那样</span>,<span style="color: black;">为何</span><span style="color: black;">咱们</span><span style="color: black;">必须</span>指针? </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">为了理解这一点,你<span style="color: black;">必要</span>明白值和引用类型之间的区别。值类型就<span style="color: black;">好似</span>每次都从<span style="color: black;">理学</span>上复制变量并赋给它一个新变量。值类型占用的空间<span style="color: black;">更加多</span>,<span style="color: black;">况且</span><span style="color: black;">每一个</span>变量都有自己的内存<span style="color: black;">位置</span>。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span><span style="color: black;">能够</span>修改<span style="color: black;">以上</span>函数,让它接受指针<span style="color: black;">做为</span>参数。<span style="color: black;">这般</span>的修改<span style="color: black;">能够</span>减少程序占用的内存量。<span style="color: black;">况且</span>还不会创建不必要的重复变量。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">如下函数<span style="color: black;">能够</span>直接修改变量的值。 </p><span style="color: black;"><span style="color: black;">void</span> <span style="color: black;">a</span></span><span style="color: black;"><span style="color: black;">dd</span></span><span style="color: black;"><span style="color: black;">_hundred</span>(<span style="color: black;"><span style="color: black;">int</span> *x</span>)</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> {</p>*x += <span style="color: black;">100</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>}<span style="color: black;"><span style="color: black;">int</span> <span style="color: black;">main</span>(<span style="color: black;"><span style="color: black;">void</span></span>)</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> {</p><span style="color: black;">int</span>x =<span style="color: black;">2337</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p><span style="color: black;">int</span> *y = &amp;<span style="color: black;">x <span style="color: black;">a</span></span><span style="color: black;"><span style="color: black;">dd</span></span><span style="color: black;"><span style="color: black;">_hundred</span>(<span style="color: black;">y</span>)</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p><span style="color: black;">return</span> <span style="color: black;">0</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>}<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">下面,<span style="color: black;">咱们</span>创建了一个指针y,并传递给一个函数,而这个函数会将输入参数x加100,即使<span style="color: black;">位置于</span>函数<span style="color: black;">外边</span>的x值<span style="color: black;">亦</span>会<span style="color: black;">出现</span>变化。<span style="color: black;">亦</span><span style="color: black;">便是</span>说,在函数执行完成后,x的值为2437。</p><img src="https://p3-sign.toutiaoimg.com/pgc-image/RpF3cSBBRLMdtl~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723889270&amp;x-signature=DZ6CLCMfchoq20hY0BnyVKNW7iw%3D" style="width: 50%; margin-bottom: 20px;">
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">我对指针的看法</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运用</span>指针的<span style="color: black;">原由</span>之一在于,你<span style="color: black;">能够</span>在调用的函数中修改变量或对象。<span style="color: black;">然则</span>,我<span style="color: black;">意见</span><span style="color: black;">尽可能</span>避免<span style="color: black;">运用</span>指针。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在C++中,<span style="color: black;">运用</span>引用比<span style="color: black;">运用</span>指针更好,<span style="color: black;">由于</span>你<span style="color: black;">能够</span><span style="color: black;">容易</span>地修改调用的函数,而不会改变调用所<span style="color: black;">暗示</span>的语义。 </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">指针是一个<span style="color: black;">繁杂</span>的主题,程序员会经常<span style="color: black;">由于</span><span style="color: black;">运用</span>指针而<span style="color: black;">导致</span>内存管理错误。Python是适合初学者的编程语言,<span style="color: black;">因此呢</span>Python中<span style="color: black;">无</span>这种<span style="color: black;">繁杂</span>的类型。</p><img src="https://p3-sign.toutiaoimg.com/pgc-image/RTLSNam5ZxLDlM~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723889270&amp;x-signature=w1%2BY5RfcLqCgnnQIARBuYfkAhkk%3D" style="width: 50%; margin-bottom: 20px;">
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">最后</span>的<span style="color: black;">意见</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">我有什么建议?Python还是C++?</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这取决于你<span style="color: black;">必须</span>实现的<span style="color: black;">处理</span><span style="color: black;">方法</span>。你<span style="color: black;">是不是</span><span style="color: black;">必须</span>跨平台、高性能、<span style="color: black;">设备</span>学习的<span style="color: black;">处理</span><span style="color: black;">方法</span>?</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span><span style="color: black;">要求</span><span style="color: black;">准许</span>的话,你<span style="color: black;">能够</span><span style="color: black;">同期</span><span style="color: black;">运用</span>两者。 </p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">我<span style="color: black;">剧烈</span><span style="color: black;">意见</span>两者你都应该学习和<span style="color: black;">运用</span>,<span style="color: black;">由于</span>这<span style="color: black;">能够</span>拓宽你对编程语言的理解。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>你<span style="color: black;">熟练</span>C++,<span style="color: black;">那样</span>毫不费力就<span style="color: black;">能够</span>用Python编写程序。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>你<span style="color: black;">熟练</span>Python,<span style="color: black;">那样</span>应该对大<span style="color: black;">都数</span>的编程概念都有很好的<span style="color: black;">认识</span>,C++<span style="color: black;">能够</span><span style="color: black;">帮忙</span>你进一步理解内存管理、并发和指针等,<span style="color: black;">因此呢</span>二者都应该学习。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">另一</span>,你还<span style="color: black;">能够</span>将Python和C++集成到<span style="color: black;">一块</span>,实现高级的实时分析<span style="color: black;">处理</span><span style="color: black;">方法</span>。</p><img src="https://p3-sign.toutiaoimg.com/pgc-image/RpF3cSxF9pmoSP~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723889270&amp;x-signature=OlexJxQJxzADcvxplQF1tDkadb0%3D" style="width: 50%; margin-bottom: 20px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/RTRIuPNI7JPB03~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723889270&amp;x-signature=Tz%2F0oJUwzMe%2FVPXTr05cKZY77Gs%3D" style="width: 50%; margin-bottom: 20px;">
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">总结</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">C++和Python都是非常流行的语言,我非常<span style="color: black;">举荐</span>所有人都来学习这两种语言。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在本文中,<span style="color: black;">咱们</span>重点介绍了这两种语言之间的<span style="color: black;">类似</span>点和差异性,以便<span style="color: black;">咱们</span>理解何时<span style="color: black;">运用</span>哪种语言。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">原文:https://medium.com<span style="color: black;">/fintechexplained/from-python-to-c-a-thorough-comparison-9244b32d63f4</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">本文为 CSDN 翻译,转载请注明<span style="color: black;">源自</span>出处。</p>




听听海 发表于 2024-9-2 13:46:45

seo常来的论坛,希望我的网站快点收录。

7wu1wm0 发表于 2024-10-4 12:02:41

网站建设seio论坛http://www.fok120.com/

m5k1umn 发表于 2024-10-26 21:48:13

回顾过去一年,是艰难的一年;展望未来,是辉煌的一年。

1fy07h 发表于 2024-10-27 12:01:41

在遇到你之前,我对人世间是否有真正的圣人是怀疑的。

nqkk58 发表于 前天 13:38

我赞同你的看法,你的智慧让人佩服,谢谢分享。
页: [1]
查看完整版本: 势头强劲的 Python PK 强大的 C++,到底谁更胜一筹?