Recent

Author Topic: Compare code optimization of C and FreePascal !  (Read 17043 times)

mohsenti

  • Jr. Member
  • **
  • Posts: 56
Compare code optimization of C and FreePascal !
« on: May 16, 2016, 10:56:30 am »
Hi,

I write some codes to find primes number between 2 and 1999 with c and free-pascal and compile them; execution time of codes are same but when compile C with -Ofast and Pascal with all optimization options  C is too faster than free-pascal.
Why ?

you can see results in this git.

BeniBela

  • Hero Member
  • *****
  • Posts: 755
    • homepage
Re: Compare code optimization of C and FreePascal !
« Reply #1 on: May 16, 2016, 11:09:09 am »
The C compilers have become incredible clever.

So you tested the source in the git?

In that case, the compiler is way too clever

Code: [Select]

int main() {
   int count=0;
   for(int i=2;i<2000;i++)
      if (isPrime(i)){a
         count++;
      }
   return 0;
}

You never read the value assigned to count, so it does not matter and the compiler starts optimizing it to:


Code: [Select]

int main() {
   for(int i=2;i<2000;i++)      isPrime(i);
   return 0;
}


But now the value of isPrime is not used, so it keeps optimizing:

Code: [Select]

int main() {
   for(int i=2;i<2000;i++)  ;
   return 0;
}

Now the loop is pointless, and the entire program is reduced to:

Code: [Select]

int main() {
   return 0;
}

And that is a very, very fast program

mohsenti

  • Jr. Member
  • **
  • Posts: 56
Re: Compare code optimization of C and FreePascal !
« Reply #2 on: May 16, 2016, 11:13:04 am »
yes I know.
But why freepascal compiler doesn't optimize code to
Code: Pascal  [Select][+][-]
  1. begin
  2. end.
  3.  

?

Awkward

  • Jr. Member
  • **
  • Posts: 66
Re: Compare code optimization of C and FreePascal !
« Reply #3 on: May 16, 2016, 11:16:57 am »
Try to remove "uses SysUtils" first

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8734
  • FPC developer.
Re: Compare code optimization of C and FreePascal !
« Reply #4 on: May 16, 2016, 11:17:16 am »
I write some codes to find primes number between 2 and 1999 with c and free-pascal and compile them; execution time of codes are same but when compile C with -Ofast and Pascal with all optimization options  C is too faster than free-pascal.
Why ?

What about big companies paying for fulltime programmers working on gcc, while FPC is done by people in their spare time?

Also, FPC's focus is more applications than system (though it is usable for both).

The big problem with comparisons like this is that you are not only comparing languages but also compilers.   You could try to get some perspective by using some minor C compilers with a similar background like FPC.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8734
  • FPC developer.
Re: Compare code optimization of C and FreePascal !
« Reply #5 on: May 16, 2016, 11:18:53 am »
yes I know.
But why freepascal compiler doesn't optimize code to
Code: Pascal  [Select][+][-]
  1. begin
  2. end.
  3.  

?

Somebody has to do it and maintain it, while the program is pointless for any purpose except implementing *nix /usr/bin/true and /usr/bin/false.

rtusrghsdfhsfdhsdfhsfdhs

  • Full Member
  • ***
  • Posts: 162
Re: Compare code optimization of C and FreePascal !
« Reply #6 on: May 16, 2016, 11:35:14 am »
IMHO it would be better to improve interop with GCC. So we can call Classes, Templates etc.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8734
  • FPC developer.
Re: Compare code optimization of C and FreePascal !
« Reply #7 on: May 16, 2016, 11:37:05 am »
IMHO it would be better to improve interop with GCC. So we can call Classes, Templates etc.

You can't without pulling GCC into FPC. And then again for every new major version.

rtusrghsdfhsfdhsdfhsfdhs

  • Full Member
  • ***
  • Posts: 162
Re: Compare code optimization of C and FreePascal !
« Reply #8 on: May 16, 2016, 11:40:40 am »
FPC already uses some parts from GCC like linker I think. So might as well do the rest.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8267
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Compare code optimization of C and FreePascal !
« Reply #9 on: May 16, 2016, 12:24:41 pm »
FPC already uses some parts from GCC like linker I think. So might as well do the rest.
I wouldn't call binutils a part of GCC...

rtusrghsdfhsfdhsdfhsfdhs

  • Full Member
  • ***
  • Posts: 162
Re: Compare code optimization of C and FreePascal !
« Reply #10 on: May 16, 2016, 12:31:10 pm »
If you want C performance and VCL library use C++ Builder.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8734
  • FPC developer.
Re: Compare code optimization of C and FreePascal !
« Reply #11 on: May 16, 2016, 01:06:15 pm »
FPC already uses some parts from GCC like linker I think.

Over the years less and less. A trend that hopefully continues. 

And as Leledumbo correctly states, binutils is not part of gcc, and is not related to the issue that C/C++ headers are notoriously hard to interface.

Thaddy

  • Hero Member
  • *****
  • Posts: 10488
Re: Compare code optimization of C and FreePascal !
« Reply #12 on: May 16, 2016, 01:21:46 pm »
If you want C performance and VCL library use C++ Builder.

That's bad advice. You mean any C/C++ compiler other than C++Builder when performance is an issue. MS Visual studio C /C++  (free), GNU compiler suite C/C++ parts (free), you name them...
« Last Edit: May 16, 2016, 01:24:02 pm by Thaddy »
When you ask a question that is actually answered in the documentation, you are either lazy or a moron.

Thaddy

  • Hero Member
  • *****
  • Posts: 10488
Re: Compare code optimization of C and FreePascal !
« Reply #13 on: May 16, 2016, 01:28:45 pm »

IMHO it would be better to improve interop with GCC. So we can call Classes, Templates etc.
Templates are macro's in C++. So you can't ever call them. Unless you take the intermediate step of calling a c++ preprocessor. Which would kill performance of the compiler. Again, you have some silly idea's about how some things work. I suggest you take up my earlier advice and study a bit more. Frankly, you are silly. >:D 8-) O:-)
When you ask a question that is actually answered in the documentation, you are either lazy or a moron.

mohsenti

  • Jr. Member
  • **
  • Posts: 56
Re: Compare code optimization of C and FreePascal !
« Reply #14 on: May 16, 2016, 01:39:15 pm »
Base on your posts I think we have 2 solution to speed up applications.
1. Add some parts to fpc to compile code to c insted of assembly to use power of gcc compiler.
2.Write speed limit part of application with C and build them as static library and link them with other parts

We can use these solution until fpc optimization become power up.

 

TinyPortal © 2005-2018