Recent

Author Topic: My new benchmark to test speed of some compilers  (Read 4076 times)

Edson

  • Hero Member
  • *****
  • Posts: 1325
My new benchmark to test speed of some compilers
« on: January 04, 2025, 06:54:46 pm »
Hi.

I just want to share a new benchmark I made to test the speed of the programs generated for some compilers using https://www.onlinegdb.com.

Code: Pascal  [Select][+][-]
  1. LANGUAGE        EXEC.TIME
  2. JavaScrip(Node) 0.17
  3. Object Pascal   0.19
  4. Go      0.19
  5. C       0.25
  6. Java    0.25
  7. C++ 23  0.28
  8. Rust    0.39
  9. PHP 7   1.50
  10. Python3 2.95
  11.  

Good for FPC that have the second position.

The details of the test are in my blog: https://blogdetito.com/2025/01/03/los-lenguajes-mas-rapidos-para-este-2025/
Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

440bx

  • Hero Member
  • *****
  • Posts: 5477
Re: My new benchmark to test speed of some compilers
« Reply #1 on: January 04, 2025, 07:18:16 pm »
Hello Edson (or should I say Tito ?),

Like you I was surprised by how well Javascript did and was looking for the test program source to see if I could see some reason why it beat compiled languages (the JIT time should have been a clear disadvantage.)

can you post the Javascript code ?

Thanks.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v4.0rc3) on Windows 7 SP1 64bit.

Edson

  • Hero Member
  • *****
  • Posts: 1325
Re: My new benchmark to test speed of some compilers
« Reply #2 on: January 04, 2025, 08:06:08 pm »
Hello Edson (or should I say Tito ?),

In fact. I'm Tito Edson  :D

Like you I was surprised by how well Javascript did and was looking for the test program source to see if I could see some reason why it beat compiled languages (the JIT time should have been a clear disadvantage.)

A JIT compiler (like Node.js or Java use), has the advantage that know exactly the machine where the program is going to run so it can do special optimization. Moreover the Node.js JIT compiler has been well designed and optimized because corporations have invested a lot in it.

can you post the Javascript code ?

The code for all languages are in the post of my article: https://blogdetito.com/wp-content/uploads/2025/01/Validar-primo.zip

Tell me if you have problems on download.
Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

440bx

  • Hero Member
  • *****
  • Posts: 5477
Re: My new benchmark to test speed of some compilers
« Reply #3 on: January 04, 2025, 09:32:24 pm »
Thank you Tito Edson :)
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v4.0rc3) on Windows 7 SP1 64bit.

440bx

  • Hero Member
  • *****
  • Posts: 5477
Re: My new benchmark to test speed of some compilers
« Reply #4 on: January 04, 2025, 11:23:26 pm »
Hello Edson,

I made a few very minor changes to the Pascal source to make it more "parallel" to the javascript source and re-tested.  Only the functions "EsPrimo" and "TareaLarga" have changes, most of the changes are in "EsPrimo".

Code: Pascal  [Select][+][-]
  1. program MedicionTiempo;
  2.  
  3. uses
  4.   SysUtils, Math;
  5.  
  6. { Función para verificar si un número es primo }
  7. function EsPrimo(num: LongInt): Boolean;
  8. var
  9.   i: LongInt;
  10. begin
  11.   if num <= 1 then exit(FALSE);  // mod
  12.     //EsPrimo := False           // mod
  13.   //else                         // mod
  14.   //begin                        // mod
  15.     //EsPrimo := True;           // mod
  16.     for i := 2 to Trunc(Sqrt(num)) do
  17.     begin
  18.       if num mod i = 0 then
  19.       begin
  20.         exit(FALSE);             // mod
  21.  
  22.         //EsPrimo := False;      // mod
  23.         //Break;                 // mod
  24.       end;
  25.     end;
  26.  
  27.     exit(TRUE);
  28.   //end;                         // mod
  29. end;
  30.  
  31. { Función que simula una tarea compleja de cálculo (encontrar primos en un rango grande) }
  32. procedure TareaLarga;
  33. var
  34.   i, count: LongInt;
  35. begin
  36.   count := 0;
  37.   for i := 2 to 1000000 - 1 do  { same number as in js and c }  // mod
  38.   begin
  39.     if EsPrimo(i) then
  40.       Inc(count);  { Contar los números primos encontrados }
  41.   end;
  42.   WriteLn('Cantidad de números primos encontrados: ', count);
  43. end;
  44.  
  45. var
  46.   StartTime, EndTime: TDateTime;
  47.   Tiempo: Double;
  48. begin
  49.   { Capturar el tiempo de inicio }
  50.   StartTime := Now;
  51.  
  52.   { Ejecutar la tarea larga }
  53.   TareaLarga;
  54.  
  55.   { Capturar el tiempo de finalización }
  56.   EndTime := Now;
  57.  
  58.   { Calcular el tiempo transcurrido en segundos }
  59.   Tiempo := (EndTime - StartTime) * 24 * 60 * 60;  { Convertir de días a segundos }
  60.  
  61.   { Mostrar el tiempo de ejecución }
  62.   WriteLn('El tiempo de ejecución de TareaLarga() es: ', Tiempo:0:6, ' segundos');
  63.   WriteLn('El tiempo de ejecución de TareaLarga() en milisegundos es: ', Tiempo * 1000:0:3, ' ms');
  64. end.
  65.  
with the code above and testing multiple times and discarding the first time, I obtain an execution time of 0.157000 secs which would put FPC in first place :)

Note that I did _not_ make any changes to optimize the algorithm, it's just more "parallel" to the code for other languages than it previously was.

ETA:

Highlighted single line change in TareaLarga.
« Last Edit: January 04, 2025, 11:34:48 pm by 440bx »
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v4.0rc3) on Windows 7 SP1 64bit.

Edson

  • Hero Member
  • *****
  • Posts: 1325
Re: My new benchmark to test speed of some compilers
« Reply #5 on: January 06, 2025, 04:38:50 pm »
Hello Edson,

I made a few very minor changes to the Pascal source to make it more "parallel" to the javascript source and re-tested.  Only the functions "EsPrimo" and "TareaLarga" have changes, most of the changes are in "EsPrimo".


Good observation.

However, it doesn't change the time when I compare in https://www.onlinegdb.com.

Maybe you can do a new test for all languages and get different times.
Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

jwdietrich

  • Hero Member
  • *****
  • Posts: 1259
    • formatio reticularis
Re: My new benchmark to test speed of some compilers
« Reply #6 on: January 06, 2025, 05:46:05 pm »
Interestingly, Object Pascal is faster than C++, a language known for its speed.

We have made a similar observation with code for simulating biological feedback loops and published it as a paper. It is available from https://doi.org/10.14201/adcaij.31762. Our results are highly debated but confirmed by your observations.
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 4.0.0 | FPC 3.2.2 | PPC, Intel, ARM | macOS, Windows, Linux

 

TinyPortal © 2005-2018