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


当前回答

这里是对“运行时和编译时的区别?”这个问题的回答的扩展。运行时和编译时开销的差异?

产品的运行时性能通过更快地交付结果来提高其质量。产品的编译时性能通过缩短编辑-编译-调试周期来提高其时效性。然而,运行时性能和编译时性能都是实现及时性质量的次要因素。因此,只有当整体产品质量和时效性得到改善时,才应该考虑运行时和编译时性能的改进。

这里有一个很好的进一步阅读的来源:

其他回答

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).

编译时间:您作为开发人员编译代码的时间段。

运行时间:用户运行你的软件的时间段。

你需要更明确的定义吗?

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

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

编译时

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 :-)

看看这个例子:

public class Test {

    public static void main(String[] args) {
        int[] x=new int[-5];//compile time no error
        System.out.println(x.length);
    }}

上面的代码编译成功,没有语法错误,完全有效。 但是在运行时,它抛出以下错误。

Exception in thread "main" java.lang.NegativeArraySizeException
    at Test.main(Test.java:5)

比如在编译时检查了某些情况,在运行时检查了某些情况,一旦程序满足所有条件,就会得到输出。 否则,您将得到编译时或运行时错误。

下面是《JAVA编程入门》的作者Daniel Liang关于编译的一段话:

用高级语言编写的程序称为源程序或源代码。因为计算机不能执行源程序,所以必须将源程序转换成机器代码才能执行。翻译可以使用另一种被称为解释器或编译器的编程工具来完成。”(Daniel Liang,“JAVA编程入门”,p8)。

...他仍在继续……

编译器将整个源代码翻译成一个机器代码文件,然后执行机器代码文件。

当我们输入高级/人类可读的代码时,这在一开始是无用的!它必须被翻译成一个序列的“电子事件”在你的小CPU!实现这一目标的第一步是编译。

简单地说:编译时错误发生在这个阶段,而运行时错误稍后发生。

请记住:仅仅因为一个程序没有错误地编译,并不意味着它将没有错误地运行。

运行时错误将发生在程序生命周期的准备、运行或等待部分,而编译时错误将发生在生命周期的“新”阶段之前。

编译时错误的例子:

语法错误——如果你的代码有歧义,你怎么能把它们编译成机器级指令??你的代码需要100%符合语言的语法规则,否则它不能被编译成工作的机器代码。

运行时错误的例子:

内存不足——例如,在给定特定程度的变量时,调用递归函数可能会导致堆栈溢出!编译器怎么能预料到这一点!?它不能。

这就是编译时错误和运行时错误的区别