Upload
stephen-lambert
View
214
Download
1
Embed Size (px)
Citation preview
Which Language is Better?
A compiled language, C++, compared to an interpreted
language, Matlab.
Interpreted vs. Compiled
• In computer science, interpret means to execute, while compile means to translate into the language of the computer (also known as, the “machine”).– C++ is a compiled language. – Matlab is an interpreted language (so is
Java).
Compiled vs. Interpreted
Matlab program
translatorrunning
on computer
pcode Interpreter, which runs on computer, interprets
outputinput
C++ program
compilerrunning
on computer
machinecode Computer interprets
outputinput
Declared Variables vs. Undeclared Variables
• C++ requires that every variable be “declared” before it is used (like Java).– Declaration gives its name and “type”.– Use before assignment gives random result.
• Matlab does not require declaration.– Variable’s type is determined by its use.– Use before assignment causes error.
Two example programs that do the same thing
One program is written in C++.
One is written in Matlab.
// C++ program to calculate// weighted averagevoid main(void) { int N, i; float weights[3], sum_weights; float grades[3], sum, average; N = 3; cout << "Please input 3 grades: "; for (i = 0; i<N; i++){ cin >> grades[i];
} cout << "Please input 3 weights: "; for (i = 0; i<N; i++){ cin >> weights[i];
} sum = 0; sum_weights = 0; for (i = 0; i <N; i++){ sum += grades[i]*weights[i];
sum_weights += weights[i]; }
average = sum/sum_weights; cout << "Weighted average = " << average << endl; }
% Matlab program to calculate % weighted average
grades = input('Please input 3 grades: ');weights = input('Please input 3 weights: ');average = sum(grades.*weights)/sum(weights);fprintf('Weighted average = %f\n', average);
Reasons for both approaches
• Compiling emphasizes program speed – Declaring variables gives the compiler more
information for optimization.– The source code is translated into “native”
machine code (i.e., language of the computer).
• Interpreting emphasizes programming speed– Freedom from variable declaration simplifies
programming.– The interpreter can give detailed debugging
information.
Reasons for both approaches
SUMMARY:• Compiled programs run faster.• Interpreted programs are written faster.
Another advantage to C++: For big programs (>10,000 lines), C++ makes it easier to employ disciplined programming techniques that reduce programming errors.
Which language is better?
• For large programs that will be run by people who do not understand programming—C++ is best.
• For small engineering programs that will be run by people who do understand programming—Matlab is best.
Which language is better?
CONCLUSIONS:
• The language that best fits the task is the best language for that task.
• Until the task is specified, no language is better than any other.