Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pascal #7

Open
cleberlr opened this issue Nov 9, 2019 · 1 comment
Open

pascal #7

cleberlr opened this issue Nov 9, 2019 · 1 comment
Labels
add-language Suggests adding a language.

Comments

@cleberlr
Copy link

cleberlr commented Nov 9, 2019

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
Copy link
Owner

niklas-heer commented Dec 9, 2019

@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.

@niklas-heer niklas-heer added the add-language Suggests adding a language. label Oct 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
add-language Suggests adding a language.
Projects
Status: Backlog
Development

No branches or pull requests

2 participants