below i will provide to you the transcription of a video lesson called " Compiled and interpreted languages".. Study the content of it, find the meaning the speaker wants to convey and create a text, a lesson that will convey this meaning in an easy to understand way to college students new to the subjects discused in it. this is the transcript : ""1
00:00:00,000 --> 00:00:04,990
So we need to get our source code converted into machine code somehow before it
2
00:00:05,000 --> 00:00:09,990
can run and there are two main ways of doing this: what's called compiling the
3
00:00:10,000 --> 00:00:13,990
source code and what's called interpreting the source code.
4
00:00:14,000 --> 00:00:16,990
Now luckily, this is not a big decision you have to worry about.
5
00:00:17,000 --> 00:00:21,990
Most languages you'll deal with will naturally fall into one or the other, but
6
00:00:22,000 --> 00:00:22,990
it is worth knowing the difference.
7
00:00:23,000 --> 00:00:24,990
So let's have a simple scenario.
8
00:00:25,000 --> 00:00:30,990
Let's say it's just you and me. You have your computer and I have my computer
9
00:00:31,000 --> 00:00:34,990
and you're going to write a program that you want me to run.
10
00:00:35,000 --> 00:00:38,990
Now, with a compiled language, what happens is you write your source code and
11
00:00:39,000 --> 00:00:42,990
then you have a program called a compiler that will go through that source code
12
00:00:43,000 --> 00:00:48,990
and create a separate file that contains the machine code, and you just give me that file.
13
00:00:49,000 --> 00:00:53,990
This end result is sometimes referred to as an executable or an executable file
14
00:00:54,000 --> 00:00:55,990
because I can directly execute it.
15
00:00:56,000 --> 00:00:57,990
I can now just run your program.
16
00:00:58,000 --> 00:01:01,990
You keep your source code and I never see it.
17
00:01:02,000 --> 00:01:05,990
Now, with an interpreted language on the other hand, you don't compile your
18
00:01:06,000 --> 00:01:08,990
source code beforehand. You just give me a copy of it.
19
00:01:09,000 --> 00:01:14,990
So I'll need my machine to interpret it whenever I want to run your program.
20
00:01:15,000 --> 00:01:17,990
Now, interpreter is different to a compiler.
21
00:01:18,000 --> 00:01:18,990
It does this on-the-fly.
22
00:01:19,000 --> 00:01:22,990
We can think of it as going through your source code line by line and
23
00:01:23,000 --> 00:01:23,990
processing it on the spot.
24
00:01:24,000 --> 00:01:27,990
It does not save it as a separate machine code file.
25
00:01:28,000 --> 00:01:31,990
Now, you've used interpreted languages even if you don't know it.
Assistant
26
00:01:32,000 --> 00:01:34,990
Whenever you've looked at a webpage with JavaScript, which if you've surfed
27
00:01:35,000 --> 00:01:39,990
the web for more than two minutes in your lifetime you have, this is what's been happening.
28
00:01:40,000 --> 00:01:43,990
The JavaScript has been sent to you over the web along with a bunch of other
29
00:01:44,000 --> 00:01:48,990
files like webpages and images and it's been sent as source code onto your
30
00:01:49,000 --> 00:01:54,990
machine, and your web browser has just interpreted that JavaScript so it can run that code.
31
00:01:55,000 --> 00:01:55,990
So which one is best?
32
00:01:56,000 --> 00:01:58,990
Well, they both have their good and their bad points.
33
00:01:59,000 --> 00:02:00,990
Benefits of compiled code.
34
00:02:01,000 --> 00:02:04,990
Once it's compiled, it's immediately ready to run and you could send it to 100
35
00:02:05,000 --> 00:02:08,990
or 1,000 or 100,000 different people. It's ready to go.
36
00:02:09,000 --> 00:02:13,990
It can be optimized for a CPU, so it can actually be faster and you don't have
37
00:02:14,000 --> 00:02:17,990
to send your source code to everybody, which might be a good thing.
38
00:02:18,000 --> 00:02:22,990
However, the downsides are if I compile it on a PC, that executable file
39
00:02:23,000 --> 00:02:24,990
won't work on a Mac.
40
00:02:25,000 --> 00:02:28,990
In fact, it often needs to be compiled separately for different kinds of CPU
41
00:02:29,000 --> 00:02:33,990
even on the same platform, and when you're writing code to compile is an extra
42
00:02:34,000 --> 00:02:37,990
step that you have to take every time you want to test your program.
43
00:02:38,000 --> 00:02:41,990
Now, with interpreted code, the big benefits are I don't really care what kind
44
00:02:42,000 --> 00:02:45,990
of machine is on the other end, because we don't provide machine code.
45
00:02:46,000 --> 00:02:49,990
We just send the source code and we let the other side take care of it.
46
00:02:50,000 --> 00:02:53,990
So it can be more portable and more flexible across platforms.
47
00:02:54,000 --> 00:02:57,990
It's also a little easier when testing because you just write your source code
48
00:02:58,000 --> 00:03:00,990
and then run it, letting the interpreter take care of converting it.
49
00:03:01,000 --> 00:03:04,990
There is no in-between compile step.
50
00:03:05,000 --> 00:03:08,990
It can be easier to debug when things go wrong because you always have access to
51
00:03:09,000 --> 00:03:09,990
all the source code.
52
00:03:10,000 --> 00:03:13,990
However, it has its down sides too, because everyone who needs to run that
53
00:03:14,000 --> 00:03:18,990
program on their machine has to have an interpreter for that language on their machine.
54
00:03:19,000 --> 00:03:22,990
It also can be slower because you have to interpret it every time the program is run,
55
00:03:23,000 --> 00:03:26,990
and the source code is effectively public because you're sending it to
56
00:03:27,000 --> 00:03:28,990
everyone who needs to run that program.
57
00:03:29,000 --> 00:03:33,990
Now, because there are good things about compiled languages and good things
58
00:03:34,000 --> 00:03:38,990
about interpreted languages, there is also a third way of doing this which is a bit of both.
59
00:03:39,000 --> 00:03:43,990
Instead of the compiled model where all the work is done upfront but can be a
60
00:03:44,000 --> 00:03:48,990
little bit inflexible or the interpreted model where all the work is done on the
61
00:03:49,000 --> 00:03:54,990
receiving end but can be a little bit slower, we kind of do half-and-half.
62
00:03:55,000 --> 00:03:59,990
Upfront, we compile it part of the way to what's called an intermediate
63
00:04:00,000 --> 00:04:03,990
language, which takes it as far along the way to machine code as it can get
64
00:04:04,000 --> 00:04:06,990
while still being portable often across platforms.
65
00:04:07,000 --> 00:04:11,990
You then distribute this, sending it to the people who need to run it, and
66
00:04:12,000 --> 00:04:16,990
each person who runs it takes it the last step to take it to machine code on their computers.
67
00:04:17,000 --> 00:04:20,990
This is sometimes referred to as Just-In-Time or JIT compilation.
68
00:04:21,000 --> 00:04:25,990
Now, this intermediate language sometimes also goes by the name of bytecode.
69
00:04:26,000 --> 00:04:28,990
So this process has to happen somehow.
70
00:04:29,000 --> 00:04:33,990
It's just how much of it happens on your machine and how much of it happens on mine.
71
00:04:34,000 --> 00:04:39,990
Now, while theoretically all computer languages could use any of these methods,
72
00:04:40,000 --> 00:04:43,990
the normal usage of any one language tends to be one or the other.
73
00:04:44,000 --> 00:04:49,990
So for example, C, C++, and Objective-C, these are typically found as compiled
74
00:04:50,000 --> 00:04:51,990
languages, so you need a compiler.
75
00:04:52,000 --> 00:04:55,990
Now the compiler can be downloaded for free but they're often built into
76
00:04:56,000 --> 00:04:58,990
integrated development environment applications.
77
00:04:59,000 --> 00:05:03,990
Now, languages like PHP and JavaScript and indeed most languages with the word
78
00:05:04,000 --> 00:05:10,990
script at the end are usually interpreted, and languages like Java, C#, VB.NET,
79
00:05:11,000 --> 00:05:14,990
and Python use this intermediate hybrid approach.
80
00:05:15,000 --> 00:05:19,990
Now, whether a language is compiled or interpreted or somewhere in-between is
81
00:05:20,000 --> 00:05:23,990
rarely a reason by itself to choose a language, but it can be something that
82
00:05:24,000 --> 00:05:24,990
you take into account.
83
00:05:25,000 --> 00:05:30,990
If one main priority of your program is absolute maximum speed running on one
84
00:05:31,000 --> 00:05:34,990
single platform, you'll probably look at a compiled language.
85
00:05:35,000 --> 00:05:38,990
If you're more interested in easily moving your code across multiple platforms,
86
00:05:39,000 --> 00:05:41,990
you're probably more interested in an interpreted one.
87
00:05:42,000 --> 00:05:45,990
But more usually, you're driven more by what you need to do.
88
00:05:46,000 --> 00:05:49,990
You need to build iPhone apps or Windows desktop apps or dynamic website,
89
00:05:50,000 --> 00:05:54,990
or, in our case, just learn the fundamentals of programming, and you let that
90
00:05:55,000 --> 00:05:58,990
decision drive the language choice and the language choice will determine
91
00:05:59,000 --> 00:06:09,000
whether you are compiled, interpreted, or somewhere in the middle.
Compiled and Interpreted Languages: Understanding How Your Code Runs
Introduction
When you write a program, you write it in a programming language that humans can understand, like Python, Java, or C++. However, computers don't understand these languages directly. They only understand machine code, a series of binary instructions specific to the computer's processor. So, how does your human-readable code become machine-executable? The answer lies in compiling and interpreting—the two primary methods for translating source code into machine code.
This lesson will explore the differences between compiled and interpreted languages, the pros and cons of each approach, and how a hybrid method combines elements of both. By the end, you'll have a clear understanding of how your code gets executed on a computer.
Compiling vs. Interpreting: The Two Paths from Source Code to Machine Code
Compiled Languages
In the world of compiled languages, the journey from source code to machine code happens before the program runs. Here's how it works:
Writing the Source Code: You write your program in a high-level language like C++ or C#.
Compiling the Code: You use a compiler, a special program that translates your entire source code into machine code, producing an executable file (like .exe files on Windows).
Running the Program: You or someone else can run this executable file directly on a computer without needing the original source code.
Example Scenario:
Imagine you write a game in C++. You compile it, creating an executable file. You can send this executable to your friend, and they can play the game without seeing or needing your original C++ code.
Interpreted Languages
Interpreted languages take a different approach:
Writing the Source Code: You write your program in a language like JavaScript or Python.
Running the Program via an Interpreter: Instead of compiling, you run the source code directly using an interpreter, a program that reads your code line by line and executes it on the fly.
The Need for an Interpreter: Anyone who wants to run your program needs to have the interpreter for that language installed on their computer.
Example Scenario:
Suppose you write a script in Python. You share your Python file with a friend. For them to run it, they need to have the Python interpreter installed on their computer, which will read and execute your source code.
The Hybrid Approach: Just-In-Time Compilation
Some languages combine aspects of both compiling and interpreting, using a method known as Just-In-Time (JIT) compilation or bytecode interpretation.
Here's how it works:
Writing the Source Code: You code in a language like Java or C#.
Compiling to an Intermediate Language: You compile your code into an intermediate form known as bytecode. This bytecode is not specific to any one machine's processor.
Distributing the Bytecode: You share this bytecode with others.
Running the Program via a Virtual Machine: On the user's computer, a virtual machine (like the Java Virtual Machine or the .NET Common Language Runtime) interprets or compiles the bytecode into machine code specific to their system, often at runtime.
Example Scenario:
You write a Java application and compile it into bytecode (.class files). Your friend runs this bytecode using the Java Virtual Machine on their computer, which translates it into machine code that runs on their specific hardware.
Advantages and Disadvantages
Compiled Languages
Advantages:
Performance: Compiled programs generally run faster because the translation to machine code is done beforehand, and the code can be optimized specifically for the target machine.
No Source Code Distribution: You distribute the executable, not your source code, which helps protect your intellectual property.
Optimizations: Compilers can perform advanced optimizations to improve performance.
Disadvantages:
Platform Dependency: The executable is specific to a particular operating system and processor. A program compiled for Windows won't run on macOS or Linux.
Compilation Time: Every time you make changes, you need to recompile the program, which can be time-consuming for large projects.
Less Flexibility: Distributing updates can be more cumbersome since you need to provide a new executable for each platform.
Interpreted Languages
Advantages:
Portability: The same source code can run on any system that has the appropriate interpreter installed.
Ease of Testing and Debugging: You can test and run your code immediately without a separate compilation step, which speeds up development.
Flexibility: Interpreted languages often allow for more dynamic coding patterns.
Disadvantages:
Performance: Interpreted programs can run slower because the translation happens at runtime.
Interpreter Requirement: Users need to have the interpreter installed on their systems.
Source Code Exposure: Since you distribute the source code, others can view and possibly modify your code.
Hybrid Approach (JIT Compilation)
Advantages:
Balance of Performance and Portability: Bytecode is portable across systems, and JIT compilation can optimize performance at runtime.
Runtime Optimizations: The virtual machine can optimize the code based on the actual usage patterns.
Easier Updates: Distribute bytecode without worrying about platform-specific executables.
Disadvantages:
Interpreter/Virtual Machine Requirement: Users must have the appropriate virtual machine installed.
Startup Performance Hit: The initial compilation at runtime can cause a delay when starting the program.
Complexity: The system is more complex due to the combination of compilation and interpretation steps.
Examples of Languages by Execution Method
Compiled Languages:
C
C++
Objective-C
Interpreted Languages:
JavaScript
PHP
Ruby
Hybrid Languages (Bytecode and JIT Compilation):
Java
C#
Python
Choosing Between Compiled and Interpreted Languages
When deciding which type of language to use for a project, consider the following:
Performance Needs: If maximum performance is critical (e.g., systems programming, game engines), compiled languages might be preferable.
Portability Requirements: If you need your program to run on multiple platforms without modification, interpreted or hybrid languages could be more suitable.
Development Speed: Interpreted languages often allow for quicker development cycles due to easier testing and debugging.
Ecosystem and Libraries: Sometimes, the availability of libraries and tools in a language can influence your choice more than how it's executed.
Project Goals: The specific goals and requirements of your project should drive the decision.
Key Point: The choice between a compiled or interpreted language is just one factor among many. Often, the specific problem you're trying to solve or the platform you're targeting will determine the best language and execution method to use.
Conclusion
Understanding how your source code becomes machine code is fundamental to programming. Compiled and interpreted languages offer different advantages and challenges:
Compiled languages provide speed and optimized performance but require separate binaries for each platform and a compilation step after each change.
Interpreted languages offer flexibility and ease of use but might sacrifice performance and require an interpreter on the user's machine.
Hybrid approaches aim to combine the best of both worlds, offering portability and better runtime performance through techniques like Just-In-Time compilation.
As you continue your programming journey, being aware of these differences will help you make informed decisions about the languages and tools you use. Whether you're developing desktop applications, web services, mobile apps, or any other kind of software, the way your code is executed plays a crucial role in how you build and distribute your applications.
Remember, there's no one-size-fits-all answer. The best choice depends on your project's needs, your performance requirements, and the environments your applications will run in.
Further Reading and Exploration:
Experiment with both compiled and interpreted languages. Try writing a simple program in C (compiled) and Python (interpreted) to see the differences in development and execution.
Look into how Just-In-Time compilers work in languages like Java and C#.
Explore how modern JavaScript engines (like Google's V8) use JIT compilation to improve performance, blurring the lines between traditional interpreting and compiling.
By getting hands-on experience, you'll deepen your understanding of these concepts and become a more versatile programmer.