< Return to Video

Software II (11 mins)

  • 0:00 - 0:05
    You've seen a little bit of JavaScript
    code and in this section; I wanna talk
  • 0:05 - 0:10
    about what connect to that world to the
    machine code in the CPU. So, this is
  • 0:10 - 0:16
    beyond generally the topic of computer
    languages. So a programmer works in some
  • 0:16 - 0:21
    computer language and computer languages
    are said to have high quote, high level
  • 0:21 - 0:26
    features and that means things like loops
    and if statements and strings. Those are
  • 0:26 - 0:31
    facilities that are more complicated, that
    are most present in a really simple
  • 0:31 - 0:35
    machine code on the CPU. So the programmer
    writes what it's called source code in
  • 0:35 - 0:40
    whatever computer language they are
    writing. So here is an example of some C++
  • 0:40 - 0:44
    source code, this assigns a string into a
    variable and then puts and exclamation
  • 0:44 - 0:48
    mark on the end of that string. You know
    what? Some, it's kinda similar to the
  • 0:48 - 0:53
    JavaScript code we've seen A lot of these
    languages at the syntactic level and of
  • 0:53 - 0:58
    their kinda similar to use of quotes and
    semicolons or what not. So how do you get
  • 0:58 - 1:04
    from the C++ source code say to running on
    a CPU? So one strategy used is what's
  • 1:04 - 1:10
    called a Compiler. And, the Compiler is a
    separate program. And what the compiler
  • 1:10 - 1:15
    does is it looks at all of the source code
    and it does a bulk translation over to a,
  • 1:15 - 1:20
    to create a big body of machine code. So
    for example maybe there is a part of the
  • 1:20 - 1:25
    source code where there is an if
    statement. And there is not a specific
  • 1:25 - 1:30
    instruction in a machine code for an if
    statement but maybe there's a sequence of
  • 1:30 - 1:34
    five machine code instructions that in
    effect make up an if statement and so the
  • 1:34 - 1:41
    compiler does that kind of expansion. So,
    just an example Firefox is written in C++
  • 1:41 - 1:50
    and so, to make a new version of Firefox
    someone runs the C++ compiler and it reads
  • 1:50 - 1:55
    in. Of the many source files that adamant,
    the great body of source code that makes
  • 1:55 - 1:59
    out Firefox and proves that essentially,
    firefox.exe. Firefox.exe is the output of
  • 1:59 - 2:04
    the compiler to just translate all of that
    source code. Couple of things to point out
  • 2:04 - 2:09
    about this The compilation only has to
    happen once so somebody with source code,
  • 2:09 - 2:13
    someone makes Fi refox [inaudible] and
    then you can just send out Firefox
  • 2:13 - 2:18
    [inaudible]. The end users don't need the
    source code and they don't need the
  • 2:18 - 2:22
    compiler so the compilation can just sort
    of to be done, done at the factory in
  • 2:22 - 2:27
    effect. Another fact, another feature that
    this doesn't work backwards, So, I can
  • 2:27 - 2:31
    distribute firefox.exe all through this
    machine code but there's not something
  • 2:31 - 2:35
    that will read that in and go backwards to
    give me a big body of source code. Really
  • 2:35 - 2:40
    is not perfectly, you can try to do it
    imperfectly but ideally, to produce a new
  • 2:40 - 2:44
    version you who really wants the source
    code. So, let's talk about the source code
  • 2:44 - 2:49
    a little bit. So if you wanna add a
    feature of fix a bug to Firefox, real way
  • 2:49 - 2:54
    to do that is to go back to the original
    source code and rework it just what we've
  • 2:54 - 2:59
    done in JavaScript. So, add a few lines or
    an If statement to change some behavior
  • 2:59 - 3:04
    and some way into source code and then run
    the compiler again to make in effect a new
  • 3:04 - 3:08
    version of Firefox that has this, this
    little change that you've put in the
  • 3:08 - 3:12
    source code. And just as were, we're
    talking about source code. There's also
  • 3:12 - 3:17
    give you, gives you a sense of what Open
    Source software means. So, Open Source
  • 3:17 - 3:22
    software is a format where the program is
    distributed but also there's access to the
  • 3:22 - 3:27
    original source code to the program and
    usually does come with a license saying,
  • 3:27 - 3:32
    hey here's the source code if you wanna
    make your own version or if it's about
  • 3:32 - 3:37
    whatever, this program, feel free. So, the
    two main features of Open Source software
  • 3:37 - 3:41
    is one, it's, it's typically distributed
    for free so it's just, you know, cheap.
  • 3:41 - 3:47
    But the other feature is it comes with a
    sort of freedom. Because if the software
  • 3:47 - 3:51
    is close source and you want some
    different feature there is some bug that
  • 3:51 - 3:55
    you need implemented you're really
    dependent on the vendor who holds the
  • 3:55 - 3:59
    source code like do they feel like
    implementing that feature. In contrast
  • 3:59 - 4:04
    with open source software it's like there
    is no handcuffs you know the source code
  • 4:04 - 4:08
    is available and if there is something
    that you really wanted to do or say the
  • 4:08 - 4:13
    original vendor goes out of business then
    because you got the source code you could
  • 4:13 - 4:17
    or you know maybe pay someone to make a
    change and produce your own custom
  • 4:17 - 4:22
    version. And so it comes with this sort of
    deep, you could take this as an insurance
  • 4:22 - 4:26
    policy. So often there's, there's many
    different types of license terms for open
  • 4:26 - 4:30
    Source software but most often they
    require in some cases that if you take the
  • 4:30 - 4:34
    source code and add some feature that you
    then offer those changes back to the
  • 4:34 - 4:38
    community. So sort of in the same way that
    you've benefited from others work they can
  • 4:38 - 4:42
    benefit from your work. In, in the details
    on how that can be done is complicated.
  • 4:42 - 4:46
    There's, there's many different forms of
    it. I'm gonna talk about open source
  • 4:46 - 4:50
    software a little bit later on but I just
    want to mention it now because it really
  • 4:50 - 4:54
    gets up this idea of what is the source
    code of a program versus just having the
  • 4:54 - 5:01
    .exe or something. So that's why I just
    talked about C++. So there's, a whole
  • 5:01 - 5:06
    another very big category of languages. We
    come in to call Dynamic Languages Or
  • 5:06 - 5:10
    sometimes interpreter languages. And
    there's sort of a big strategy, there are
  • 5:10 - 5:15
    many different languages with many
    different qualities and I'm just kinda
  • 5:15 - 5:19
    sort of [inaudible] for this large
    category So examples of Dynamic Languages
  • 5:19 - 5:24
    currently are Java and JavaScripts and
    Python. So one way that these can work is
  • 5:24 - 5:30
    that they are implemented by what is
    called an interpreter as supposed to the
  • 5:30 - 5:36
    compiler case I was describing earlier so
    an interpreter is a program which takes in
  • 5:36 - 5:43
    code written in some language and sort of
    quote, unquote runs it so. Best example is
  • 5:43 - 5:47
    that there is this computer language
    called JavaScript that actually we've been
  • 5:47 - 5:52
    using and web browsers which are programs
    include as part of them a Java Script
  • 5:52 - 5:57
    interpreter and so when the web browser
    comes across a page with some JavaScript
  • 5:57 - 6:01
    code in it, it can use the interpreter to
    run that code to make it do whatever it's
  • 6:01 - 6:08
    going to do. So, an available detail, the
    way an interpreter works. Is that it, it
  • 6:08 - 6:11
    deals with the program one line at a time.
    So, if, so here's a little bit of
  • 6:11 - 6:16
    JavaScript code. If the interpreter wanted
    t run this, well, we'll look at the first
  • 6:16 - 6:20
    line and it would look at what that line
    says and then the interpreter would do it.
  • 6:20 - 6:24
    So in this case the interpreter would say
    oh well, I guess I need a variable name a
  • 6:24 - 6:28
    and I need to put a one in it. So, once it
    summed at that line it goes ahead and it
  • 6:28 - 6:32
    does the next line and so on. So the
    compiler does this big bulk translation
  • 6:32 - 6:37
    from the source code into machine code.
    But it's not really running code, it's
  • 6:37 - 6:41
    just doing a translation to be run in the
    future. In contrast, the interpreter
  • 6:41 - 6:46
    really lives in the moment Just going
    through the code line by line and just
  • 6:46 - 6:50
    doing it. So let me talk a little about
    the differences between these two and I
  • 6:50 - 6:56
    should tell you as a disclaimer there are
    many computer languages. There is no best
  • 6:56 - 7:00
    language. The old joke is that. Really
    when, you know, you're on this seminar and
  • 7:00 - 7:04
    you are arguing about the pros and cons or
    something. Ultimately, the answer is just
  • 7:04 - 7:07
    it depends. Like there's not a best
    language, it's like well, what's the best
  • 7:07 - 7:11
    language for the situation with all the,
    with all the feature. So broadly speaking,
  • 7:11 - 7:16
    compiler code does tend to run faster and
    when you put, when you compile C++ code
  • 7:16 - 7:21
    and produce that EXE, it's sort of in a
    sense lean. A lot of detail and decisions
  • 7:21 - 7:27
    has been stripped out as much as possible.
    On the other hand, dynamic or interpreter
  • 7:27 - 7:32
    languages tend to have more features. More
    features that make their programmers job a
  • 7:32 - 7:37
    little bit easier or put in another way.
    Broadly speaking, programmers can tend to
  • 7:37 - 7:42
    get more work done per hour in dynamic
    languages than they do in compiled
  • 7:42 - 7:48
    languages. However there is no free lunch.
    It is also the case that dynamic language
  • 7:48 - 7:53
    code tends to run slower on the CPU in
    terms of just getting work done compared
  • 7:53 - 7:58
    to a compiled language, compiled source
    code that was going to do the same thing.
  • 7:59 - 8:03
    So, an example of this, a great example of
    this is memory management. So, memory
  • 8:03 - 8:08
    management refers to the problem in the
    code of knowing how many bytes are needed
  • 8:08 - 8:12
    to store something And in particular, for
    how long. Well, I need these bytes for
  • 8:12 - 8:17
    here on line six but maybe on line twenty.
    I don't need them anymore and I can reuse
  • 8:17 - 8:21
    those bytes for something else. And that
    is actually a kind of a complicated
  • 8:21 - 8:26
    problem. In C and C++ memory management is
    to a degree manual and that just means
  • 8:26 - 8:31
    that the programmer, they're producing all
    these lines of codes to do stuff and there
  • 8:31 - 8:36
    is going to be times when they maybe to
    add some lines to do some thinking to help
  • 8:36 - 8:40
    guide the memory management so there is
    some programmer effort required. In
  • 8:40 - 8:45
    contrast most dynamic language have what's
    called automatic memory management and
  • 8:45 - 8:50
    this means that. No programmer input is
    required that the lower on the stacks that
  • 8:50 - 8:56
    the language is gonna notice automatically
    when memory is needed; when memory is not
  • 8:56 - 9:01
    needed anymore it can be recycled and just
    take on that whole problem. So, this fits
  • 9:01 - 9:05
    within the, the, the trend of dynamic
    languages that I was mentioning earlier.
  • 9:05 - 9:09
    So this is an example of how well, you
    know, because the programmers has less to
  • 9:09 - 9:13
    do they're able to just get stuff done a
    little quicker, so memory management is a
  • 9:13 - 9:17
    great example of that. However, you can
    also imagine this is one of the things
  • 9:17 - 9:21
    that makes dynamic language codes run a
    little slower because the CPU now has this
  • 9:21 - 9:25
    additional bookkeeping problem where it's
    looking at memory and trying to figure out
  • 9:25 - 9:31
    of when it can be reused. So, very broadly
    speaking the trend for computer
  • 9:31 - 9:38
    programming today is towards dynamic
    languages that the trade off that. The
  • 9:38 - 9:42
    programmer will get things done a little
    quicker at the expense of using a little
  • 9:42 - 9:47
    more CPU or maybe a little more memory,
    that's a pretty attractive payoff for many
  • 9:47 - 9:52
    for many problems. In this, maybe a little
    unintuitive you can think off. We'll
  • 9:52 - 9:56
    what's the scariest quality for a computer
    program and oftentimes, it's the
  • 9:56 - 10:01
    programmer that having skilled programmers
    working on a problem who, you know,
  • 10:01 - 10:05
    working on those, working on these do
    main. A lot of times there are hard to
  • 10:05 - 10:10
    find and so using up their hours. Using
    few of their hours looks attractive. In
  • 10:10 - 10:15
    contrast the CPU only we want programs to
    run as fast as possible but Cpus are
  • 10:15 - 10:19
    fairly, and if it got some fairly,
    effective and there was even this funny
  • 10:19 - 10:23
    effect where Moore's Law keeps making it
    more truth. Where the CPU in a fact each
  • 10:23 - 10:27
    year gets a little bit cheaper, but if you
    think of the balance between the
  • 10:27 - 10:30
    programmer cost and the CPU cost of
    getting something done, that means in
  • 10:30 - 10:34
    terms of the share of the pie, the
    programmer is in a fact of getting more
  • 10:34 - 10:38
    expensive each year. A little bit more
    scarce. So, this fits the pattern that on,
  • 10:38 - 10:42
    on, on, on whole, dynamic languages are
    becoming a little bit more popular. Just
  • 10:42 - 10:47
    the last thing I'll mention is that
    there's, I think a thing called a JIT, a
  • 10:47 - 10:51
    Just in Time compiler and this is sort of
    the natural synthesis of the compiler and
  • 10:51 - 10:55
    interpretative strategies of trying to get
    the best of both worlds. It's like, well,
  • 10:55 - 10:59
    we'd like to have a dynamic language with
    all these features, all these
  • 10:59 - 11:04
    programmer-friendly features but we wanted
    to run fast and so the JIT will take parts
  • 11:04 - 11:08
    of the code and try and compile them on
    the fly and so I try I to sort of
  • 11:08 - 11:13
    [inaudible] the two approaches. It turns
    out this works pretty well. So all modern
  • 11:13 - 11:19
    browsers now have jets for JavaScript code
    so actually when you are running
  • 11:19 - 11:25
    JavaScript code even for this class inside
    of the browser there was a jet that was in
  • 11:25 - 11:30
    a very physical way on the fly, taking
    those scraps of JavaScript code and
  • 11:30 - 11:35
    compiling them on the fly, Anyway it, You
    do not get exactly the formats of compiled
  • 11:35 - 11:39
    code but it gets fairly close. So, this
    is, this is actually how most dynamic
  • 11:39 - 11:43
    languages were at first. And I'll say
    well, it's an act the value of research
  • 11:43 - 11:43
    but it.
Title:
Software II (11 mins)
Video Language:
English
stanford-bot edited English subtitles for Software II (11 mins)
stanford-bot edited English subtitles for Software II (11 mins)
stanford-bot edited English subtitles for Software II (11 mins)
g3 edited English subtitles for Software II (11 mins)
g3 added a translation

English subtitles

Revisions