C# 运行时和编译时有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15868456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between runtime and compile-time?
提问by Garrett Biermann
So what is a runtime? Is it a virtual machine that executes half-compiled code that cannot run on a specific processor. If so, then what's a virtual machine? Is it another software that further translates the half-compiled code to machine specific code? So what if we are talking about one of those languages that don't compile to intermediate code but rather translate/compile directly to machine code. What's a runtime in that situation? is it the hardware (CPU and RAM)?
那么什么是运行时?是不是执行不能在特定处理器上运行的半编译代码的虚拟机。如果是这样,那么什么是虚拟机?它是另一种将半编译代码进一步转换为机器特定代码的软件吗?那么,如果我们谈论一种不编译为中间代码而是直接翻译/编译为机器代码的语言呢?在这种情况下什么是运行时?是硬件(CPU 和 RAM)吗?
Also, what's the difference between compile-time and runtime? Are they stages of a software lifecycle. I mean a program is originally a bunch of text files, right? So you compile or translate those to a form of data that then either can be loaded to memory and executed by the processor or if it's a "managed" language, then it would need further compilation before it can run on hardware. What exactly is a managed language?
另外,编译时和运行时有什么区别?它们是软件生命周期的各个阶段吗?我的意思是一个程序最初是一堆文本文件,对吧?因此,您将它们编译或转换为一种数据形式,然后可以将其加载到内存中并由处理器执行,或者如果它是“托管”语言,则需要进一步编译才能在硬件上运行。究竟什么是托管语言?
Lastly, is there such a thing as debug-time and what is it?
最后,是否有调试时间之类的东西,它是什么?
I'm in my first term studying computer science, and it really confuses me how illogically things are taught. "Information" is being shoved down my throat, but whenever I try to make sense out of everything by organizing everything related into a single system of well defined components and relations, I get stuck.
我在第一学期学习计算机科学,这真的让我很困惑,教授的东西是多么不合逻辑。“信息”被塞进我的喉咙,但每当我试图通过将所有相关内容组织成一个由明确定义的组件和关系组成的单一系统来理解所有内容时,我就会陷入困境。
Thanks in advance, Garrett
提前致谢,加勒特
回答by poke
Compile-time and run-time usually refers to when checks occur or when errors can happen. For example, in a statically typed language like C# the static type checks are made at compile time. That means that you cannot compile the application if you for example try to assign a string to an int-variable. Run-time on the other hand refers to the time when the code is actually executed. For example exceptions are always thrown at run-time.
编译时和运行时通常是指何时发生检查或何时可能发生错误。例如,在像 C# 这样的静态类型语言中,静态类型检查是在编译时进行的。这意味着如果您尝试将字符串分配给 int 变量,则无法编译应用程序。另一方面,运行时间是指代码实际执行的时间。例如,异常总是在运行时抛出。
As for virtual machines and such; C# is a language that compiles into the Common Intermediate Language(CIL, or IL). The result is a code which is the same regardless of which .NET language you use (C# and VB.NET both produce IL). The .NET Framework then executes this language at run-time using Just-in-time compilation. So yeah, you can see the .NET Framework as a virtual machine that runs a special sublanguage against the target machine code.
至于虚拟机等;C# 是一种编译为通用中间语言(CIL,或 IL)的语言。结果是无论您使用哪种 .NET 语言都相同的代码(C# 和 VB.NET 都产生 IL)。然后 .NET Framework 使用实时编译在运行时执行此语言。所以是的,您可以将 .NET Framework 视为一个虚拟机,它针对目标机器代码运行一种特殊的子语言。
As for debug-time, I don't think there is such a thing, as you are still running the program when debugging. So if anything, debug-time would be run-time with an attached debugger. But you wouldn't use a term like that.
至于debug-time,我觉得没有,因为你调试的时候还在运行程序。因此,如果有的话,调试时将是带有附加调试器的运行时。但你不会使用这样的术语。
回答by Peter Geiger
Compile-time - The period at which a compiler will attempt to compile some code. Example: "The compiler found 3 type errors at compile-time which prevented the program from being compiled."
Compile-time - 编译器尝试编译某些代码的时间段。示例:“编译器在编译时发现了 3 个类型错误,导致程序无法编译。”
Runtime - The period during which a program is executing. Example: "We did not spot the error until runtime because it was a logic error."
运行时间 - 程序执行的时间段。示例:“我们直到运行时才发现错误,因为这是一个逻辑错误。”
Run-time and virtual machines are two separate ideas - your first question doesn't make sense to me.
运行时和虚拟机是两个不同的想法 - 你的第一个问题对我来说没有意义。
Virtual Machines are indeed software programs that translate "object" [Java, C#, etc.] code into byte-code that can be run on a machine. If a language uses a virtual machine, it also often uses Just In Time compiling - which means that compile-time and run-time are in essence happening at the same time.
虚拟机确实是将“对象”[Java、C# 等] 代码转换为可以在机器上运行的字节码的软件程序。如果一种语言使用虚拟机,它也经常使用即时编译——这意味着编译时和运行时本质上是同时发生的。
Conversely, compiled languages like C, C++ are usually compiled into byte-code before being executed on a machine and therefore compile-time and run-time are completely separate.
相反,像 C、C++ 这样的编译语言通常在机器上执行之前被编译成字节码,因此编译时和运行时是完全分开的。
Generally "managed" languages have garbage collection (you don't directly manipulate memory with allocations and de-allocations [Java and C# are both examples]) and run on some type of virtual machine.
通常,“托管”语言具有垃圾收集功能(您不会通过分配和取消分配直接操作内存 [Java 和 C# 都是示例])并在某种类型的虚拟机上运行。
回答by Branko Dimitrijevic
The kind of code suitable for reasoning by human beings (let's call it "source code") needs to pass through several stages of translation before it can be physically executed by the underlying hardware (such as CPU or GPU):
那种适合人类推理的代码(我们称之为“源代码”)需要经过几个阶段的翻译才能被底层硬件(如 CPU 或 GPU)物理执行:
- Source code.
- [Optionally] intermediate code (such as .NET MSIL or Java bytecode).
- Machine code conformant to the target instruction set architecture.
- The microcode that actually flips the logical gates in silicon.
- 源代码。
- [可选] 中间代码(例如 .NET MSIL 或 Java 字节码)。
- 符合目标指令集架构的机器代码。
- 实际上翻转硅中逻辑门的微代码。
These translations can be done in various phases of the program's "lifecycle". For example, a particular programming language or tool might choose to translate from 1to 2when the developer "builds" the program and translate from 2to 3when the user "runs" it (which is typically done by a piece of software called "virtual machine"1that needs to be pre-installed on user's computer). This scenario is typical for "managed" languages such as C# and Java.
这些翻译可以在程序“生命周期”的各个阶段完成。例如,一个特定的编程语言或工具可能会选择翻译从1到2的时候开发商“建立”程序和翻译从2到3,当用户“运行”它(这通常是由一个软件叫“做虚拟机” 1,需要预先安装在用户的计算机上)。这种情况是典型的“托管”语言,如 C# 和 Java。
Or it could translate from 1to 3directly at build time, as common for "native" languages such as C and C++.
或者它可以在构建时直接从1转换为3,就像 C 和 C++ 等“原生”语言一样。
The translation between 3and 4is almost always done by the underlying hardware. It's technically a part of the "run time" but is typically abstracted away and largely invisible to the developer.
3和4之间的转换几乎总是由底层硬件完成。从技术上讲,它是“运行时”的一部分,但通常被抽象出来,并且在很大程度上对开发人员不可见。
The term "compile time" typically denotes the translation from 1to 2(or 3). There are certain checks that can be done at compile time before the program is actually run, such as making sure the types of arguments passed to a method match the declared types of method parameters (assuming the language is "strongly typed"). The earlier the error is caught, the easier it is to fix, but this has to be balanced with the flexibility, which is why some "scripting" languages lack comprehensive compile-time checks.
术语“编译时间”通常表示从1到2(或3)的转换。在程序实际运行之前,可以在编译时进行某些检查,例如确保传递给方法的参数类型与方法参数的声明类型匹配(假设语言是“强类型”的)。错误发现得越早,修复起来就越容易,但这必须与灵活性相平衡,这就是为什么某些“脚本”语言缺乏全面的编译时检查的原因。
The term "run-time" typically denotes the translation from 2(or 3) all the way down to 4. It is even possible to translate directly from 1at run-time, as done by so called "interpreted languages".
术语“运行时”通常表示从2(或3)一直到4 的转换。甚至可以在运行时直接从1进行翻译,就像所谓的“解释型语言”所做的那样。
There are certain kinds of problems that can't be caught at compile time, and you'll have to use appropriate debugging techniques (such debuggers, logging, profilers etc...) to identify them at run-time. The typical example of a run-time error is when you try to access an element of a collection that is not there, which could then manifest at run-time as an exception and is a consequence of the flow of execution too complex for the compiler to "predict" at compile time.
某些类型的问题在编译时无法捕获,您必须使用适当的调试技术(如调试器、日志记录、分析器等)在运行时识别它们。运行时错误的典型示例是当您尝试访问不存在的集合元素时,这可能会在运行时表现为异常,并且是由于执行流程对编译器来说过于复杂而导致的在编译时“预测”。
The "debug time" is simply a run-time while the debugger is attached to the running program (or you are monitoring the debug log etc.).
“调试时间”只是调试器连接到正在运行的程序(或者您正在监视调试日志等)时的运行时间。
1Don't confuse this with virtual machines that are designed to run native code, such as VMware or Oracle VirtualBox.
1不要将此与旨在运行本机代码的虚拟机混淆,例如 VMware 或 Oracle VirtualBox。