niklas-heer / speed-comparison

A repo which compares the speed of different programming languages.
https://niklas-heer.github.io/speed-comparison
MIT License
475 stars 76 forks source link

pascal #7

Open cleberlr opened 4 years ago

cleberlr commented 4 years ago

Could I offer a Pascal version of this code? Using FreePascal 3.3 compiler is almost as fast than C version.


{$mode delphi} program leibniz;

uses Windows, Messages, SysUtils, Classes, DateUtils;

var file_contents: String; t0,t2: Int64; i,rounds: Integer; input_file: TextFile; x,pi: Double; Frequency: int64;

begin // Read the file into a string QueryPerformanceFrequency(Frequency); AssignFile(input_file,'rounds.txt'); Reset(input_file); Readln(input_file, file_contents); CloseFile(input_file);

rounds := StrToInt(file_contents); // convert into an Integer QueryPerformanceCounter(t0);

x := 1.0; pi := 1.0;

for i := 2 to rounds + 1 do begin x:= x -1; pi:= pi + (x / (2.0 i - 1.0)); end;

pi:= pi * 4;

QueryPerformanceCounter(t2); Writeln(Format('%.16f',[pi])); Writeln('time: '+Format('%.3f',[1000*(t2-t0)/Frequency])+' msec'); end.


If you want test a binary (that not use rounds.txt, only a fix value, 1000000), I uploaded an optimized binary in https://pixeldrain.com/u/jMFWXYIO I ran some tests using the fastest binary that I could compile for my machine (Ryzen 7 1700).. C -> 0.141ms C++ -> 0.141ms pascal -> 0.157ms python -> 378.7ms

niklas-heer commented 4 years ago

@cleberlr very interesting results thank you for sharing👍 If you open up a pull-request I will gladly expect it, but right now I'm not actively working on this project.