[language-agnostic] Runtime vs. Compile time

What is the difference between run-time and compile-time?

This question is related to language-agnostic runtime compile-time

The answer is


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.


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:

int i = 2;
i += MY_CONSTANT;

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.


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

  1. The source code is fed into another program (usually called a compiler--go figure), which produces an executable program (or an error).
  2. 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)):

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


Run time means something happens when you run the program.

Compile time means something happens when you compile the program.


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.


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?


IMHO you need to read many links , resources to make an idea about the difference between Runtime vs Compile time because it is a very complex subject . I have list below some of this pictures/links that I am recommend .

Apart from what it is said above I want to add that sometimes a picture worth 1000 words :

  1. the order of this two: first is compile time and then you run A compiled program can be opened and run by a user. When an application is running, it is called runtime : compile time and then runtime1 compile time and then runtime1 ;

CLR_diag compile time and then runtime2  CLR_diag compile time and then runtime2

 from Wiki  

https://en.wikipedia.org/wiki/Run_time https://en.wikipedia.org/wiki/Run_time_(program_lifecycle_phase)

Run time, run-time, or runtime may refer to:

Computing

Run time (program lifecycle phase), the period during which a computer program is executing

Runtime library, a program library designed to implement functions built into a programming language

Runtime system, software designed to support the execution of computer programs

Software execution, the process of performing instructions one by one during the run time phase

enter image description here enter image description here

enter image description here enter image description here enter image description here enter image description here enter image description here List of compilers enter image description here enter image description here https://en.wikipedia.org/wiki/List_of_compilers

  • search on google and compare runtime errors vs compile errors:

runtime errors

compile errors ;

  1. In my opinion a very important thing to know : 3.1 the difference between build vs compile and the Build Lifecycle https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

3.2 the difference between this 3 things : compile vs build vs runtime

https://www.quora.com/What-is-the-difference-between-build-run-and-compile Fernando Padoan, A developer that's just a bit curious for language design Answered Feb 23 I’m going backwards in relation to other answers:

running is getting some binary executable (or a script, for interpreted languages) to be, well… executed as a new process on the computer; compiling is the process of parsing a program written in some high level language (higher if compared to machine code), checking it’s syntax, semantics, linking libraries, maybe doing some optimization, then creating a binary executable program as an output. This executable may be in the form of machine code, or some kind of byte code — that is, instructions targeting some kind of virtual machine; building usually involves checking and providing dependencies, inspecting code, compiling the code into binary, running automated tests and packaging the resulting binary[ies] and other assets (images, configuration files, libraries, etc.) into some specific format of deployable file. Note that most processes are optional and some depend on the targeted platform you are building for. As an example, packaging a Java application for Tomcat will output a .war file. Building a Win32 executable out of C++ code could just output the .exe program, or could also package it inside a .msi installer.


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


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;

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:

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

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


The major difference between run-time and compile time is:

  1. If there are any syntax errors and type checks in your code,then it throws compile time error, where-as run-time:it checks after executing the code. 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.

  1. Compile time doesn't look for output of functionality provided by your code, whereas run-time does.

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.


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


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


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.


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.


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:


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 as Int.

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


public class RuntimeVsCompileTime {

public static void main(String[] args) {

    //test(new D()); COMPILETIME ERROR
    /**
     * Compiler knows that B is not an instance of A
     */
    test(new B());
}

/**
 * compiler has no hint whether the actual type is A, B or C
 * C c = (C)a; will be checked during runtime
 * @param a
 */
public static void test(A a) {
    C c = (C)a;//RUNTIME ERROR
}

}

class A{

}

class B extends A{

}

class C extends A{

}

class D{

}


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.


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.


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


Here is a quote from Daniel Liang, author of 'Introduction to JAVA programming', on the subject of compilation:

"A program written in a high-level language is called a source program or source code. Because a computer cannot execute a source program, a source program must be translated into machine code for execution. The translation can be done using another programming tool called an interpreter or a compiler." (Daniel Liang, "Introduction to JAVA programming", p8).

...He Continues...

"A compiler translates the entire source code into a machine-code file, and the machine-code file is then executed"

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


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.


(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

Look into this example:

public class Test {

    public static void main(String[] args) {
        int[] x=new int[-5];//compile time no error
        System.out.println(x.length);
    }}

The above code is compiled successfully, there is no syntax error, it is perfectly valid. But at the run time, it throws following error.

Exception in thread "main" java.lang.NegativeArraySizeException
    at Test.main(Test.java:5)

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.


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


Examples related to language-agnostic

IOException: The process cannot access the file 'file path' because it is being used by another process Peak signal detection in realtime timeseries data Match linebreaks - \n or \r\n? Simple way to understand Encapsulation and Abstraction How can I pair socks from a pile efficiently? How do I determine whether my calculation of pi is accurate? What is ADT? (Abstract Data Type) How to explain callbacks in plain english? How are they different from calling one function from another function? Ukkonen's suffix tree algorithm in plain English Private vs Protected - Visibility Good-Practice Concern

Examples related to runtime

how can I debug a jar at runtime? How to check whether java is installed on the computer Excel VBA Code: Compile Error in x64 Version ('PtrSafe' attribute required) Excel VBA Run-time error '424': Object Required when trying to copy TextBox Execute external program An unhandled exception of type 'System.IO.FileNotFoundException' occurred in Unknown Module Error when checking Java version: could not find java.dll Change the location of an object programmatically java.lang.NoClassDefFoundError: Could not initialize class XXX How permission can be checked at runtime without throwing SecurityException?

Examples related to compile-time

What is the equivalent of Java's System.out.println() in Javascript? C++ Get name of type in template Runtime vs. Compile time