English 中文(简体)
Runtime vs. Compile time
原标题:
  • 时间:2009-05-10 21:06:35
  •  标签:

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

最佳回答

编译时和运行时之间的区别是一个例子的尖端理论家所说的< em > < a href = " https://en.wikipedia.org/wiki/Phase_distinction " rel = " noreferrer " > < / > < / em >相区别。这是最难学习的概念之一,特别是对于那些没有背景的编程语言。解决这个问题,我发现它有用的问

  1. What invariants does the program satisfy?
  2. What can go wrong in this phase?
  3. If the phase succeeds, what are the postconditions (what do we know)?
  4. What are the inputs and outputs, if any?

Compile time

  1. 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...
  2. What can go wrong at compile time:
    • Syntax errors
    • Typechecking errors
    • (Rarely) compiler crashes
  3. 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.)
  4. 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.

Run time

  1. 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.
  2. 什么可能出错< em > < / em >:运行时错误

    • Division by zero
    • Dereferencing a null pointer
    • Running out of memory

    也可能有错误检测的程序本身:

    • Trying to open a file that isn t there
    • Trying find a web page and discovering that an alleged URL is not well formed
  3. If run-time succeeds, the program finishes (or keeps going) without crashing.
  4. 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 :-)
问题回答

我认为它的错误,当他们可以抓住。

编制时间:

string my_value = Console.ReadLine();
int i = my_value;

一个字符串值t可以分配一个int类型的变量,所以编译器在编译时知道< em > < / em >,这段代码有问题

运行时间:

string my_value = Console.ReadLine();
int i = int.Parse(my_value);

这里的结果取决于ReadLine()返回的字符串是什么。一些值可以解析int,其他人可以t。这只能在< em >运行时确定< / em >

<强>编译时:< / >强的时期,开发人员,是编译代码。

<强>运行时:< /强>的时间段用户正在运行的软件。

你需要更清晰的定义吗?

(<强>编辑< /强>:以下适用于c#和相似,强类型的编程语言。我不确定这将帮助你)。

例如,下面的错误将被编译器(在<强>编译时< /强>)在你运行一个程序,会导致一个编译错误:

int i = "string"; --> error at compile-time

另一方面,一个错误如下不能编译器检测到的。你会收到一个错误/异常在<强>运行时< /强>(程序运行时)。

Hashtable ht = new Hashtable();
ht.Add("key", "string");
// the compiler does not know what is stored in the hashtable
// under the key "key"
int i = (int)ht["key"];  // --> exception at run-time

源代码翻译成stuff-happening-on-the——(屏幕|磁盘|网络)可以发生在(大约)两种方式;编译和解释。

在<强> < /强>编译程序(例如c和fortran):

  1. The source code is fed into another program (usually called a compiler--go figure), which produces an executable program (or an error).
  2. The executable is run (by double clicking it, or typing it s name on the command line)

< em >事情发生在第一步发生在编译时,事情发生在第二步发生在“运行时间”。< / em >

在一个<强> < /强>解释程序(例如微软基本(dos)和python(我认为)):

  1. The source code is fed into another program (usually called an interpreter) which "runs" it directly. Here the interpreter serves as an intermediate layer between your program and the operating system (or the hardware in really simple computers).

在这种情况下,编译时和运行时的区别是相当难以确定,和更少的相关程序员或用户。

Java是一种混合的代码编译成字节码,然后运行在一个虚拟机通常为字节码解释器。

还有一个中间案件程序编译成字节码并立即运行(如awk或perl)。

基本上如果你的编译器可以解决你的意思或者什么是价值”在编译时它可以硬编码到运行时代码。显然如果你的运行时代码做一个计算每次会运行缓慢,所以,如果你可以在编译时确定的东西要好得多。

如。

常数合并:

如果我写:

int i = 2;
i += MY_CONSTANT;

编译器可以在编译时执行此计算,因为它知道2是什么,和什么是MY_CONSTANT。因此它可以节省从执行计算每一个执行。

嗯,好的,运行时用于描述事情发生在一个程序正在运行。

编译时用于描述事情发生在一个程序正在建造(通常由编译器)。

Compile Time:

事情做在编译时产生(几乎)没有程序运行时产生的成本,但会产生大量成本当你构建程序。

Run-Time:

或多或少完全相反。当您构建成本,更当程序运行成本。

从另一边;如果在编译时完成的东西,它只运行在您的机器上,如果是运行时,在用户机器上运行。

Relevance

这是很重要的一个例子是一个单元类型。编译时的版本(比如< A href = " http://www.boost.org/doc/libs/1_37_0/doc/html/boost_units.html " rel = " noreferrer " >。单位< / >或< a href = " http://www.dsource.org/projects/scrapple/browser/trunk/units " rel = " noreferrer " >我的版本D < / >)最终被一样快速解决本地浮点代码的问题而最终不得不包在运行时版本的信息单位,一个值在每个操作和执行检查在他们一边。另一方面,编译时版本报答的单位值在编译时是已知的和无法处理的情况下,他们来自运行时输入。

作为一个附加到另一个答案,这年代我一个门外汉解释:

源代码就像一艘船的蓝图。它定义了船应该如何。

如果你移交蓝图船厂,他们找到一个缺陷构建船时,他们会立即停止生产并报告给你,船之前曾经离开了干船坞或接触水。这是一个编译时错误。这艘船被从未真正浮动或使用它的引擎。甚至发现了这个错误,因为它阻止了船。

当你的代码编译,这年代像船完成。建立和准备好了。当你执行你的代码,这年代像发射船航行。乘客登机时,发动机运行和船体在水面上,这是运行时。如果你的船有一个致命缺陷,沉在处女航中(或者一些额外的航行后头痛)那么它运行时错误。

后从先前的类似回答的问题< a href = " https://stackoverflow.com/questions/9471837/what-is-the-difference-between-run-time-error-and-compiler-error/30431830 30431830 " >之间的区别是什么运行时错误和编译器错误? < / >

Compilation/Compile time/Syntax/Semantic errors: Compilation or compile time errors are error occurred due to typing mistake, if we do not follow the proper syntax and semantics of any programming language then compile time errors are thrown by the compiler. They wont let your program to execute a single line until you remove all the syntax errors or until you debug the compile time errors.
Example: Missing a semicolon in C or mistyping int as Int.

Runtime errors: Runtime errors are the errors that are generated when the program is in running state. These types of errors will cause your program to behave unexpectedly or may even kill your program. They are often referred as Exceptions.
Example: Suppose you are reading a file that doesn t exist, will result in a runtime error.

阅读更多关于所有< a href = " https://codeforwin.org/2015/05/introduction-to-programming-errors.html " rel = " nofollow noreferrer " > < / >编程错误

这是引用丹尼尔梁,JAVA编程导论》一书的作者在这个问题上的编译:

“用高级语言编写的程序称为源程序或源代码。因为电脑无法执行源程序,源程序必须<强> < /强>翻译成<强>机器代码为<强> < /强> < /强>执行。翻译可以使用另一个编程工具叫翻译或<强>编译器< / >强。”(丹尼尔·梁< a href = " https://rads.stackoverflow.com/amzn/click/com/0133761312 " rel = " noreferrer " >“JAVA编程导论”< / >,p8)。

…他仍在继续……

“编译器翻译整个源代码<强> < /强>到<强>机器码< /强>文件,然后机器码文件执行”

当我们在高级打孔/这是人类可读的代码,首先,没用的!必须翻译成一个序列的电子发生在你的小CPU !这是编译的第一步。

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

<强>记住:仅仅因为程序编译没有错误并不意味着它将运行没有错误。< /强>

运行时错误发生在准备,跑步或者等待项目生命周期的一部分,而之前会发生编译时错误生命周期的新阶段。

<强>编译时错误的例子:< /强>

语法错误,如何编译成机器代码级别说明如果他们模棱两可的? ?代码需要遵循100%到该语言的语法规则否则不能被编译成工作< a href = " https://i.stack.imgur.com/SJoZu.png " rel = " noreferrer " > < / >机器代码。

<强>一个运行时错误的例子:< /强>

耗尽内存——例如调用递归函数可能会导致堆栈溢出一个变量的一个特定的程度!这怎么能预期由编译器! ?它不能。

这之间的区别是一个编译时错误,运行时错误

例如:在强类型语言中,一种可以在编译时检查或在运行时。这意味着在编译时,编译器抱怨如果类型不兼容。在运行时,意味着你可以编译你的程序很好但在运行时,它将抛出一个异常。

In simply word difference b/w Compile time & Run time.

. java的编译时间:开发人员编写程序格式&转换是一个类的字节码文件,编译期间发生任何错误可以被定义为编译时错误。

运行时:生成的. class文件是由应用程序使用的附加功能和逻辑证明是错误的,抛出一个错误是一个运行时错误

Compile time: Time taken to convert the source code into a machine code so that it becomes an executable is called compile time.

Run time: When an application is running, it is called run time.

Compile time errors are those syntax errors, missing file reference errors. Runtime errors happen after the source code has been compiled into an executable program and while the program is running. Examples are program crashes, unexpected program behavior or features don t work.

运行时意味着当你运行程序的东西。

编译时意味着当你编译程序。

想象你是一个老板,你有一个助理,一个女仆,你给他们一个任务列表,助理(编译时)将抓住这个列表,检查任务是否可以理解,你没有写任何尴尬的语言或语法,所以他知道你想指定某人工作所以他指派他为你,他知道你想要一些咖啡,因此他的角色是在和女服务员(运行时)开始运行这些任务所以她去让你喝咖啡但是突然她没有找到任何咖啡让她停止使它或行为不同,让你一些茶(当程序行为不同,因为他发现了一个错误)。

编制时间:

Things that are done at compile time incur (almost) no cost when the resulting program is run, but might incur a large cost when you build the program. Run-Time:

或多或少完全相反。当您构建成本,更当程序运行成本。

从另一边;如果在编译时完成的东西,它只运行在您的机器上,如果是运行时,在用户机器上运行。

我一直认为相对于程序处理开销和它如何影响性能如前所述。一个简单的例子,要么定义绝对内存我对象所需的代码。

一个定义布尔x的记忆这是在编译后的程序并不能改变。当程序运行它确切地知道有多少内存分配为x。

另一方面,如果我只是定义一个通用的对象类型(即一种定义占位符或者一些大型blob)的指针,我所需的实际内存对象是未知的,直到程序运行和我分配一些,因此,必须评估和内存分配,等等就会处理在运行时动态(更多的运行时开销)。

如何动态地处理将依赖于语言、编译器、操作系统、代码等。

然而,注意这将取决于您使用的是运行时的环境和编译时间。

这是一个扩展问题的答案“运行时和编译时的区别?”——<强>开销< /强>的差异与运行时编译时?

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

一个伟大的来源进一步阅读< A href = " http://developers.sun.com/solaris/articles/CC_perf/content.html " rel =“nofollow”> < / >:

我们可以将这些不同两大类下静态绑定和动态绑定。它是基于绑定完成后与相应的值。如果在编译时解析的引用,那么静态绑定,如果在运行时解析的引用,那么它是动态绑定。静态绑定和动态绑定也称为早期绑定和后期绑定。有时也称为静态多态和动态多态性。

Joseph Kulandai‏.

运行时和编译时的主要区别是:

  1. If there are any syntax errors and type checks in your code,then it throws compile time error, where-as run-time:it checks after executing the code. For example:

int a = 1 int b = a/0;

下面第一行并t分号结束时- - - >编译时错误执行程序在执行操作b后,结果是无限- - - >运行时错误。

  1. Compile time doesn t look for output of functionality provided by your code, whereas run-time does.

下面是一个非常简单的答案:

Runtime and compile time are programming terms that refer to different stages of software program development. In order to create a program, a developer first writes source code, which defines how the program will function. Small programs may only contain a few hundred lines of source code, while large programs may contain hundreds of thousands of lines of source code. The source code must be compiled into machine code in order to become and executable program. This compilation process is referred to as compile time.(think of a compiler as a translator)

编译程序由用户可以打开并运行。当应用程序运行时,它被称为运行时。

术语“运行时”和“编译时”往往被程序员用来指不同类型的错误。编译时错误问题比如语法错误或丢失的文件引用阻止程序成功编译。编译器编译时产生的错误,通常表示行源代码是造成这个问题。

如果一个程序源代码年代已经被编译成可执行程序,它可能仍然有错误发生在程序运行。例子包括功能不工作,意想不到的程序行为,或者程序崩溃。这些类型的问题被称为运行时错误,因为它们发生在运行时。

< a href = " http://pc.net/helpcenter/answers/compile_time_vs_runtime " rel =“nofollow”>引用< / >

看看这个例子:

public class Test {

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

The above code is compiled successfully, there is no syntax error, it is perfectly valid. But at the run time, it throws following error.

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

Like when in compile time certain cases has been checked, after that run time certain cases has been checked once the program satisfies all the condition you will get an output. Otherwise, you will get compile time or run time error.

你可以从阅读理解的代码编译结构实际的代码。运行时结构不清楚,除非你了解使用的模式。

public class RuntimeVsCompileTime {

    public static void main(String[] args) {
        
        //test(new D()); COMPILETIME ERROR
        /**
         * Compiler knows that B is not an instance of A
         */
        test(new B());
    }
    
    /**
     * compiler has no hint whether the actual type is A, B or C
     * C c = (C)a; will be checked during runtime
     * @param a
     */
    public static void test(A a) {
        C c = (C)a;//RUNTIME ERROR
    }

}

    class A{
    
}

    class B extends A{
    
}

    class C extends A{
    
}

    class D{
    
}

它不是一个好问题S.O.(这不是一个特定的编程问题),但它不是一个糟糕的问题。

如果你认为这年代微不足道:读取期vs编译时,当这是一个有用的区别?语言编译器在运行时可用呢?人斯蒂尔(不假,他)写了7页CLTL2 EVAL-WHEN, CL程序员可以使用它来控制。2句子是勉强够一个< em > < / em >定义,这本身是远低于< em > < / em >解释。

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

和周围的problemspace编译时和运行时在很大程度上仍是巨大的和未知的。这年代不是说S.O.是正确的地方有讨论,但我鼓励人们进一步探索这个领域,特别是那些没有先入为主的观念应该是什么。问题是既不简单也不傻,我们至少可以点检察官在正确的方向上。

不幸的是,我不知道任何好的参考。CLTL2谈论它,但它不适合学习。





相关问题
热门标签