0:00:00.000,0:00:05.305 You've seen a little bit of JavaScript[br]code and in this section; I wanna talk 0:00:05.305,0:00:10.473 about what connect to that world to the[br]machine code in the CPU. So, this is 0:00:10.473,0:00:15.705 beyond generally the topic of computer[br]languages. So a programmer works in some 0:00:15.705,0:00:20.652 computer language and computer languages[br]are said to have high quote, high level 0:00:20.652,0:00:25.722 features and that means things like loops[br]and if statements and strings. Those are 0:00:25.722,0:00:30.545 facilities that are more complicated, that[br]are most present in a really simple 0:00:30.545,0:00:35.307 machine code on the CPU. So the programmer[br]writes what it's called source code in 0:00:35.307,0:00:39.623 whatever computer language they are[br]writing. So here is an example of some C++ 0:00:39.623,0:00:43.993 source code, this assigns a string into a[br]variable and then puts and exclamation 0:00:43.993,0:00:48.145 mark on the end of that string. You know[br]what? Some, it's kinda similar to the 0:00:48.145,0:00:52.624 JavaScript code we've seen A lot of these[br]languages at the syntactic level and of 0:00:52.624,0:00:57.927 their kinda similar to use of quotes and[br]semicolons or what not. So how do you get 0:00:57.927,0:01:04.232 from the C++ source code say to running on[br]a CPU? So one strategy used is what's 0:01:04.232,0:01:10.157 called a Compiler. And, the Compiler is a[br]separate program. And what the compiler 0:01:10.157,0:01:15.186 does is it looks at all of the source code[br]and it does a bulk translation over to a, 0:01:15.186,0:01:20.215 to create a big body of machine code. So[br]for example maybe there is a part of the 0:01:20.215,0:01:24.645 source code where there is an if[br]statement. And there is not a specific 0:01:24.645,0:01:29.555 instruction in a machine code for an if[br]statement but maybe there's a sequence of 0:01:29.555,0:01:34.404 five machine code instructions that in[br]effect make up an if statement and so the 0:01:34.404,0:01:40.991 compiler does that kind of expansion. So,[br]just an example Firefox is written in C++ 0:01:41.296,0:01:49.835 and so, to make a new version of Firefox[br]someone runs the C++ compiler and it reads 0:01:49.835,0:01:54.706 in. Of the many source files that adamant,[br]the great body of source code that makes 0:01:54.706,0:01:59.387 out Firefox and proves that essentially,[br]firefox.exe. Firefox.exe is the output of 0:01:59.387,0:02:04.449 the compiler to just translate all of that[br]source code. Couple of things to point out 0:02:04.449,0:02:09.180 about this The compilation only has to[br]happen once so somebody with source code, 0:02:09.180,0:02:13.340 someone makes Fi refox [inaudible] and[br]then you can just send out Firefox 0:02:13.340,0:02:17.728 [inaudible]. The end users don't need the[br]source code and they don't need the 0:02:17.728,0:02:22.231 compiler so the compilation can just sort[br]of to be done, done at the factory in 0:02:22.231,0:02:26.579 effect. Another fact, another feature that[br]this doesn't work backwards, So, I can 0:02:26.579,0:02:30.954 distribute firefox.exe all through this[br]machine code but there's not something 0:02:30.954,0:02:35.437 that will read that in and go backwards to[br]give me a big body of source code. Really 0:02:35.437,0:02:39.813 is not perfectly, you can try to do it[br]imperfectly but ideally, to produce a new 0:02:39.813,0:02:44.296 version you who really wants the source[br]code. So, let's talk about the source code 0:02:44.296,0:02:48.881 a little bit. So if you wanna add a[br]feature of fix a bug to Firefox, real way 0:02:48.881,0:02:53.774 to do that is to go back to the original[br]source code and rework it just what we've 0:02:53.774,0:02:58.549 done in JavaScript. So, add a few lines or[br]an If statement to change some behavior 0:02:58.549,0:03:03.560 and some way into source code and then run[br]the compiler again to make in effect a new 0:03:03.560,0:03:07.982 version of Firefox that has this, this[br]little change that you've put in the 0:03:07.982,0:03:12.315 source code. And just as were, we're[br]talking about source code. There's also 0:03:12.315,0:03:16.818 give you, gives you a sense of what Open[br]Source software means. So, Open Source 0:03:16.818,0:03:22.140 software is a format where the program is[br]distributed but also there's access to the 0:03:22.140,0:03:26.993 original source code to the program and[br]usually does come with a license saying, 0:03:26.993,0:03:31.788 hey here's the source code if you wanna[br]make your own version or if it's about 0:03:31.788,0:03:36.525 whatever, this program, feel free. So, the[br]two main features of Open Source software 0:03:36.525,0:03:40.970 is one, it's, it's typically distributed[br]for free so it's just, you know, cheap. 0:03:41.200,0:03:46.901 But the other feature is it comes with a[br]sort of freedom. Because if the software 0:03:46.901,0:03:51.031 is close source and you want some[br]different feature there is some bug that 0:03:51.031,0:03:55.050 you need implemented you're really[br]dependent on the vendor who holds the 0:03:55.050,0:03:59.069 source code like do they feel like[br]implementing that feature. In contrast 0:03:59.069,0:04:03.639 with open source software it's like there[br]is no handcuffs you know the source code 0:04:03.639,0:04:07.989 is available and if there is something[br]that you really wanted to do or say the 0:04:07.989,0:04:12.779 original vendor goes out of business then[br]because you got the source code you could 0:04:12.779,0:04:16.909 or you know maybe pay someone to make a[br]change and produce your own custom 0:04:16.909,0:04:21.534 version. And so it comes with this sort of[br]deep, you could take this as an insurance 0:04:21.534,0:04:25.615 policy. So often there's, there's many[br]different types of license terms for open 0:04:25.615,0:04:29.666 Source software but most often they[br]require in some cases that if you take the 0:04:29.666,0:04:33.569 source code and add some feature that you[br]then offer those changes back to the 0:04:33.569,0:04:37.916 community. So sort of in the same way that[br]you've benefited from others work they can 0:04:37.916,0:04:42.017 benefit from your work. In, in the details[br]on how that can be done is complicated. 0:04:42.017,0:04:45.920 There's, there's many different forms of[br]it. I'm gonna talk about open source 0:04:45.920,0:04:50.020 software a little bit later on but I just[br]want to mention it now because it really 0:04:50.020,0:04:54.170 gets up this idea of what is the source[br]code of a program versus just having the 0:04:54.170,0:05:01.476 .exe or something. So that's why I just[br]talked about C++. So there's, a whole 0:05:01.476,0:05:06.012 another very big category of languages. We[br]come in to call Dynamic Languages Or 0:05:06.012,0:05:10.437 sometimes interpreter languages. And[br]there's sort of a big strategy, there are 0:05:10.437,0:05:14.530 many different languages with many[br]different qualities and I'm just kinda 0:05:14.530,0:05:18.955 sort of [inaudible] for this large[br]category So examples of Dynamic Languages 0:05:18.955,0:05:24.275 currently are Java and JavaScripts and[br]Python. So one way that these can work is 0:05:24.275,0:05:30.125 that they are implemented by what is[br]called an interpreter as supposed to the 0:05:30.125,0:05:36.500 compiler case I was describing earlier so[br]an interpreter is a program which takes in 0:05:36.500,0:05:42.675 code written in some language and sort of[br]quote, unquote runs it so. Best example is 0:05:42.675,0:05:47.300 that there is this computer language[br]called JavaScript that actually we've been 0:05:47.300,0:05:51.925 using and web browsers which are programs[br]include as part of them a Java Script 0:05:51.925,0:05:56.607 interpreter and so when the web browser[br]comes across a page with some JavaScript 0:05:56.607,0:06:01.406 code in it, it can use the interpreter to[br]run that code to make it do whatever it's 0:06:01.406,0:06:07.550 going to do. So, an available detail, the[br]way an interpreter works. Is that it, it 0:06:07.550,0:06:11.296 deals with the program one line at a time.[br]So, if, so here's a little bit of 0:06:11.296,0:06:15.599 JavaScript code. If the interpreter wanted[br]t run this, well, we'll look at the first 0:06:15.599,0:06:19.749 line and it would look at what that line[br]says and then the interpreter would do it. 0:06:19.749,0:06:23.951 So in this case the interpreter would say[br]oh well, I guess I need a variable name a 0:06:23.951,0:06:28.102 and I need to put a one in it. So, once it[br]summed at that line it goes ahead and it 0:06:28.102,0:06:32.425 does the next line and so on. So the[br]compiler does this big bulk translation 0:06:32.425,0:06:36.822 from the source code into machine code.[br]But it's not really running code, it's 0:06:36.822,0:06:41.219 just doing a translation to be run in the[br]future. In contrast, the interpreter 0:06:41.219,0:06:45.616 really lives in the moment Just going[br]through the code line by line and just 0:06:45.616,0:06:50.440 doing it. So let me talk a little about[br]the differences between these two and I 0:06:50.440,0:06:55.552 should tell you as a disclaimer there are[br]many computer languages. There is no best 0:06:55.552,0:06:59.993 language. The old joke is that. Really[br]when, you know, you're on this seminar and 0:06:59.993,0:07:03.751 you are arguing about the pros and cons or[br]something. Ultimately, the answer is just 0:07:03.751,0:07:07.147 it depends. Like there's not a best[br]language, it's like well, what's the best 0:07:07.147,0:07:11.331 language for the situation with all the,[br]with all the feature. So broadly speaking, 0:07:11.331,0:07:16.214 compiler code does tend to run faster and[br]when you put, when you compile C++ code 0:07:16.214,0:07:21.158 and produce that EXE, it's sort of in a[br]sense lean. A lot of detail and decisions 0:07:21.158,0:07:26.844 has been stripped out as much as possible.[br]On the other hand, dynamic or interpreter 0:07:26.844,0:07:32.097 languages tend to have more features. More[br]features that make their programmers job a 0:07:32.097,0:07:37.287 little bit easier or put in another way.[br]Broadly speaking, programmers can tend to 0:07:37.287,0:07:42.193 get more work done per hour in dynamic[br]languages than they do in compiled 0:07:42.193,0:07:47.893 languages. However there is no free lunch.[br]It is also the case that dynamic language 0:07:47.893,0:07:53.196 code tends to run slower on the CPU in[br]terms of just getting work done compared 0:07:53.196,0:07:58.500 to a compiled language, compiled source[br]code that was going to do the same thing. 0:07:58.670,0:08:03.050 So, an example of this, a great example of[br]this is memory management. So, memory 0:08:03.050,0:08:07.886 management refers to the problem in the[br]code of knowing how many bytes are needed 0:08:07.886,0:08:12.323 to store something And in particular, for[br]how long. Well, I need these bytes for 0:08:12.323,0:08:17.101 here on line six but maybe on line twenty.[br]I don't need them anymore and I can reuse 0:08:17.101,0:08:21.424 those bytes for something else. And that[br]is actually a kind of a complicated 0:08:21.424,0:08:26.077 problem. In C and C++ memory management is[br]to a degree manual and that just means 0:08:26.077,0:08:31.103 that the programmer, they're producing all[br]these lines of codes to do stuff and there 0:08:31.103,0:08:36.011 is going to be times when they maybe to[br]add some lines to do some thinking to help 0:08:36.011,0:08:40.445 guide the memory management so there is[br]some programmer effort required. In 0:08:40.445,0:08:45.293 contrast most dynamic language have what's[br]called automatic memory management and 0:08:45.293,0:08:50.463 this means that. No programmer input is[br]required that the lower on the stacks that 0:08:50.463,0:08:55.605 the language is gonna notice automatically[br]when memory is needed; when memory is not 0:08:55.605,0:09:00.708 needed anymore it can be recycled and just[br]take on that whole problem. So, this fits 0:09:00.708,0:09:04.599 within the, the, the trend of dynamic[br]languages that I was mentioning earlier. 0:09:04.599,0:09:08.642 So this is an example of how well, you[br]know, because the programmers has less to 0:09:08.642,0:09:12.839 do they're able to just get stuff done a[br]little quicker, so memory management is a 0:09:12.839,0:09:16.781 great example of that. However, you can[br]also imagine this is one of the things 0:09:16.781,0:09:21.029 that makes dynamic language codes run a[br]little slower because the CPU now has this 0:09:21.029,0:09:25.380 additional bookkeeping problem where it's[br]looking at memory and trying to figure out 0:09:25.380,0:09:31.208 of when it can be reused. So, very broadly[br]speaking the trend for computer 0:09:31.208,0:09:37.768 programming today is towards dynamic[br]languages that the trade off that. The 0:09:37.768,0:09:42.448 programmer will get things done a little[br]quicker at the expense of using a little 0:09:42.448,0:09:47.128 more CPU or maybe a little more memory,[br]that's a pretty attractive payoff for many 0:09:47.299,0:09:51.865 for many problems. In this, maybe a little[br]unintuitive you can think off. We'll 0:09:51.865,0:09:56.145 what's the scariest quality for a computer[br]program and oftentimes, it's the 0:09:56.145,0:10:00.540 programmer that having skilled programmers[br]working on a problem who, you know, 0:10:00.540,0:10:05.010 working on those, working on these do[br]main. A lot of times there are hard to 0:10:05.010,0:10:10.046 find and so using up their hours. Using[br]few of their hours looks attractive. In 0:10:10.046,0:10:15.214 contrast the CPU only we want programs to[br]run as fast as possible but Cpus are 0:10:15.214,0:10:18.899 fairly, and if it got some fairly,[br]effective and there was even this funny 0:10:18.899,0:10:22.988 effect where Moore's Law keeps making it[br]more truth. Where the CPU in a fact each 0:10:22.988,0:10:26.775 year gets a little bit cheaper, but if you[br]think of the balance between the 0:10:26.775,0:10:30.460 programmer cost and the CPU cost of[br]getting something done, that means in 0:10:30.460,0:10:34.196 terms of the share of the pie, the[br]programmer is in a fact of getting more 0:10:34.196,0:10:38.285 expensive each year. A little bit more[br]scarce. So, this fits the pattern that on, 0:10:38.285,0:10:42.346 on, on, on whole, dynamic languages are[br]becoming a little bit more popular. Just 0:10:42.346,0:10:46.504 the last thing I'll mention is that[br]there's, I think a thing called a JIT, a 0:10:46.504,0:10:50.982 Just in Time compiler and this is sort of[br]the natural synthesis of the compiler and 0:10:50.982,0:10:55.353 interpretative strategies of trying to get[br]the best of both worlds. It's like, well, 0:10:55.353,0:10:59.138 we'd like to have a dynamic language with[br]all these features, all these 0:10:59.138,0:11:03.882 programmer-friendly features but we wanted[br]to run fast and so the JIT will take parts 0:11:03.882,0:11:07.827 of the code and try and compile them on[br]the fly and so I try I to sort of 0:11:07.827,0:11:13.244 [inaudible] the two approaches. It turns[br]out this works pretty well. So all modern 0:11:13.244,0:11:18.642 browsers now have jets for JavaScript code[br]so actually when you are running 0:11:18.642,0:11:24.963 JavaScript code even for this class inside[br]of the browser there was a jet that was in 0:11:24.963,0:11:30.219 a very physical way on the fly, taking[br]those scraps of JavaScript code and 0:11:30.219,0:11:35.390 compiling them on the fly, Anyway it, You[br]do not get exactly the formats of compiled 0:11:35.390,0:11:39.220 code but it gets fairly close. So, this[br]is, this is actually how most dynamic 0:11:39.220,0:11:43.102 languages were at first. And I'll say[br]well, it's an act the value of research 0:11:43.102,0:11:43.460 but it.