Board index » delphi » Why TP is slower than TC ?

Why TP is slower than TC ?

I wrote two identical programs in Turbo Pascal version 7 and Turbo C++
version 3 . I then compiled and run the programs on the same computer
and to my surprise the execution time of the program written in Turbo
C++ version was nearly 50% faster than Turbo Pascal version.

I had heard that C based language are fast but I did not expect it to
be that fast. Can someone explain to me why there is such a difference
between the performance of the two compiler ? I should say that I
prefere to write my programs in Pascal but after seeing the
performance of C, I am planning to switch C. Am I doing the right
thing ?

Regards

M. Abdollahi

 

Re:Why TP is slower than TC ?


Quote
Mehrzad Abdollahi wrote:

> I wrote two identical programs in Turbo Pascal version 7 and Turbo C++
> version 3 . I then compiled and run the programs on the same computer
> and to my surprise the execution time of the program written in Turbo
> C++ version was nearly 50% faster than Turbo Pascal version.

> I had heard that C based language are fast but I did not expect it to
> be that fast. Can someone explain to me why there is such a difference
> between the performance of the two compiler ? I should say that I
> prefere to write my programs in Pascal but after seeing the
> performance of C, I am planning to switch C. Am I doing the right
> thing ?

> Regards

> M. Abdollahi

As far as I know the C compiler is a multipass compiler, and the pascal
compiler is a onepass. Than means that the C compiler has a better chanse
of optimizing than the pascal compiler, but it may take some more time to
compile. If you rewrite some of the timecritical and obvious timespending
pascal code in assembler, I think you may come near the C version.

- Asbj?rn

Re:Why TP is slower than TC ?


Quote
In article <32c96253.2113568@news> M.Abdoll...@bham.ac.uk (Mehrzad Abdollahi) writes:
>I wrote two identical programs in Turbo Pascal version 7 and Turbo C++
>version 3 . I then compiled and run the programs on the same computer
>and to my surprise the execution time of the program written in Turbo
>C++ version was nearly 50% faster than Turbo Pascal version.
>I had heard that C based language are fast but I did not expect it to
>be that fast. Can someone explain to me why there is such a difference
>between the performance of the two compiler ? I should say that I
>prefere to write my programs in Pascal but after seeing the
>performance of C, I am planning to switch to C. Am I doing the right
>thing ?

No, Mehrzad, I think you are really jumping the gun.  Pascal is not
"categorically" faster or slower than C or C++; it depends totally upon the
compiler you use, the runtime library routines you use, and the overall
implementation.

The only way to realistically determine where your program is spending its
time is with a profiler.  This can reveal interesting things.  I believe that
you will find that the speed of the overall program (main-body code) will be
about the same in most cases .. there's only so many ways to write a "for"
loop .. but you may find surprising differences in the runtime libraries.  You
may also discover, if you're more familiar with one language than the other,
that you did not, in fact, write them exactly the same.

And finally, consider whether you inadvertantly committed "benchmark sins."  
Certain algorithms, especially CPU-intensive ones, might make a lesser
compiler look a -lot- "lesser" than its neighbors, when the same compiler
would perform quite well in more typical, I/O-intensive applications.

/mr/

Re:Why TP is slower than TC ?


In article <32c96253.2113568@news>,

Quote
Mehrzad Abdollahi <M.Abdoll...@bham.ac.uk> wrote:

>I wrote two identical programs in Turbo Pascal version 7 and Turbo C++
>version 3 . I then compiled and run the programs on the same computer
>and to my surprise the execution time of the program written in Turbo
>C++ version was nearly 50% faster than Turbo Pascal version.

>I had heard that C based language are fast but I did not expect it to
>be that fast. Can someone explain to me why there is such a difference
>between the performance of the two compiler ? I should say that I
>prefere to write my programs in Pascal but after seeing the
>performance of C, I am planning to switch C. Am I doing the right
>thing ?

It is not that C is inherently faster. Turbo C just produces better code
than Turbo Pascal. Partially this is because it uses registers (SI/DI)
as variables. The C language provides some features, like user defined
register variables, notations like i++, that make it easier to produce
efficient code. A good optimizer can, however, produce fast code even
without them.

Is the speed increase really relevant?

BTW even BP produces bad code compared to other C compilers (or other
compilers in general).

Osmo

Re:Why TP is slower than TC ?


Quote
Mehrzad Abdollahi wrote:
> I wrote two identical programs in Turbo Pascal version 7 and Turbo C++
> version 3 . I then compiled and run the programs on the same computer
> and to my surprise the execution time of the program written in Turbo
> C++ version was nearly 50% faster than Turbo Pascal version.

Some years ago, a colleague of mine and I were challenging each other to
write programs. I used Turbo Pascal and he used Turbo C++. In an
overwhelming majority of the contests the pascal program was not only
developed faster but also executed significantly faster. The difference
wasn't the compiler, but rather the fact that I was intimate with how my
compiler worked and he couldn't say the first thing about how his
compiler produced code. Neither of us was using any Assembly or inline
code.

I hear entirely too many people saying that Turbo Pascal is simply a bad
compiler that produces inefficient code. I disagree. Overall, I am very
pleased with my TP/BP compiler. More often than not, the people who
complain about inefficient compilers are not taking full advantage of
the compiler that they are using, or don't understand that there may be
fifty ways to solve a problem and they chose the wrong one.

Quote
> I should say that I
> prefere to write my programs in Pascal but after seeing the
> performance of C, I am planning to switch C. Am I doing the right
> thing ?

Why switch? Learning C can only be a plus to your resume, but you don't
need to drop Pascal to learn C.

AME

Re:Why TP is slower than TC ?


Quote
> I hear entirely too many people saying that Turbo Pascal is simply a bad
> compiler that produces inefficient code. I disagree. Overall, I am very
> pleased with my TP/BP compiler. More often than not, the people who
> complain about inefficient compilers are not taking full advantage of
> the compiler that they are using, or don't understand that there may be
> fifty ways to solve a problem and they chose the wrong one.

   Absolutely.  I second your thoughts!

Re:Why TP is slower than TC ?


Mehrzad Abdollahi <M.Abdoll...@bham.ac.uk> wrote in article
<32c96253.2113568@news>...

Quote

> I wrote two identical programs in Turbo Pascal version 7 and Turbo C++
> version 3 . I then compiled and run the programs on the same computer
> and to my surprise the execution time of the program written in Turbo
> C++ version was nearly 50% faster than Turbo Pascal version.

Are you sure you used the same compiler/linker settings in each language.
Simply leaving range checking,stack checking etc on can slow a BP7 program
a lot. My experience with an FFT routine is that both compilers generate
very similiar code and run at about the same speed.
--Frank--
Quote

Re:Why TP is slower than TC ?


In comp.lang.pascal.borland, Mehrzad Abdollahi wrote:

Quote

>I wrote two identical programs in Turbo Pascal version 7 and Turbo C++
>version 3 . I then compiled and run the programs on the same computer
>and to my surprise the execution time of the program written in Turbo
>C++ version was nearly 50% faster than Turbo Pascal version.

>I had heard that C based language are fast but I did not expect it to
>be that fast. Can someone explain to me why there is such a difference
>between the performance of the two compiler ?

Did you compile the Pascal version with run time checks enabled ? This
can easily make a 50 % difference. If the programs really were
identical in sytntax then I would expect the run times to be very
similar, but it is easy to hit something that makes a significant slow
down.

If you mean the programs were identical in function, then use of
pointers in C as opposed to array subscripts in Pascal can make a
significant difference.

If the code is short, it would be interesting to see it.

Quote
>I should say that I
>prefer to write my programs in Pascal but after seeing the
>performance of C, I am planning to switch C. Am I doing the right
>thing ?

No. Basing a decision purely on speed as a result of a probably
unrepresentative test is a bad idea. You should consider other factors
such as quality of the code, correctness etc. (What does a[i] = i++ do
in C?).

A Formula 1 car (or Indy car) is faster than a saloon car, but is more
difficult to drive. Which would you use to a) go racing b) take the
kids to school?

Regards,
--
Bob Cousins, Software Engineer.
Home page at http://www.demon.co.uk/sirius-{*word*104}netics/

Please do NOT use my email address on mailing lists without my prior permission.

Re:Why TP is slower than TC ?


On Sat, 04 Jan 1997 17:12:26 GMT, b...@lintilla.demon.co.uk (Bob

Quote
Cousins) wrote:
>In comp.lang.pascal.borland, Mehrzad Abdollahi wrote:

>>I wrote two identical programs in Turbo Pascal version 7 and Turbo C++
>>version 3 . I then compiled and run the programs on the same computer
>>and to my surprise the execution time of the program written in Turbo
>>C++ version was nearly 50% faster than Turbo Pascal version.

>>I had heard that C based language are fast but I did not expect it to
>>be that fast. Can someone explain to me why there is such a difference
>>between the performance of the two compiler ?

>Did you compile the Pascal version with run time checks enabled ? This
>can easily make a 50 % difference. If the programs really were
>identical in sytntax then I would expect the run times to be very
>similar, but it is easy to hit something that makes a significant slow
>down.

>If you mean the programs were identical in function, then use of
>pointers in C as opposed to array subscripts in Pascal can make a
>significant difference.

>If the code is short, it would be interesting to see it.

>>I should say that I
>>prefer to write my programs in Pascal but after seeing the
>>performance of C, I am planning to switch C. Am I doing the right
>>thing ?

>No. Basing a decision purely on speed as a result of a probably
>unrepresentative test is a bad idea. You should consider other factors
>such as quality of the code, correctness etc. (What does a[i] = i++ do
>in C?).

>A Formula 1 car (or Indy car) is faster than a saloon car, but is more
>difficult to drive. Which would you use to a) go racing b) take the
>kids to school?

>Regards,
>--
>Bob Cousins, Software Engineer.
You wrote:
>  How was your Pascal compiler switches?

I have not changed the switches in the Turbo Pascal compiler, and
everything is as the default setting. As I remember in the default
setting all the switches are on. I also did not changed the default
setting of the Turbo C++ compiler. I guess the default switch setting
in the Turbo C++ compiler is on. The programs are as follows:

Turbo Pascal version:
--------------------------
var
   i,j: integer;
   a:real;
begin
   writeln('Start:');
   a:=0;
   for i:=1 to 300 do
      begin
          for j:=1 to 3000 do a:=a+1;
          a:=a/2;
      end;
   writeln('Finish ',i,' ',j,' ',a);
end.

Turbo C++ version:
------------------------
#include <stdio.h>
int   i,j;
float  a;
int main()
{
  printf("Start:\n");
   a=0;
   for ( i=1; i < 300; i++) {
         for (j=1; j < 3000; j++) a=a+1;
          a=a/2;
         }
   printf"'Finish %d  %d  %f\n",i,j,a);
return(0);

Quote
}

Thanks very much for your comments.
Regards
Mehrzad Abdollahi

Re:Why TP is slower than TC ?


Quote
Mehrzad Abdollahi wrote:

[snip]

Quote
> >  How was your Pascal compiler switches?

> I have not changed the switches in the Turbo Pascal compiler, and
> everything is as the default setting. As I remember in the default
> setting all the switches are on. I also did not changed the default
> setting of the Turbo C++ compiler. I guess the default switch setting
> in the Turbo C++ compiler is on. The programs are as follows:

Well, there you are. Switch off the rangechecking, and your (pascal)
program goes 45% faster.

Quote
> Turbo Pascal version:
> --------------------------
> var
>    i,j: integer;
>    a:real;

And switching to single of double wouldn't hurt to much either.

[snip]

- Asbj?rn

Re:Why TP is slower than TC ?


Quote
In article <32D1A1FF....@sn.no>, Asbj?rn <bh...@sn.no> wrote:

snip
Quote
>#Well, there you are. Switch off the rangechecking, and your (pascal)
>program goes 45% faster.

>And switching to single of double wouldn't hurt to much either.

[snip]

And if I remember correctly it will even run faster if you replace
a := a /2 by a := a * 0.5.

Alex

Re:Why TP is slower than TC ?


Quote
In article <32d15be6.343830@news>, M.Abdoll...@bham.ac.uk (Mehrzad Abdollahi) wrote:
>Turbo Pascal version:
>--------------------------
>var
>   i,j: integer;
>   a:real;
>begin
> [Snip]
>Turbo C++ version:
>------------------------
>#include <stdio.h>
>int   i,j;
>float  a;
>int main()

[Snip]

Aha!  Real is not the equivalent of float.  If you change the declaration of
variable "a" in the pascal version to "float", and rerun it, you should see
some better numbers.  (On my machine there was a factor of 10 increase in
speed by doing this).

Just for fun, I ran your example programs through both the delphi 2.0 and the
BC 5.01 compilers and ran them.  Below are the results (200 Mhz P6, NT
Workstation 4.0).   Timing was done by calling "GetProcessTimes" function in
NT.  I increased the loop limits to make it run long enough to get a
reasonable measurement.

/* All optimizations on - 30000 on outside loop */
/* Delphi 2.0 */
D:\Test>testp
Start:
Finish 30001 3001  3.00000000000000E+0003
Kernel: 0
User: 3.6562500 seconds

D:\Test>testc
/* Borland C 5.01 */
Start:
Finish 30001  3001  3000.000000
Kernel: 0
User: 3.2031250 seconds

Here are the file sizes:

01/07/97  01:18p                36,864 testc.EXE
01/07/97  01:16p                 9,728 TestP.exe

Brian

Re:Why TP is slower than TC ?


Quote
> >#Well, there you are. Switch off the rangechecking, and your (pascal)
> >program goes 45% faster.

> >And switching to single of double wouldn't hurt to much either.

> And if I remember correctly it will even run faster if you replace
> a := a /2 by a := a * 0.5.

   Yes, but the results(s) might not be the same...

Re:Why TP is slower than TC ?


Quote
bcwri...@ucsd.edu (Brian Wright) wrote:
>In article <32d15be6.343830@news>, M.Abdoll...@bham.ac.uk (Mehrzad Abdollahi) wrote:
>>Turbo Pascal version:
>>--------------------------
>>var
>>   i,j: integer;
>>   a:real;
>>begin
>> [Snip]
>>Turbo C++ version:
>>------------------------
>>#include <stdio.h>
>>int   i,j;
>>float  a;
>>int main()
>[Snip]

>Aha!  Real is not the equivalent of float.  If you change the eclaration > of variable "a" in the pascal version to "float", and re=

run it, you > > should see some better numbers.  

Make this "single" in turbo pascal or delphi.

Re:Why TP is slower than TC ?


Quote
M.Abdoll...@bham.ac.uk (Mehrzad Abdollahi) wrote:

.....

Quote
>I have not changed the switches in the Turbo Pascal compiler, and
>everything is as the default setting. As I remember in the default
>setting all the switches are on. I also did not changed the default
>setting of the Turbo C++ compiler. I guess the default switch setting
>in the Turbo C++ compiler is on. The programs are as follows:
>Turbo Pascal version:
>--------------------------
>var
>   i,j: integer;
>   a:real;

.....

Quote
>Turbo C++ version:
>------------------------
>#include <stdio.h>
>int   i,j;
>float  a;

.....

The main thing that jumps out at me is that you are treating the TP
"real" as equivalent to "float".  "Real" is a 6-byte type peculiar to
TP -- which also supports "single" and "double" (IEEE types more
comparable to "float") -- in fact, I believe it would be "single".
The TP "real" is maintained for backward compatibility with programs
written before the general availability of FPUs.  Your C program is
probably using the FPU, "real" never does.  Rewrite the TP program
substituting "single" for "real" and compare the times.

   --- Gary Wiltshire

Go to page: [1] [2]

Other Threads