运行时和编译时的区别是什么?


当前回答

编译时和运行时之间的差异就是精明的理论家所说的阶段差异的一个例子。它是最难学习的概念之一,特别是对于没有太多编程语言背景的人来说。要解决这个问题,我发现问一下很有帮助

程序满足哪些不变量? 在这个阶段会出现什么问题? 如果阶段成功,后置条件是什么(我们知道什么)? 输入和输出是什么(如果有的话)?

编译时

The program need not satisfy any invariants. In fact, it needn't be a well-formed program at all. You could feed this HTML to the compiler and watch it barf... What can go wrong at compile time: Syntax errors Typechecking errors (Rarely) compiler crashes If the compiler succeeds, what do we know? The program was well formed---a meaningful program in whatever language. It's possible to start running the program. (The program might fail immediately, but at least we can try.) What are the inputs and outputs? Input was the program being compiled, plus any header files, interfaces, libraries, or other voodoo that it needed to import in order to get compiled. Output is hopefully assembly code or relocatable object code or even an executable program. Or if something goes wrong, output is a bunch of error messages.

运行时

We know nothing about the program's invariants---they are whatever the programmer put in. Run-time invariants are rarely enforced by the compiler alone; it needs help from the programmer. What can go wrong are run-time errors: Division by zero Dereferencing a null pointer Running out of memory Also there can be errors that are detected by the program itself: Trying to open a file that isn't there Trying find a web page and discovering that an alleged URL is not well formed If run-time succeeds, the program finishes (or keeps going) without crashing. Inputs and outputs are entirely up to the programmer. Files, windows on the screen, network packets, jobs sent to the printer, you name it. If the program launches missiles, that's an output, and it happens only at run time :-)

其他回答

嗯,好吧,运行时是用来描述程序运行时发生的事情。

编译时间用来描述在构建程序(通常由编译器)时发生的事情。

Imagine that you are a boss and you have an assistant and a maid, and you give them a list of tasks to do, the assistant (compile time) will grab this list and make a checkup to see if the tasks are understandable and that you didn't write in any awkward language or syntax, so he understands that you want to assign someone for a Job so he assign him for you and he understand that you want some coffee, so his role is over and the maid (run time)starts to run those tasks so she goes to make you some coffee but in sudden she doesn’t find any coffee to make so she stops making it or she acts differently and make you some tea (when the program acts differently because he found an error).

我们可以将其分为两大类静态绑定和动态绑定。它取决于何时与相应值绑定。如果引用是在编译时解析的,那么它是静态绑定;如果引用是在运行时解析的,那么它是动态绑定。静态绑定和动态绑定又称早期绑定和后期绑定。有时也称为静态多态和动态多态。

约瑟夫Kulandai‏。

运行时是指在运行程序时发生的事情。

编译时是指在编译程序时发生的事情。

对于S.O.来说,这不是一个好问题(这不是一个特定的编程问题),但总的来说,这不是一个坏问题。

如果您认为这是微不足道的:那么读时与编译时的区别是什么呢?什么时候这是一个有用的区别?编译器在运行时可用的语言呢?Guy Steele(不是笨蛋,他)在CLTL2中写了7页关于EVAL-WHEN的内容,CL程序员可以使用它来控制这一点。两句话只能勉强给出一个定义,而定义本身还远远不够解释。

In general, it's a tough problem that language designers have seemed to try to avoid. They often just say "here's a compiler, it does compile-time things; everything after that is run-time, have fun". C is designed to be simple to implement, not the most flexible environment for computation. When you don't have the compiler available at runtime, or the ability to easily control when an expression is evaluated, you tend to end up with hacks in the language to fake common uses of macros, or users come up with Design Patterns to simulate having more powerful constructs. A simple-to-implement language can definitely be a worthwhile goal, but that doesn't mean it's the end-all-be-all of programming language design. (I don't use EVAL-WHEN much, but I can't imagine life without it.)

关于编译时和运行时的问题空间是巨大的,而且在很大程度上仍未被探索。这并不是说S.O.是进行讨论的正确场所,但我鼓励人们进一步探索这一领域,特别是那些对它应该是什么没有先入为主概念的人。这个问题既不简单也不愚蠢,我们至少可以给检察官指出正确的方向。

不幸的是,我不知道任何好的参考资料。CLTL2稍微讲了一下,但对于学习它并不是很好。