Recent

Author Topic: [SOLVED] Program compiles, but never completes when executed  (Read 2343 times)

Schmitty2005

  • Jr. Member
  • **
  • Posts: 52
[SOLVED] Program compiles, but never completes when executed
« on: November 30, 2025, 04:03:18 pm »
I have the following code:
Code: Pascal  [Select][+][-]
  1. Program fpccpuload;
  2.  
  3. Uses Math;
  4.  
  5. Const
  6.   MAX = 22000;
  7.   //a value of 40000 does not complete after 10 minutes ! :(
  8.   // but a value of 22000 is done in under a second.
  9.  
  10. Var
  11.   x : Integer;
  12.   c : Float;
  13. Begin
  14.   x := 0;
  15.   While x < MAX Do
  16.     Begin
  17.       c := sin(cos(tanh(x)));
  18.       inc(x);
  19.     End;
  20. End.

As you can see in the comments, when MAX is at 22000, it works as expected.  at 40000, it does not complete after 10 minutes. 

I was learning ADA, and wanted a relative speed test for personal knowledge.  I thought this would be a good high CPU usage test to compare the speeds of GNAT ADA and FPC. 

This is the code in ADA , which completes 999999 in under 3 seconds:

Code: Ada  [Select][+][-]
  1. with ADA.Numerics.Elementary_Functions; use ADA.Numerics.Elementary_Functions;
  2. procedure Main is
  3.    x: Integer;
  4.    c : Float;
  5.    MAX :constant integer := 999999; --This is complete easily under 3 seconds !
  6. begin
  7.    x := 0;
  8.    while x < MAX loop
  9.       c := sin(cos(tanh(float(x))));
  10.       x := x + 1;
  11.       end loop;
  12. end Main;

I know that I am comparing apples to oranges. 

My question is :

 Why does the FPC (v3.2.2) version seem to lock up and never finish ?
« Last Edit: November 30, 2025, 04:14:07 pm by Schmitty2005 »

Nitorami

  • Hero Member
  • *****
  • Posts: 605
Guess you have set a Mode where integer is defined as 16 bit only.

Schmitty2005

  • Jr. Member
  • **
  • Posts: 52
Yes,  I just did
Code: Pascal  [Select][+][-]
  1. writeln(high(Integer))
and the output was 32767 !  I switched to OBJFPC and it works. 

Thank you!

Schmitty2005

  • Jr. Member
  • **
  • Posts: 52
Re: [SOLVED] Program compiles, but never completes when executed
« Reply #3 on: November 30, 2025, 04:19:38 pm »
If anybody is interested with 99999999 cycles :

ADA   EDIT: Float in ADA is 32-bit default
real   0m1.518s
user   0m1.515s
sys   0m0.003s


EDIT :
ADA with Long_Float :
real   0m3.331s
user   0m3.328s
sys   0m0.002s




FPC (with -O3)  :(  EDIT : Float in FPC defaults to double on x86_64
real   0m7.820s
user   0m7.817s
sys   0m0.002s

FPC with
Code: Pascal  [Select][+][-]
  1. c: single;//switched from double
  was in the same ballpark (7 + seconds);

« Last Edit: December 02, 2025, 12:28:27 am by Schmitty2005 »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12645
  • FPC developer.
Re: [SOLVED] Program compiles, but never completes when executed
« Reply #4 on: November 30, 2025, 04:59:15 pm »
Posting benchmarks without border condition sis pointless ? Same architecture, same precision, same configured instruction sets, IOW is it an apples to apples comparison or just a pointless ADA name drop?

Schmitty2005

  • Jr. Member
  • **
  • Posts: 52
Re: [SOLVED] Program compiles, but never completes when executed
« Reply #5 on: November 30, 2025, 06:52:19 pm »
Posting benchmarks without border condition sis pointless ? Same architecture, same precision, same configured instruction sets, IOW is it an apples to apples comparison or just a pointless ADA name drop?

It was me simply posting my findings in case anybody is interested.  If I wanted to name drop, I would have had ADA all over the title of the post.  This is just a hobby for me. I know nothing about making real benchmarks.  This comparison was enough for me to know what I wanted to know.  That is all there is too it. 

FPC and Lazarus are awesome open-source tools.  There is nothing else like them.

creaothceann

  • Sr. Member
  • ****
  • Posts: 262
Re: [SOLVED] Program compiles, but never completes when executed
« Reply #6 on: November 30, 2025, 08:28:42 pm »
For a more relevant benchmark you should definitely specify the target OS, CPU family, target processor and vector processing options. (Inlining and optimizing for size may also influence results.)

Example: C:\lazarus\fpc\3.2.4\bin\x86_64-win64\fpc.exe -Twin64 -Px86_64 -CpCOREAVX2 -MObjFPC -O3 -Sv

Lazarus has a project options window for that.

Nitorami

  • Hero Member
  • *****
  • Posts: 605
Re: [SOLVED] Program compiles, but never completes when executed
« Reply #7 on: December 01, 2025, 10:10:01 am »
Something's fishy about this benchmark.
With MAX=99999999, I get 6 seconds here with FPC3.4.2, approximately the same as the OP. To see what causes the load, I remove the inner cos(tanh(x)) so that only the sine remains, and then get 3 seconds. So the load is dominated by processing the trigonometric calls.

Looking at the assembler, the CPU is really only calling FSIN repeatedly; compiler optimisation settings don't matter, neither the FPU type. So I cannot see how any language could run this faster, except by implicit parallelisation which I don't believe ADA can do.

I suspect that the OP has run the benchmark with different MAX values; in the first post he writes that ADA runs in under 3 seconds with MAX=999999, but in the benchmark MAX=99999999 which ADA allegedly  does in 1.5 seconds...

dbannon

  • Hero Member
  • *****
  • Posts: 3678
    • tomboy-ng, a rewrite of the classic Tomboy
Re: [SOLVED] Program compiles, but never completes when executed
« Reply #8 on: December 01, 2025, 12:34:00 pm »
OK, with both set to 99999999 (that is eight nines) I get (fpc3.2.3 and Ada 12.2) -

dbannon@dell:~/Pascal/CLI/Ada$ time ./test

real   0m6.366s
user   0m6.359s
sys   0m0.001s
dbannon@dell:~/Pascal/CLI/Ada$ time ./main

real   0m1.066s
user   0m1.061s
sys   0m0.004s

And, believe me, no one can call me an apologist for Ada, I once refused a job (ie employment) I had really wanted when I found out it used ada.

I do not remember enough Ada to try and display what it calculates, be interesting to see if they give the same answer. Maybe a precision issue, maybe Ada just has a faster trig library ? ?

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

dbannon

  • Hero Member
  • *****
  • Posts: 3678
    • tomboy-ng, a rewrite of the classic Tomboy
Re: [SOLVED] Program compiles, but never completes when executed
« Reply #9 on: December 01, 2025, 12:46:44 pm »
OK, here is an explanation perhaps -

dbannon@dell:~/Pascal/CLI/Ada$ fpc -CF32 test.pas
....
dbannon@dell:~/Pascal/CLI/Ada$ time ./main

real   0m1.088s
user   0m1.084s
sys   0m0.004s

Lower precision FP. Much quicker

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

tetrastes

  • Hero Member
  • *****
  • Posts: 739
Re: [SOLVED] Program compiles, but never completes when executed
« Reply #10 on: December 01, 2025, 01:25:10 pm »
OK, here is an explanation perhaps -

dbannon@dell:~/Pascal/CLI/Ada$ fpc -CF32 test.pas
....
dbannon@dell:~/Pascal/CLI/Ada$ time ./main

real   0m1.088s
user   0m1.084s
sys   0m0.004s

Lower precision FP. Much quicker

Davo

Isn't main Ada program? I don't see difference with or without -CF32.

Nitorami

  • Hero Member
  • *****
  • Posts: 605
Re: [SOLVED] Program compiles, but never completes when executed
« Reply #11 on: December 01, 2025, 01:35:19 pm »
Quote
I don't see difference with or without -CF32.

Me neither... and making c single or double neither makes a difference to me.
 
Quote
maybe Ada just has a faster trig library ?
Sin/cos simply call the math coprocessors fsin / fcos. There is no library involved.

tetrastes

  • Hero Member
  • *****
  • Posts: 739
Re: [SOLVED] Program compiles, but never completes when executed
« Reply #12 on: December 01, 2025, 02:02:26 pm »
I think this is optimization question.
I don't want to install Ada to check this, but gcc already with -O1 removes all trigonometry from this code.
And fpc could also do it, but it only notes, that "Local variable "c" is assigned but never used".

Nitorami

  • Hero Member
  • *****
  • Posts: 605
Re: [SOLVED] Program compiles, but never completes when executed
« Reply #13 on: December 01, 2025, 02:13:39 pm »
That would sound plausible to me. The loop content is simply not executed because c is never used.

So maybe someone with an ADA compiler could print c at the end of the loop and see if that makes a difference ?
« Last Edit: December 01, 2025, 02:17:03 pm by Nitorami »

bytebites

  • Hero Member
  • *****
  • Posts: 777
Re: [SOLVED] Program compiles, but never completes when executed
« Reply #14 on: December 01, 2025, 03:02:00 pm »
Code: Ada  [Select][+][-]
  1. with ADA.Numerics.Elementary_Functions; use ADA.Numerics.Elementary_Functions;
  2. with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
  3. with Ada.Text_IO; use Ada.Text_IO;
  4. with Ada.Float_Text_IO;
  5. procedure Main is
  6.    x: Integer;
  7.    c, tot : Float;
  8.    MAX :constant integer := 999999; --This is complete easily under 3 seconds !
  9.  
  10. begin
  11.    x := 0;
  12.    tot:=0.0;
  13.    while x < MAX loop
  14.       c := sin(cos(tanh(float(x))));
  15.       tot:=tot+c;
  16.       x := x + 1;
  17.       end loop;
  18.    Ada.Float_Text_IO.put(item=>tot, exp=>0, aft=>2);
  19.    Ada.Text_IO.New_line;
  20.    put(Integer'image(x));
  21. end Main;

Code: Pascal  [Select][+][-]
  1. Program sincosi;
  2.  
  3. Uses Math;
  4.  
  5. Const
  6.   MAX = 999999;
  7.  
  8. Var
  9.   x : Integer;
  10.   c, tot : single;
  11. Begin
  12.   x := 0;
  13.   tot:=0;
  14.   While x < MAX Do
  15.     Begin
  16.       c := sin(cos(tanh(x)));
  17.       tot:=tot+c;
  18.       inc(x);
  19.     End;
  20.   writeln(tot);
  21.   writeln(x);
  22.  
  23. End.    
Ada result
Quote
507847.50
 999999

real   0m0,013s
user   0m0,012s
sys   0m0,002s

Fpc result
Quote
5.078475000E+05
999999

real   0m0,069s
user   0m0,069s
sys   0m0,000s
« Last Edit: December 01, 2025, 03:11:00 pm by bytebites »

 

TinyPortal © 2005-2018