【轉載】Runtime vs Compile time

Can anyone please give me a good understanding of whats the difference between run-time and compile-time?

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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

  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. Of 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. What can go wrong are run-time errors:

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

    Also there can be errors that are detected by the program itself:

    • 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 :-)

 

Very good answer for what it covers (+1) however you don't even touch on the meta-programing aspects of compile-time vs. run-time and that, IMHO, is the most interesting part. I'll grant, given this answer got accepted, that it may well be outside what the OP was looking for. – BCS May 11 '09 at 4:28
 
Nice, if somebody ask me about it during my lectures, I'll use your anwser :-) – e-satis Oct 31 '09 at 13:39
 
This is really nice answer. It is pretty clear and comprehensible. It is not easy to find that much clear answers in Google. – Tarik Nov 9 '09 at 7:48
 
Even if you've programmed a while it's still not easy to get...it's not just newbies man. Good question by the OP. – CoffeeAddict Jan 19 '11 at 5:50
 
"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..." I have no idea what you're saying here. Can you explain this in simple terms, not congested with all this technical garbage? – CoffeeAddict Jan 19 '11 at 5:52
 
 
 
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 

I think of it in terms of errors, and when they can be caught.

Compile time:

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

An int can't be assigned a string value, so the compiler can know for sure that this code has a problem, i.e. it can be caught at compile time

Run time:

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

Here the outcome depends on the value that was given by the user, some values can be parsed to an int, others can't i.e. it can only be caught at run time

 

Nice example. – BCS May 11 '09 at 0:42
 
Now this is something we can all understand. No general garbage tech words here. Nice. – CoffeeAddict Jan 19 '11 at 5:57
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 

(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:

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

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

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
Exceptions. Hashtable was one but I found the biggest step was .net 1.1 to .net 2.0, going from untyped to typed datasets (and now linq). Trying to troubleshoot a broken form with a dodgy database used to make me very sad! – Spence May 10 '09 at 21:35
----------------------------------------------------------------------------------------------------
 

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?

 

If that is what the OP is looking for, they are already a lost cause. – BCS May 11 '09 at 0:44
 
@BCS: The OP may have had a exceedingly simple introduction to programming using an interpreted or byte-compile-then-run-in-one-step language so that the distinction was never needed. The question in naive, but not dumb. – dmckee May 11 '09 at 2:15
 
@dmckee: I think this answer wouldn't even be of use to your user as it has no more information content than the original question. Anyone who would ask the question that this answer answers has no business programming (and I don't think the OP was asking that). – BCS May 11 '09 at 4:35
 
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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.

 

And it is easier to maintain compile time code than runtime bound code. At compile time you may use the compiler to check some stuff. The same stuff at runtime takes more time to check because involves testing. – user1154664

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

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

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

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

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

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

 
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

What is the difference between runtime and compile time?

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.

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.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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.

 

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.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

Run time means something happens when you run the program.

Compile time means something happens when you compile the program.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

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:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章