10
Which Language is Better? A compiled language, C++, compared to an interpreted language, Matlab.

Which Language is Better? A compiled language, C++, compared to an interpreted language, Matlab

Embed Size (px)

Citation preview

Page 1: Which Language is Better? A compiled language, C++, compared to an interpreted language, Matlab

Which Language is Better?

A compiled language, C++, compared to an interpreted

language, Matlab.

Page 2: 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).

Page 3: Which Language is Better? A compiled language, C++, compared to an interpreted language, Matlab

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

Page 4: Which Language is Better? A compiled language, C++, compared to an interpreted language, Matlab

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.

Page 5: Which Language is Better? A compiled language, C++, compared to an interpreted language, Matlab

Two example programs that do the same thing

One program is written in C++.

One is written in Matlab.

Page 6: Which Language is Better? A compiled language, C++, compared to an interpreted language, 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);

Page 7: Which Language is Better? A compiled language, C++, compared to an interpreted language, Matlab

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.

Page 8: Which Language is Better? A compiled language, C++, compared to an interpreted language, Matlab

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.

Page 9: Which Language is Better? A compiled language, C++, compared to an interpreted language, Matlab

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.

Page 10: Which Language is Better? A compiled language, C++, compared to an interpreted language, Matlab

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.