运行时和编译时的区别是什么?
当前回答
我认为它是错误的,以及什么时候可以发现错误。
编制时间:
string my_value = Console.ReadLine();
int i = my_value;
字符串值不能被赋给int类型的变量,因此编译器在编译时肯定知道这段代码有问题
运行时间:
string my_value = Console.ReadLine();
int i = int.Parse(my_value);
这里的结果取决于ReadLine()返回的字符串。有些值可以解析为int型,有些则不能。这只能在运行时确定
其他回答
编制时间:
在编译时执行的操作在最终程序运行时几乎不会产生任何开销,但在构建程序时可能会产生很大开销。
运行时:
或多或少完全相反。构建时成本小,运行程序时成本大。
从另一边;如果在编译时执行了某些操作,那么它只在您的机器上运行;如果在运行时执行了某些操作,那么它将在用户的机器上运行。
相关性
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.
(编辑:以下内容适用于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
编译时间:您作为开发人员编译代码的时间段。
运行时间:用户运行你的软件的时间段。
你需要更明确的定义吗?
这里有一个非常简单的答案:
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)
用户可以打开并运行编译后的程序。当应用程序正在运行时,它被称为运行时。
术语“运行时”和“编译时”经常被程序员用来指代不同类型的错误。编译时错误是一种问题,例如语法错误或缺少文件引用,从而阻止程序成功编译。编译器产生编译时错误,并通常指出源代码的哪一行导致了问题。
如果一个程序的源代码已经被编译成一个可执行程序,那么它在程序运行时可能仍然有错误。例子包括不能工作的特性、意外的程序行为或程序崩溃。这些类型的问题被称为运行时错误,因为它们发生在运行时。
参考
简单地说,b/w编译时间和运行时间的差异。
编译时:开发人员以.java格式编写程序,并将其转换为类文件字节码,在编译期间发生的任何错误都可以定义为编译时错误。
运行时:生成的.class文件被应用程序用于它的附加功能&逻辑是错误的,并抛出一个错误,这是一个运行时错误