运行时和编译时的区别是什么?
当前回答
我认为它是错误的,以及什么时候可以发现错误。
编制时间:
string my_value = Console.ReadLine();
int i = my_value;
字符串值不能被赋给int类型的变量,因此编译器在编译时肯定知道这段代码有问题
运行时间:
string my_value = Console.ReadLine();
int i = int.Parse(my_value);
这里的结果取决于ReadLine()返回的字符串。有些值可以解析为int型,有些则不能。这只能在运行时确定
其他回答
运行时是指在运行程序时发生的事情。
编译时是指在编译程序时发生的事情。
您可以通过阅读实际代码来理解代码编译结构。运行时结构并不清楚,除非您了解所使用的模式。
我一直认为它与程序处理开销以及它如何影响性能有关,如前所述。一个简单的例子是,在代码中定义对象所需的绝对内存。
一个定义的布尔值占用x个内存,然后在编译后的程序中,不能更改。当程序运行时,它确切地知道为x分配多少内存。
另一方面,如果我只是定义了一个泛型对象类型(即一种未定义的占位符或可能是一个指向一些巨大blob的指针),我的对象所需的实际内存是不知道的,直到程序运行,我分配了一些东西给它,因此它必须评估和内存分配等,然后将在运行时动态处理(更多的运行时开销)。
如何动态处理它取决于语言、编译器、操作系统、你的代码等等。
然而,在这一点上,它实际上取决于您使用运行时和编译时的上下文。
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).
嗯,好吧,运行时是用来描述程序运行时发生的事情。
编译时间用来描述在构建程序(通常由编译器)时发生的事情。