0% found this document useful (0 votes)
38 views

Computer Language Tour C++ / Java / Python: Stanford CS107, Dec 2010

This document summarizes three popular computer languages: C++, Java, and Python. It discusses their differing strengths and how each is suited for different situations. C++ is best for performance-critical or small, simple projects. Java provides static typing which enables refactoring tools but code can be verbose. Python is best for small projects where brevity and flexibility are important, though it has the worst performance. The document also covers key concepts like compile-time vs. run-time typing and how just-in-time compilers like HotSpot optimize code.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Computer Language Tour C++ / Java / Python: Stanford CS107, Dec 2010

This document summarizes three popular computer languages: C++, Java, and Python. It discusses their differing strengths and how each is suited for different situations. C++ is best for performance-critical or small, simple projects. Java provides static typing which enables refactoring tools but code can be verbose. Python is best for small projects where brevity and flexibility are important, though it has the worst performance. The document also covers key concepts like compile-time vs. run-time typing and how just-in-time compilers like HotSpot optimize code.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Computer Language Tour

C++ / Java / Python

Stanford CS107, Dec 2010


Nick Parlante
But First -- Big, Obvious Ideas
There is no best programming language
Tradeoffs ... choosing the right language for a situation
Moore's Law .. CPUs getting cheaper and faster
Programmers are rare and in demand - reflects Moore
How many computer languages are there?
The answer is: 3
C++ / Java / Python
Look at differing strengths
Window into programming language choices
You should know these 3
Then using any other language is pretty easy
Compiler / JIT/ Interpreter
And I have too much teeny text on my slides, since really
they're just my notes to myself
Java - The Middle Language

(revisit compile time typing)


Java has a full compile time type system
Lots of type info making the code verbose
Compile time error detect, refactoring tools
Java ALSO tracks types at run time
e.g. ((String)x).toLowerCase()
Java mostly looks like a statically typed language
Its run time typing provides extra flexibility
Aside: cross-platform, vendor lock-in vs. competition
Aside 1 - Java Implementation

Java compiles to portable "bytecode" (diagram)


Like PDF, not tied to OS, CPU etc.
Old: interpreter runs bytecode
Modern: Just In Time compiler (JIT) -> native code
HotSpot, GPL open source from Sun/Oracle
HotSpot does aggressive re-writes of "hot" code
Done at run time -- HotSpot can see all the cards
IMHO: this is the future of interesting code optimizations --
run time re-writes of running code
Mozilla/Google JITs for JavaScript
it's a harder problem with dynamic languages
Aside 2 - HotSpot Optimization Simple

// HotSpot re-writes code to remove


// [i] bounds check (0, a.length)
int sum = 0;
for (int i=0; i<a.length; i++) {
sum += a[i]; // remove [i] bounds check
}

// The re-write must not change the program's


// output. The optimizer must in effect prove
// that a computation is not needed.
Aside 3 - HotSpot Optimization
More Complex
// Suppose have int[] p.a, int p.min
for (int i=0; i<p.a.length; i++)
if (p.min < p.a[i])
p.min = p.a[i]
// Plain code: dereferencing p.xxx in loop
// :( very sad
// HotSpot: before loop, r1=p.min r2=p.a
// dynamically re-code-gen the loop body to
// use r1/r2 registers directly.
// post loop, assign p.min = r1
// Native performance! (poss for Python?)
HotSpot: dynamic, aggressive re-writes of
code, no worries about linkage(!)
What is Compile Time Typing?
C/C++, Java
Compile time analysis
Just have the static code text
Hierarchy of declarations
System of declarations about will happen at run time
An approximate forecast, e.g.
This will be an int
This pointer will be one of A, B, C, but don't know which
x.foo() -- how do different language compilers treat this?
"no method foo() exists" -- when is this error possible?
Compile Time Typing Pro/Con
Advantages
Detect some errors at compile time
Better performance, decision not deferred to run time
e.g. a + b (int vs. string .. ct vs. rt decision)
Better tool refactoring and auto-complete (!)
+/- Readable/Verbose
Enables easier performance optimization (JIT compiler)
Dynamic language may get there someday
Disadvantages
Extra stuff to key in, more verbose code
May be hard to express some ideas in type system,
though they will actually work at run time (!)
Maintaining type info is work and can get in the way
Demo Eclipse: java code, verbose, error check, refactoring
Dynamic Typing

aka Run Time Typing, "scripting languages"


Python, Javascript, Ruby, ...
Simplest implementation
tag each value at run time with its type
Figure it all out as the program runs
i.e. decisions are defer-to-run-time
Typically run as an "interpreter"
Possible to JIT to native code, but the defer-to-rt semantics
are still present, even in the JIT native code
The PyPy project JITs Python code
Dynamic Typing Pro/Con
Advantages
Less to key in, less to get in the way (!!)
Language not limited to what can be expressed within
static typing structure, most flexible
Python has lots of features for a reason
Code is short, defer-to-rt is simple to implement
Interpreters are surprisingly easy to build
Disadvantages
In some ways hard to read -- type info not there
Find yourself adding it in the variable names
A clue that type info can be useful to the reader
Worse performance (defer decisions)
Worse compile time error detection
Compensate with unit tests
Worse compile time tool refactoring, auto-complete (!!)
Language Choice Realities 1
Reality: source code is high inertia, high cost (unintuitive?)
Not a criticism, just a reality you should plan for
1. Therefore: "Legacy" huge driver
We use language X because that's how this project
started 10 years ago, and now it weighs 10 million tons
Not an error, just rational
2. Therefore: Avoid building your system on top of locked-in,
proprietary infrastructure
Once your system develops high inertia,
you are screwed
Like building your extremely expensive building on
someone else's land
Standards: C, C++, Java, Python, HTML, JavaScript
Why standard/open-source infrastructure dominates
Language Choice Precepts 2
Warning: engineers can get excited and emotional about
language choice
Modern languages+tools are all pretty good
Aside: Bikeshed Painting (wikipedia)
People feel they should say something to not look
stupid. If something is simple, they are most comfortable
arguing about it.
Therefore most discussion is on the trivial.
Strategy: if it's kind of trivial, consider just letting the
implementer do it however they like, even if you prefer
the other way.
New skill to develop: "shutting up skills"
Conclusion -- Three Language Choices
3 Languages, all can work very well (Nick's opinion here)
"features" = language features + libraries
C/C++
High performance, Small mem use, Low dependency
Features are pretty weak (C++ 0x, limits of what can do)
Fit: small or simple, performance, (legacy, Google)
Java
Static typing, performance good (varies), features large
Fit: large or complex project, team project, tolerate
verbose
Python
Dynamic typing, performance the worst, features large (+
flexibility that comes with dynamic, no compile step!)
Small projects, where brevity/simplicity shines (IMHO)
For a 3 page script, I can write it correctly in one
sitting without putting in extra type info .. feels quick

You might also like