Interpreted languages like Ox, Matlab and Python have overhead costs in terms of computational time relative to compiled languages like C and FORTRAN. You might think this makes this comparison true:
compiled languages = fast?
interpreted languages = slow?
This section builds up a way to see the costs and benefits of the interpreted-language overhead.
To simplify the discussion, the term
i-code
refers to a program in an interpreted language and
c-code
to one in a compiled language that does the same thing. First, the reason interpreted languages have overhead is because code written in them does not get translated to machine code. Instead, the code gets interpreted by other machine code. So on top of the instructions to do the job there must be instructions to do the interpreting. Thus, in principle, i-code must run no faster equivalent c-code. But how much slower matters a great deal on what the code is doing and who wrote both programs (in the sense of how skilled they were and their concern for execution speed).
The "in principle" qualification assumes that the two codes are both equally well-crafted and optimized for speed. Instead, suppose two conditions hold. First, the interpreter was written and compiled with great care, and the i-code it is running is written carefully. Second, the c-code was written by a neophyte economics student. Then it is quite possible that the i-code will get the job done as fast or even faster than the c-code despite the overhead from the interpreter of the i-code.
It is also quite likely that the i-code would be much easier for the neophyte to write and debug if, for example, a mathematical i-language is used. And for a neophyte to write good c-code for a complex problem to exploit the inherent speed advantage of a compiled language may required much more
CodingTime
than using a good interpreted language.
Let's see this in action. You can do this demonstration yourself if you have a complete tool box described earlier, including
Ox
and
gcc
running on the same Unix machine. As before, we start with two very short and not-quite identical Ox and C programs.
21-ox-loop.ox
1: #include
2: main(){
3: decl i, j, x,t0,dur;
4: println("Experiment 1. 1 loop, 10 million trips");
5: t0 = timer();
6: x=0;
7: for (i=0; i<100000000; ++i) {
8: ++x;
9: }
10: dur = timespan(t0);
11: println("time= ",dur," ; x=",x);
12: println("Experiment 2. 1 loop, 10 million trips, entering a loop each time");
13: t0 = timer();
14: x=0;
15: for (i=0; i<100000000; ++i) {
16: for (j=0;j<1;++j)
17: ++x;
18: }
19: dur = timespan(t0);
20: println("time= ",dur," ; x=",x);
21: }
22-c-loop.c
1: #include
2: #include
3: main(){
4: int i, x, j;
5: clock_t t0;
6: double dur;
7: printf("Experiment 1. 1 loop, 10 million trips\n");
8: x=0;
9: t0 = clock();
10: for (i=0; i<100000000; ++i) {
11: ++x;
12: }
13: dur = ((double)(clock()-t0))/CLOCKS_PER_SEC;
14: printf(" time = %f ; x=%i \n",dur,x);
15: printf("\nExperiment 2. 1 loop, 10 million trips, entering a loop each time\n");
16: x=0;
17: t0 = clock();
18: for (i=0; i<100000000; ++i) {
19: for(j=0;j<1;++j)
20: ++x;
21: }
22: dur = ((double)(clock()-t0))/CLOCKS_PER_SEC;
23: printf(" time = %f ; x=%i \n",dur,x);
24: }
Loops c-loop.c
versus 21-loop.ox
gcc 22-c-loop.c ./a.out
Experiment 1. 1 loop, 10 million trips
time = 0.260000 ; x=100000000
Experiment 2. 1 loop, 10 million trips, entering a loop each time
time = 0.430000 ; x=100000000
oxl 21-ox-loop.ox
Experiment 1. 1 loop, 10 million trips
time= 11.81 ; x=100000000
Experiment 2. 1 loop, 10 million trips, entering a loop each time
time= 26.04 ; x=100000000