264x Filetype PDF File size 0.24 MB Source: www.physics.rutgers.edu
KH Computational Physics- 2012 Programming
Short test of C++ knowledge
• Whatisaclass?
• Whatisvirtual function?
• Whatistemplate?
• Whatisimplicit type conversion?
• Whatmeansexplicit?
class A{
int a;
public:
explicit A(int a);
};
• Whatisreturn value optimization?
• Whatisthedifference between ”new” ”new[]” and ”operator new”?
• Whatisplacement new?
class A;
void memory = operator new(size);
*
a = new(memory) A;
• Whatismap?
• Whatisdeque?
Kristjan Haule, 2012 –1–
KH Computational Physics- 2012 Programming
Programminginhigh-level
languages
There is no ”perfect” computer language. The best choice depends on the problem.
For numerics, if speed is of upmost importance:
• fastest: Fortran77 (no aliasing)
• similarly fast: Fortran90, C, C++
• not so fast: Java, any interpreter (Perl, Python)
(aliasing: concurrent acces to a single entity causes synchonization problems and resuls in performace loss).
Humantimenecessarytowrite andmantain large scale projects:
• Python
• C++, Java
• C, Fortran 90
• Fortran77
Kristjan Haule, 2012 –2–
KH Computational Physics- 2012 Programming
For some purposes like manipulating files, writting small scripts or working with strings of
text (parsing) the best choice are interpreters: Phython, Perl...
Historical overview on computer language development can be found at
http://en.wikipedia.org/wiki/Timeline_of_programming_languages
There are thousands of programming languages and new ones are created every year.
Compilers: Fortran, C, C++, Pascal, (Java), ...
Interpreters: Perl, Phython, Mathematica, (Java), ...
Compilers: Code needsto be compiled and translated from the original language into
machine-code(assembler). Machine code in form of an executable can be run. When you
run the program, the computer executes the machine-code instructions in sequence.
• advantage: The machine-codecan be very optimized and execution is fast.
• dissadvantage:
– Theoriginal program is no longer connected to the machine-code, and mapping the
machine-codebackto the original program can be very difficult (hard job for
Kristjan Haule, 2012 –3–
KH Computational Physics- 2012 Programming
debugger, hard to connect run-time errors with the bugs in the original code)
– Eachprocessortype has a different machine code and thus requires a completely
different compiler.
– Thecodecan-notbechangedonfly,muchlessflexibleandusuallyharderto code.
Interpreters: Converts each line of a program into machine language as the statement is
encountered. If a statement is encountered multiple times (as occurs in a loop) the machine
mustconvert it to machine language each time.
• advantage:
– Easyto detect bugs because original program is still closely related to execution.
– Noseparateexecutablefile need be stored and the same code usually runs on
manyplatforms
– Usually offer more flexibility and provide more powerful statements (regular
expressions in Perl or Phython, Simplify,Solve,... in Mathematica,...)
– Codeoftheoriginal program can be changed on the fly (can be very powerful)
• dissadvantage: speed - Translating the same line of code into machine-code and then
executing the machine-code has quite a high overhead.
Kristjan Haule, 2012 –4–
no reviews yet
Please Login to review.