comparative Flashcards
imperative programming
the programmer
gives an explicit sequence of steps which
are done in order. (e.g. C++, Python, PHP,
Rust)
declarative
the
programmer describes the result they
need, and the language/compiler decides
how to best produce it. (e.g. Haskell)
high vs low level
Higher-level: more like what people want to
write/understand. (e.g. Python, Haskell)
Lower-level: more like what the computer
understands/executes. (e.g. assembly, C
is an interpreter purely interpreting code?
Most programs that appear to be interpreters
are actually both a compiler and interpreter.
what does a virtual machine represent
a virtual machine is an
interpreter for bytecode.
what are the pros for JIT
optimize machine code for the specific CPU architecture (e.g. Skylake vs generic x86);
collect usage stats and optimize output for the way code is actually being used;
create type-specific versions of functions for the types of arguments they actually receive (see “dynamic binding” , later).
what is JIT
Interpreting (byte)code at run-time always comes with a speed penalty. Even the most clever bytecode needs to be somehow processed while it’s running: that takes instructions and therefore time.
A Just-In-Time compiler starts with bytecode and either…
1. interprets but when it decides that some piece of logic will be used frequently, it…
2. for all code… … during execution, compiles that code to machine code and stores it in memory.
Then, when that code needs to execute (again), it can use the machine code version.
cons of JIT
But, JITs have to do their work during program execution. That will slow things down (at least momentarily). Performance may be less predictable.
are languages restricted to a specifc compilation method?
Any programming language*
could be compiled to machine
code, or to a VM, or interpreted
directly.
A question: does the compiler know a variable or value’s type when it compiles the program?
For statically typed languages, the answer is yes: C, Java, C#, Dart, etc.
Where the types aren’t known until runtime, the language is dynamically typed: Python, JavaScript, Lisp, Ruby, etc.
pros of statically typed langauges
Static:
In a statically typed language, you can have more confidence that the types are right.
pros an cons for dynamic typed langauges:
Dynamic:
Pros:
Dynamic typing allows more flexibility and often less code. The programmer doesn’t have to explicitly declare/allocate/type variables, which saves keystrokes/effort.
Cons:
types must be checked every time a line of code is executed.
what are 3 things that always have to be checked at run time
there are some related things that always have to be checked at runtime: array bounds, division by zero, overflow, etc.
what is gradual typing definition
where values/ variables can be given static types that can be checked at compile-time, but it’s optional.
when do we know which operation has to be applied for:
static
dynamic
Statically bound:
the compiler knows exactly which operation is going to happen.
Java
Dynamically bound:
the details have to be determined at run-time
Pyhton