运行时与编译时
运行时和编译时有什么区别?
What is the difference between run-time and compile-time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
运行时和编译时有什么区别?
What is the difference between run-time and compile-time?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(26)
编译时和运行时之间的差异是尖头理论家所说的相区别< /a>。 它是最难学习的概念之一,特别是对于没有太多编程语言背景的人来说。 为了解决这个问题,我发现询问
编译时间
运行时
可能出错的是运行时错误:
程序本身也可能检测到错误:
The difference between compile time and run time is an example of what pointy-headed theorists call the phase distinction. It is one of the hardest concepts to learn, especially for people without much background in programming languages. To approach this problem, I find it helpful to ask
Compile time
Run time
What can go wrong are run-time errors:
Also there can be errors that are detected by the program itself:
我从错误以及何时可以发现错误的角度来考虑。
编译时:
字符串值不能分配给 int 类型的变量,因此编译器在编译时肯定知道这段代码有问题
运行时:
这里的结果取决于字符串是什么由 ReadLine() 返回。 有些值可以解析为 int,有些则不能。 这只能在运行时确定
I think of it in terms of errors, and when they can be caught.
Compile time:
A string value can't be assigned a variable of type int, so the compiler knows for sure at compile time that this code has a problem
Run time:
Here the outcome depends on what string was returned by ReadLine(). Some values can be parsed to an int, others can't. This can only be determined at run time
编译时:您(开发人员)编译代码的时间段。
运行时:用户运行您的软件的时间段。
您需要更明确的定义吗?
Compile-time: the time period in which you, the developer, are compiling your code.
Run-time: the time period which a user is running your piece of software.
Do you need any clearer definition?
(编辑:以下内容适用于 C# 和类似的强类型编程语言。我不确定这是否对您有帮助)。
例如,在运行程序之前,编译器将(在编译时)检测到以下错误,并导致编译错误:
另一方面,不能将如下错误视为错误:由编译器检测到。 您将在运行时(程序运行时)收到错误/异常。
(edit: the following applies to C# and similar, strongly-typed programming languages. I'm not sure if this helps you).
For example, the following error will be detected by the compiler (at compile time) before you run a program and will result in a compilation error:
On the other hand, an error like the following can not be detected by the compiler. You will receive an error/exception at run-time (when the program is run).
将源代码翻译成[屏幕|磁盘|网络]上发生的事情可以(大致)两种方式进行; 称它们为编译和解释。
在编译程序中(例如 c 和 fortran):
第一步中发生的事情被称为在“编译时”发生,第二步中发生的事情被称为发生在“运行时”。
在解释程序中(例如MicroSoft basic(在dos上)和python(我认为)):
在这种情况下,编译时和运行时之间的差异更难确定,并且与程序员或用户的相关性更小。
Java 是一种混合体,其中代码被编译为字节码,然后在虚拟机上运行,该虚拟机通常是字节码的解释器。
还有一种中间情况,其中程序被编译为字节码并立即运行(如 awk 或 perl)。
Translation of source code into stuff-happening-on-the-[screen|disk|network] can occur in (roughly) two ways; call them compiling and interpreting.
In a compiled program (examples are c and fortran):
Things that happen in the first step are said to happen at "compile time", things that happen in the second step are said to happen at "run time".
In an interpreted program (example MicroSoft basic (on dos) and python (I think)):
In this case the difference between compile time and run time is rather harder to pin down, and much less relevant to the programmer or user.
Java is a sort of hybrid, where the code is compiled to bytecode, which then runs on a virtual machine which is usually an interpreter for the bytecode.
There is also an intermediate case in which the program is compiled to bytecode and run immediately (as in awk or perl).
基本上,如果您的编译器可以计算出您的意思或“在编译时”的值是什么,它可以将其硬编码到运行时代码中。 显然,如果您的运行时代码每次都必须进行计算,那么它的运行速度会变慢,因此如果您可以在编译时确定某些内容,那就更好了。
例如。
常量折叠:
如果我写:
编译器可以在编译时执行此计算,因为它知道 2 是什么,以及 MY_CONSTANT 是什么。 因此,它可以避免每次执行时都执行计算。
Basically if your compiler can work out what you mean or what a value is "at compile time" it can hardcode this into the runtime code. Obviously if your runtime code has to do a calculation every time it will run slower, so if you can determine something at compile time it is much better.
Eg.
Constant folding:
If I write:
The compiler can perform this calulation at compile time because it knows what 2 is, and what MY_CONSTANT is. As such it saves itself from performing a calculation every single execution.
嗯,好吧,运行时用于描述程序运行时发生的事情。
编译时间用于描述构建程序(通常由编译器)时发生的事情。
Hmm, ok well, runtime is used to describe something that occurs when a program is running.
Compile time is used to describe something that occurs when a program is being built (usually, by a compiler).
编译时:
在编译时完成的事情在运行结果程序时(几乎)不会产生任何成本,但在构建程序时可能会产生大量成本。
运行时:
或多或少完全相反。 构建时的成本很低,程序运行时的成本更高。
从另一边; 如果某些操作是在编译时完成的,则它仅在您的计算机上运行;如果某些操作是运行时完成的,则它在您的用户计算机上运行。
相关性
单位承载类型是这一点很重要的一个例子。 编译时版本(例如 Boost.Units 或 < a href="http://www.dsource.org/projects/scrapple/browser/trunk/units" rel="noreferrer">我在 D 中的版本)最终与解决问题一样快本机浮点代码,而运行时版本最终必须打包有关值所在单位的信息,并在每次操作时对它们进行检查。 另一方面,编译时版本要求值的单位在编译时已知,并且不能处理它们来自运行时输入的情况。
Compile Time:
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:
More or less the exact opposite. Little cost when you build, more cost when the program is run.
From the other side; If something is done at compile time, it runs only on your machine and if something is run-time, it run on your users machine.
Relevance
An example of where this is important would be a unit carrying type. A compile time version (like Boost.Units or my version in D) ends up being just as fast as solving the problem with native floating point code while a run-time version ends up having to pack around information about the units that a value are in and perform checks in them along side every operation. On the other hand, the compile time versions requiter that the units of the values be known at compile time and can't deal with the case where they come from run-time input.
作为其他答案的补充,我将向外行人解释如下:
您的源代码就像一艘船的蓝图。 它定义了船舶的制造方式。
如果你把你的蓝图交给造船厂,他们在建造船舶时发现了缺陷,他们会停止建造,并在船舶离开干船坞或接触水之前立即向你报告。 这是一个编译时错误。 这艘船甚至从未真正漂浮或使用过发动机。 这个错误被发现是因为它甚至阻止了这艘船的制造。
当你的代码编译时,就像一艘正在完工的船一样。 已建成并准备就绪。 当您执行代码时,就像在航行中启动船舶一样。 乘客登机,发动机运转,船体浮在水面上,所以这是运行时间。 如果你的船有一个致命的缺陷,导致它在处女航时沉没(或者可能在之后的一些航程中因为额外的头痛而沉没),那么它就会遇到运行时错误。
As an add-on to the other answers, here's how I'd explain it to a layman:
Your source code is like the blueprint of a ship. It defines how the ship should be made.
If you hand off your blueprint to the shipyard, and they find a defect while building the ship, they'll stop building and report it to you immediately, before the ship has ever left the drydock or touched water. This is a compile-time error. The ship was never even actually floating or using its engines. The error was found because it prevented the ship even being made.
When your code compiles, it's like the ship being completed. Built and ready to go. When you execute your code, that's like launching the ship on a voyage. The passengers are boarded, the engines are running and the hull is on the water, so this is runtime. If your ship has a fatal flaw that sinks it on its maiden voyage (or maybe some voyage after for extra headaches) then it suffered a runtime error.
根据之前类似问题的答案 运行时错误和编译器错误有什么区别?
编译/编译时/语法/语义错误:编译或编译时错误是由于键入错误而发生的错误,如果我们不遵循任何编程语言的正确语法和语义,那么编译器会抛出编译时错误。 在您删除所有语法错误或调试编译时错误之前,它们不会让您的程序执行一行。
示例:C 语言中缺少分号或将
int
错误地输入为Int
。运行时错误:运行时错误是程序处于运行状态时产生的错误。 这些类型的错误将导致您的程序出现意外行为,甚至可能终止您的程序。 它们通常被称为异常。
示例:假设您正在读取一个不存在的文件,将导致运行时错误。
在此处详细了解所有编程错误
Following from previous similar answer of question What is the difference between run-time error and compiler error?
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
asInt
.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.
Read more about all programming errors here
以下是《JAVA编程入门》一书的作者梁丹尼尔关于编译主题的一段话:
...他继续...
当我们输入高级/人类可读的内容时这段代码一开始是没用的! 它必须在你小小的 CPU 中转化为一系列“电子事件”! 实现这一目标的第一步是编译。
简单地说:编译时错误发生在这个阶段,而运行时错误则发生在稍后阶段。
记住:程序编译时没有错误并不意味着它运行时不会出现错误。
编译时,程序生命周期的就绪、运行或等待部分会发生运行时错误-时间错误将在生命周期的“新”阶段之前发生。
编译时错误示例:
语法错误 - 如果代码不明确,如何将其编译为机器级指令? 您的代码需要 100% 符合该语言的语法规则,否则无法编译为工作机器代码。
运行时错误示例:
内存不足 - 例如,给定特定程度的变量,调用递归函数可能会导致堆栈溢出! 编译器怎么能预料到这一点!? 这不可以。
这就是编译时错误和运行时错误之间的区别
Here is a quote from Daniel Liang, author of 'Introduction to JAVA programming', on the subject of compilation:
...He Continues...
When we punch in high-level/human-readable code this is, at first, useless! It must be translated into a sequence of 'electronic happenings' in your tiny little CPU! The first step towards this is compilation.
Simply put: a compile-time error happens during this phase, while a run-time error occurs later.
Remember: Just because a program is compiled without error does not mean it will run without error.
A Run-time error will occur in the ready, running or waiting part of a programs life-cycle while a compile-time error will occur prior to the 'New' stage of the life cycle.
Example of a Compile-time error:
A Syntax Error - how can your code be compiled into machine level instructions if they are ambiguous?? Your code needs to conform 100% to the syntactical rules of the language otherwise it cannot be compiled into working machine code.
Example of a run-time error:
Running out of memory - A call to a recursive function for example might lead to stack overflow given a variable of a particular degree! How can this be anticipated by the compiler!? it cannot.
And that is the difference between a compile-time error and a run-time error
例如:在强类型语言中,可以在编译时或运行时检查类型。 在编译时,这意味着编译器会抱怨类型不兼容。 在运行时意味着,您可以很好地编译程序,但在运行时,它会抛出异常。
For example: In a strongly typed language, a type could be checked at compile time or at runtime. At compile time it means, that the compiler complains if the types are not compatible. At runtime means, that you can compile your program just fine but at runtime, it throws an exception.
简单来说,就是黑白编译时间和编译时间的词差异。 运行。
编译时:开发人员以.java格式编写程序& 转换为字节码,这是一个类文件,在编译期间发生的任何错误都可以定义为编译时错误。
运行时:应用程序使用生成的 .class 文件来实现其附加功能和功能。 逻辑被证明是错误的并抛出一个错误,这是一个运行时错误
In simply word difference b/w Compile time & Run time.
compile time:Developer writes the program in .java format & converts in to the Bytecode which is a class file,during this compilation any error occurs can be defined as compile time error.
Run time:The generated .class file is use by the application for its additional functionality & the logic turns out be wrong and throws an error which is a run time error
编译时间:
将源代码转换为机器代码以使其成为可执行文件所需的时间称为编译时间。
运行时间:
当应用程序运行时,称为运行时。
编译时错误是那些语法错误、缺少文件引用错误。
运行时错误发生在源代码编译成可执行程序之后以及程序运行时。 例如程序崩溃、意外的程序行为或功能不起作用。
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.
想象一下,你是老板,有一个助手和一个女佣,你给他们一份要做的任务清单,助手(编译时)会抓取这个清单并进行检查,看看这些任务是否可以理解,以及你是否可以理解这些任务。没有用任何尴尬的语言或语法来写,所以他明白你想指派某人去做一份工作,所以他为你指派他,他明白你想要一些咖啡,所以他的角色结束了,女仆(运行时间)开始运行这些任务,所以她去给你泡一些咖啡,但突然她找不到任何咖啡可做,所以她停止制作咖啡,或者她以不同的方式为你泡一些茶(当程序因发现错误而以不同的方式运行时) )。
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).
运行时意味着运行程序时会发生一些事情。
编译时意味着编译程序时会发生一些事情。
Run time means something happens when you run the program.
Compile time means something happens when you compile the program.
编译时:
在编译时完成的事情在运行结果程序时(几乎)不会产生任何成本,但在构建程序时可能会产生大量成本。
运行时:
或多或少完全相反。 构建时的成本很低,程序运行时的成本更高。
从另一边; 如果某些操作是在编译时完成的,则它仅在您的计算机上运行;如果某些操作是运行时完成的,则它在您的用户计算机上运行。
Compile Time:
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:
More or less the exact opposite. Little cost when you build, more cost when the program is run.
From the other side; If something is done at compile time, it runs only on your machine and if something is run-time, it run on your users machine.
我一直认为它与程序处理开销相关,以及它如何影响性能,如前所述。 一个简单的例子是,要么在代码中定义我的对象所需的绝对内存,要么不定义。
定义的布尔值占用 x 内存,然后该内存位于已编译的程序中并且无法更改。 当程序运行时,它确切地知道要为 x 分配多少内存。
另一方面,如果我只是定义一个通用对象类型(即一种未定义的占位符,或者可能是指向某个巨大 blob 的指针),则在程序运行并为其分配一些内容之前,我的对象所需的实际内存是不知道的,因此必须对其进行评估,然后在运行时动态处理内存分配等(更多运行时开销)。
如何动态处理它将取决于语言、编译器、操作系统、代码等。
但就这一点而言,它实际上取决于您使用运行时与编译时的上下文。
I have always thought of it relative to program processing overhead and how it affects preformance as previously stated. A simple example would be, either defining the absolute memory required for my object in code or not.
A defined boolean takes x memory this is then in the compiled program and cannot be changed. When the program runs it knows exactly how much memory to allocate for x.
On the other hand if I just define a generic object type (i.e. kind of a undefined place holder or maybe a pointer to some giant blob) the actual memory required for my object is not known until the program is run and I assign something to it, thus it then must be evaluated and memory allocation, etc. will be then handled dynamically at run time (more run time overhead).
How it is dynamically handled would then depend on the language, the compiler, the OS, your code, etc.
On that note however it would really depends on the context in which you are using run time vs compile time.
这是对“运行时和编译时之间的差异?”问题的答案的扩展。 -- 与运行时和编译时相关的开销有何差异?
产品的运行时性能通过更快地交付结果来提高其质量。 产品的编译时性能通过缩短编辑-编译-调试周期来提高其及时性。 然而,运行时性能和编译时性能都是实现及时质量的次要因素。 因此,只有当整体产品质量和及时性的改进证明合理时,才应该考虑运行时和编译时性能的改进。
此处是进一步阅读的重要来源:
Here is an extension to the Answer to the question "difference between run-time and compile-time?" -- Differences in overheads associated with run-time and compile-time?
The run-time performance of the product contributes to its quality by delivering results faster. The compile-time performance of the product contributes to its timeliness by shortening the edit-compile-debug cycle. However, both run-time performance and compile-time performance are secondary factors in achieving timely quality. Therefore, one should consider run-time and compile-time performance improvements only when justified by improvements in overall product quality and timeliness.
A great source for further reading here:
我们可以将它们分为静态绑定和动态绑定两大类。 它基于与相应值完成绑定的时间。 如果引用在编译时解析,则它是静态绑定,如果引用在运行时解析,则它是动态绑定。 静态绑定和动态绑定也称为早期绑定和后期绑定。 有时它们也称为静态多态性和动态多态性。
约瑟夫·库兰代 (Joseph Kulandai)。
we can classify these under different two broad groups static binding and dynamic binding. It is based on when the binding is done with the corresponding values. If the references are resolved at compile time, then it is static binding and if the references are resolved at runtime then it is dynamic binding. Static binding and dynamic binding also called as early binding and late binding. Sometimes they are also referred as static polymorphism and dynamic polymorphism.
Joseph Kulandai.
运行时和编译时之间的主要区别是:
例如:
int a = 1
int b = a/0;
这里第一行末尾没有分号---> 执行操作b时执行程序后出现编译时错误,结果为无限---> 运行时错误。
The major difference between run-time and compile time is:
For example:
int a = 1
int b = a/0;
here first line doesn't have a semi-colon at the end---> compile time error after executing the program while performing operation b, result is infinite---> run-time error.
这是一个非常简单的答案:
运行时和编译时是编程术语,指的是软件程序开发的不同阶段。
为了创建程序,开发人员首先编写源代码,源代码定义了程序的运行方式。 小程序可能只包含几百行源代码,而大程序可能包含数十万行源代码。 源代码必须编译成机器代码才能成为可执行程序。 这个编译过程称为编译时。(将编译器视为翻译器)
编译后的程序可以由用户打开并运行。 当应用程序运行时,称为运行时。
程序员经常使用术语“运行时”和“编译时”来指代不同类型的错误。 编译时错误是指语法错误或缺少文件引用等导致程序无法成功编译的问题。 编译器会产生编译时错误,并且通常会指示源代码的哪一行导致了问题。
如果程序的源代码已经编译成可执行程序,则程序运行时仍可能存在错误。 示例包括不起作用的功能、意外的程序行为或程序崩溃。 这些类型的问题称为运行时错误,因为它们发生在运行时。
参考
here's a very simple answer:
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 compiled program can be opened and run by a user. When an application is running, it is called runtime.
The terms "runtime" and "compile time" are often used by programmers to refer to different types of errors. A compile time error is a problem such as a syntax error or missing file reference that prevents the program from successfully compiling. The compiler produces compile time errors and usually indicates what line of the source code is causing the problem.
If a program's source code has already been compiled into an executable program, it may still have bugs that occur while the program is running. Examples include features that don't work, unexpected program behavior, or program crashes. These types of problems are called runtime errors since they occur at runtime.
The reference
看一下这个例子:
上面的代码编译成功,没有语法错误,完全有效。
但在运行时,它会抛出以下错误。
就像在编译时检查了某些情况一样,在运行时检查了某些情况后,一旦程序满足所有条件,您将得到输出。
否则,您将收到编译时或运行时错误。
Look into this example:
The above code is compiled successfully, there is no syntax error, it is perfectly valid.
But at the run time, it throws following error.
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.
您可以通过阅读实际代码来了解代码编译结构。 除非您了解所使用的模式,否则运行时结构并不清晰。
You can understand the code compile structure from reading the actual code. Run-time structure are not clear unless you understand the pattern that was used.
对于SO来说这不是一个好问题(这不是一个特定的编程问题),但总的来说这不是一个坏问题。
如果您认为这很简单:读取时与编译时怎么样?什么时候这是有用的区别? 编译器在运行时可用的语言又如何呢? Guy Steele(不是傻瓜,他)在 CLTL2 中写了 7 页有关 EVAL-WHEN 的内容,CL 程序员可以使用它来控制这一点。 2 句话勉强够定义,而定义本身还远远不够解释。
总的来说,这是语言设计者似乎试图避免的一个棘手问题。
他们经常只是说“这是一个编译器,它执行编译时的操作;之后的所有操作都是运行时的,玩得开心”。 C 被设计为易于实现,而不是最灵活的计算环境。 当您在运行时没有可用的编译器,或者无法轻松控制何时计算表达式时,您往往会在语言中进行黑客攻击来伪造宏的常见用法,或者用户提出设计模式来模拟拥有更强大的构造。 一种易于实现的语言绝对是一个有价值的目标,但这并不意味着它是编程语言设计的最终目标。 (我不太使用 EVAL-WHEN,但我无法想象没有它的生活。)
编译时和运行时的问题空间很大,而且在很大程度上仍未被探索。 这并不是说 SO 是进行讨论的正确场所,但我鼓励人们进一步探索这个领域,尤其是那些对它应该是什么没有先入为主观念的人。 这个问题既不简单也不愚蠢,我们至少可以为审判官指明正确的方向。
不幸的是,我不知道这方面有什么好的参考资料。 CLTL2 讲了一点,但是对于学习它来说并不是很好。
It's not a good question for S.O. (it's not a specific programming question), but it's not a bad question in general.
If you think it's trivial: what about read-time vs compile-time, and when is this a useful distinction to make? What about languages where the compiler is available at runtime? Guy Steele (no dummy, he) wrote 7 pages in CLTL2 about EVAL-WHEN, which CL programmers can use to control this. 2 sentences are barely enough for a definition, which itself is far short of an explanation.
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.)
And the problemspace around compile-time and run-time is huge and still largely unexplored. That's not to say S.O. is the right place to have the discussion, but I encourage people to explore this territory further, especially those who have no preconceived notions of what it should be. The question is neither simple nor silly, and we could at least point the inquisitor in the right direction.
Unfortunately, I don't know any good references on this. CLTL2 talks about it a bit, but it's not great for learning about it.