CS 111 Gree: Program Desig I Lecture 27: Speed (cot.); partig thoughts By Nascarkig - Ow work, CC BY-SA 4.0, https://commos.wikimedia.org/w/idex.php?curid=38671041 Robert H. Sloa (CS) & Rachel Poretsky (Bio) Uiversity of Illiois, Chicago December 5, 2017
Please complete the post-class survey (ow) https://uic.ualtrics.com/jfe/form/sv_9lycb6ztepivoll Lik also available from frot page of course Blackboard site
Questios o Phylogeetic Tree Assigmet?
How are you doig? It's the ed of the semester, a stressful time, for all of us, ad ed of the semester for our first year i college for may of us, a particularly stressful time. A. Not a care i the world B. Awfully busy but I'm feelig really fie C. Somewhat stressed D. Quite stressed E. Very stressed
SPEED AND LANGUAGE TRANSLATION: COMPILED VS. INTERPRETED
Some fial course mechaics: Profs do read Piazza Next class: Etirely review for fial if fial_exam(studet) >= 80: replace_ay_lower_hour_exams(studet)
Example from last time: 4 Lies Assembler LOAD R1,#65536 TEST R1,#13 JUMPTRUE #32768 CALL #16384 ; Get a character from keyboard ; Is it a ASCII 13 (Eter)? ; If true, go to aother part of the program ; If false, call fuc. to process the ew lie Machie Laguage (13 bytes): 05 01 255 255 10 01 13 20 127 255 122 63 255
Machie laguage is executed very uickly A mid-rage laptop these days has a clock rate of 1.3 2.6 Gigahertz. What that meas exactly is hard to explai, but let s iterpret it as processig 1.5 billio bytes per secod. Probably correct to withi multiplicative factor of 8 Certaily correct to withi multiplicative factor of 100 Those 13 bytes would execute iside the computer, the, i 13/1,500,000,000 th of a secod!
Applicatios are typically compiled Applicatios like Adobe Photoshop ad Microsoft Word are compiled. This meas that they execute i the computer as pure machie laguage. They execute at that level speed. However, Pytho, Java*, JavaScript, PHP, R, ad may other laguages are (i may cases) iterpreted. They execute at a slower speed. Why? It s the differece betwee traslatig istructios ad directly executig istructios. *Java techically itermediate case (complied to bytecode)
Why do we write programs? Oe reaso we write programs is to be able to do the same thig over-ad-over agai, without havig to rehash the same steps at the Pytho commad lie i the iterpreter widow each time
Applicatios are compiled Applicatios like Photoshop ad Word are writte i laguages like C or C++ These laguages are the compiled dow to machie laguage. That stuff that executes at a rate of 1.5 billio bytes per secod. Pytho programs are iterpreted.
High-level laguages must be traslated to machie laguage High-level laguage meas ay computer laguage except assembly laguage ad machie laguage All higher-level laguages must be traslated to machie laguage before they ca be executed. Two kids of traslators from source to target: Compiler: C, C++, FORTRAN Iterpreter: Pytho, JavaScript, PHP, Flash, PDF, Java*
Iterpreter idea (Usig Pytho to illustrate) Traslates Pytho code to machie laguage 1 lie at a time ad executes it Pytho Code Iterpreter prit ("Hello, World!") Probably i file hello.py somewhere For us, lower-right cosole widow of Spyder; could also use termial widow
Compiler idea (Usig C++ to illustrate) C++ Code #iclude <iostream> it mai() { cout << "Hello, World!"; } Compiler Machie Laguage a.out file This is a app you ca ru from, e.g., a termial widow.
Which is faster for same program & iputs? A. Ruig Pytho program B. Startig with C++ source code, traslatig it usig a C++ compiler, ad the ruig that program C. Not eough iformatio; it might be close betwee them D. No Clue
Which is faster for same program & iputs? A. Ruig Pytho program B. Startig with the result of compilig C++ source code, ad the ruig that compiled executable C. Not eough iformatio; it might be close betwee them D. No Clue
Java programs typically do t compile to machie laguage. Recall that every processor has its ow machie laguage. How, the, ca you create a program that rus o ay computer? The people who iveted Java also iveted a make-believe processor a virtual machie. It does t exist aywhere. Java compiles to ru o the virtual machie The Java Virtual Machie (JVM) The laguage it compiles to is called Java Bytecode
What good is it to ru oly o a computer that does t exist?!? Machie laguage is a very simple laguage. A program that iterprets the machie laguage of some computer is ot hard to write. def VMiterpret(program): for istructio i program: if istructio == 1: #It's a load... if istructio == 2: #It's a add...
Java rus o everythig Everythig that has a JVM o it! Each computer that ca execute Java has a iterpreter for the Java machie laguage, Java bytecode Iterpretig Java bytecode is pretty easy Takes oly a small fast program Devices as small as wristwatches ca ru Java VM iterpreters. Itermediate betwee compiled ad iterpreted laguage, with some of the beefits of both
Is it ay woder that Pytho programs ca be slower? Photoshop ad Word simply execute At 1.5 Ghz ad faster! Pytho programs iterpreted So for flat-out speed, people do't use Pytho But: 1. Some of the Java itermediate represetatio trick ideas have bee adopted by other laguages, icludig Pytho, so 2017 Pytho lots faster tha 2005 Pytho 2. Not so may thigs eed flat out speed today
Which is least likely to be developed i Pytho? A. Google puttig up results page for a Google search B. Large data sciece applicatio, like studyig Chicago crime rates by locatio, icome, educatio C. Graphics-itese multi-player computer game D. Large computatioal biology applicatios E. Automatig the testig of large pieces of software for errors
Why iterpret? For us, to have iterpreter area o RHS of Spyder Compiled laguages do t typically have a commad area where you ca prit thigs ad try out fuctios. Iterpreted laguages help the learer figure out what s goig o. Same advatage for scietist data user ad data scietist Aswer developers first; software developers secod ("ru oce") For others, to maitai portability. Java ca be compiled to machie laguage. I fact, some VMs will actually compile the virtual machie laguage for you while ruig o special compilatio eeded. But oce you do that, the result ca oly ru o oe kid of computer. Programs for Java (.jar files typically) ca be moved from ay kid of computer to ay other kid of computer ad just work.
Why else iterpret? Iterpreted laguages are usually more flexible, optimizig programmer time, ot ruig time. With great speedups i computers (Moore s) law, iterpreted laguages ted to be fast eough for most thigs today Hot laguages 1997 2007 maily iterpreted: Java*, Pytho, Perl, Flash Ad of those, Pytho ad Java still hot today ad (iterpreted) JavaScript ad R more importat today tha the
THERE ARE WORSE THINGS THAN BEING A LITTLE SLOWER THAN THE BEST
Iterpreted vs. compiled is't so bad Is at worst a costat multiplicative factor Ad so may good optimizatio tricks for Pytho by 2017 that it's ot a big multiplicatio factor
The Travelig Salesma Problem Imagie that you re a sales perso, ad you re resposible for a buch of differet cliets. Let s say 30 To be efficiet, you wat to fid the shortest path that will let you visit each cliet exactly oce, ad ot more tha oce. Beig a smart graduate of CS 111, you decide to write a program to do it.
The Travelig Salesma Problem curretly ca t be solved The best kow algorithm that gives a optimal solutio for the Travelig Salesma Problem takes! steps (That s factorial) There are algorithms that are better that give close-to but ot guarateed-best paths For 30 cities, the umber of steps to be executed is 265,252,859,812,191,058,636,308,480,000,000 (30!) The Travelig Salesma Problem is real. For example, several maufacturig problems actually become this problem, e.g., movig a robot o a factory floor to process thigs i a optimal order.
Class P, Itractable, ad Class NP May problems (like sortig elemets) ca be solved with a polyomial # of steps, like 2 We call that Class P problems. Other problems, like optimizatio, have kow solutios but are so hard ad big that we kow that we just ca t solve them a reasoable amout of time for eve reasoable amouts of data. We call these itractable Still other problems, like Travelig Salesma Problem seem itractable, but maybe there s a solutio i Class P that we just have t foud yet. We call these class NP Does P=NP? BIG QUESTION! (Also $1 millio prize)
The there are impossible problems There are some problems that are provably impossible. We kow that o algorithm ca ever be writte to solve this problem. The most famous of these is the Haltig Problem Which is, essetially, to write a program to completely uderstad ad debug aother program.
The Haltig Problem We could have writte programs that could read aother program (ope a file!). Spyder is i the ed just a program Ca we write a program that will iput aother program (say, from a file) ad tell us if the program will ever stop or ot? Thik about while loops with some complex expressio will the expressio ever be false? Now thik about ested while loops, all complex It s bee prove that such a program ca ever be writte.
Ala Turig Brilliat mathematicia ad computer scietist. Came up with mathematical defiitio of what a computer could do before oe was eve built! The Turig machie was iveted i aswer to the uestio of what the limits of mathematics were: What is computable by a fuctio? Proved that haltig problem had o solutio i 1936 almost 10 years before first computers were built.
Proof No program for Haltig Pr Imagie that there IS a program to check if a iput program halts or rus forever def doesithalt(prog, iput): We do t have its code; we re just assumig it exists, takes two iputs, returs True or False Will derive cotradictio/paradox
Gettig the Paradox def trouble(fuctio): if doesithalt(fuctio, fuctio): while (True): #I.e., ALWAYS! x = 1 else: retur #What does trouble(text-of-trouble) do?????
Ca we write a program that thiks? Are huma beigs doig (mere?) computatio? Ca huma itelligece be captured i a algorithm? Yes, we ca create ad uderstad programs, but some of our programs write programs too. Is it Class P? Class NP? Itractable? Are humas just computers i flesh? These are uestios that artificial itelligece researchers ad philosophers study today.