为什么有些编程语言比其他语言更快? [英] Why are some programming languages faster than others?

查看:413
本文介绍了为什么有些编程语言比其他语言更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道ASM基本上是最快的一个可以得到,但是是什么使HLL慢于ASM的抽象?我的意思是抽象的是,例如在C + +你有一个类,数据需要存储什么存储在类中,它从中得到什么,私有/公共访问器和其他东西。当这个代码编译时,有没有实际的汇编代码工作来找出类的信息?像CPython是建立在C上的,所以有更多的抽象和指令要在运行时比C运行。我所说的真的吗?我想我已经回答了我自己的问题,但我想得到一个更有经验的人,而不是我自己的答案。

I know that ASM is basically the fastest one can get, but is what makes HLLs slower than ASM the abstraction? What I mean by abstraction is that for instance in C++ you have a class, data needs to be stored about what is stored in the class, what it derives from, private/public accessors, and other things. When this code is compiled, is there actual assembly code that does work to figure out information about the class? Like CPython is built upon C so there is even more abstraction and instructions to be run at run-time than C. Is any of what I am saying true? I think I have answered my own question but I would like to get an answer from a more experienced individual other than myself.

编辑:我知道Python被解释,但是

I understand that Python is interpreted but wouldn't it still be slower than C if it were compiled?

推荐答案

这是一个广泛的问题。

基本上,编译语言就像ASM一样被翻译成机器指令(op code)(ASM也是一个抽象层)。一个好的编译器实际上可能超过一个平均的ASM编码器的结果,因为它可以检查大块代码和应用大多数程序员不能手动(排序指令为最佳执行等)的优化规则。

Fundamentally, compiled languages get translated into machine instructions (op codes) just as ASM does (ASM is a layer of abstraction, too). A good compiler is actually likely to out-perform an average ASM coder's result because it can examine a large patch of code and apply optimization rules that most programmers could not do by hand (ordering instructions for optimal execution, etc).

在这方面,所有编译语言都创建为等于。然而,有些人比其他人更平等。编译代码执行的程度从根本上取决于编译器的性能,更不用说特定的语言。某些功能,如虚拟方法会导致性能损失(上次我检查虚拟方法是使用一个函数指针表实现的,但我的知识可能在这里。)

In that regard, all compiled languages are created "equal". However, some are more equal than others. How well the compiled code performs depends fundamentally on how good the compiler is, and much less on the specific language. Certain features such as virtual methods incur a performance penalty (last time I checked virtual methods were implemented using a table of function pointers, though my knowledge may be dated here).

解释语言在程序执行时从根本上检查人类可读的语言,本质上在程序运行时需要编译和执行阶段。因此,它们几乎总是比编译对象慢一些。智能实现将递增地解释执行的代码部分(避免解释从未命中的分支),并缓存结果,以使给定部分的代码只被解释一次。

Interpreted languages fundamentally examine human-readable language as the program executes, essentially necessitating the equivalent of both the compile and execution stages during program runtime. Therefore they will almost always be somewhat slower than a compiled counterpart. Smart implementations will incrementally interpret parts of the code as executed (to avoid interpreting branches that are never hit), and cache the result so that a given portion of code is only interpreted once.

还有一个中间点,人类可读的语言被翻译成伪代码(有时称为P代码或字节代码)。这样做的目的是使代码具有紧凑的表示,其快速解释,但是在许多操作系统上是可移植的(您仍然需要一个程序来解释每个平台上的P代码)。 Java属于这一类。

There's a middle ground as well, in which human-readable language is translated into pseudo-code (sometimes called P-code or byte code). The purpose of this is to have a compact representation of the code that is fast to interpret, yet portable across many operating systems (you still need a program to interpret the P-code on each platform). Java falls into this category.

这篇关于为什么有些编程语言比其他语言更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆