Missing the good old days (Pascal)

I saw a news article last week announcing the death of Niklaus Wirth, which I found both sad and disconcerting. For anyone studying Computer Science back in the 80’s and 90’s, you were probably exposed to at least one of his programming languages. A few in my case; Pascal,Modula-2 and variants such as “Turbo Pascal” and “Borland Delphi”.

Although I’ve “become” a Python programmer, all of a sudden I find myself missing Pascal and wonder why I didn’t continue to use Pascal on Linux.

I think the answer is that at the time, there wasn’t really a solid implementation for Linux that made sense for production code. For a time I tried a product called “Kylix” from Borland, which was essentially a port of “Delphi” to Linux, however is was very expensive and they quickly discontinued the project.

However …

More recently a project called “free pascal” has appeared. I say more recently, it’s been around for a long time now, indeed you can get it from the standard Ubuntu repository with;

apt install fpc

Looking back at Pascal, I remember (in principle) a fair amount, and looking at the documentation it all seems relevant today. A simple program looks something like;

{ demo.pas }
program demo;
begin
    writeln('Hello world');
end.

And to compile and run;

$ fpc demo
Free Pascal Compiler version 3.2.2+dfsg-9ubuntu1 for x86_64
Copyright (c) 1993-2021 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling demo.pas
Linking demo
4 lines compiled, 0.1 sec
$ ./demo
Hello world

All of a sudden programming feels real again, none of this interpreted rubbish, a real compiler! :slight_smile:

Anyway, turns out there’s also a User Interface to accompany the compiler. Now, if you remember the likes of “Borland Delphi” this may look strangely familiar, however this is the result of;

apt install lazarus

Rather than taking out a second mortgage.

Anyway, for anyone with a set of Delphi manuals on their shelf (he says putting his hand up) , Lazarus is an alternate way to write native GUI based applications for Linux, maybe without the potential mess associated with Qt, Gtk, C, Python etc and their graphical tookits and frameworks.

Any Pascal programmers out there?

So, after a little idling between builds, I wondered how something a little more complex would compare to Python in terms of code and performance. Now Python has a reputation for doing some fairly concise stuff with relative ease, for example a web server on one line.

So, how does the Python one-line web server compare to something similar in Free Pascal?

Python

python3 -m http.server
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...

$ curl http://localhost:8000/test.txt
{"text": "Hello World"}

$ ab -n1000 -c6 http://localhost:8000/test.txt
Concurrency Level:      6
Complete requests:      10000
Requests per second:    1358.39 [#/sec] (mean)

So … one line, nearly 1358 requests per second.

Pascal

Ok, so this isn’t a like for like comparison, but it should be relatively representative. Bear in mind I’ve cobbled this up from examples so it may not be the most efficient solution. (and it’s all running inside a container)

program DemoApi;
{$mode objfpc}{$H+}
uses
  cthreads, cmem, SysUtils, fphttpapp, httpdefs, httproute, fpjson, jsonparser;
procedure jsonResponse(var response: TResponse; data: String);
begin
  response.Content := data;
  response.Code := 200;
  response.ContentType := 'application/json';
  response.ContentLength := length(data);
  response.SendContent;
end;
procedure helloEndpoint(request: TRequest; response: TResponse);
var
  jObject : TJSONObject;
begin
  jObject := TJSONObject.Create;
  try
    jObject.Strings['text'] := 'Hello World!';
    jsonResponse(response, jObject.AsJSON);
  finally
    jObject.Free;
  end;
end;
begin
  Application.Port := 8000;
  HTTPRouter.RegisterRoute('/hello', @helloEndpoint);
  Application.Threaded := true;
  Application.Initialize;
  Application.Run;
end.
$ fpc -O3 DemoApi
Free Pascal Compiler version 3.2.2+dfsg-9ubuntu1 for x86_64
Copyright (c) 1993-2021 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling DemoApi.pas
Linking DemoApi
30 lines compiled, 0.4 sec
$ ./DemoApi 

$ curl http://localhost:8000/hello
{ "text" : "Hello World!" }

ab -n10000 -c6 http://localhost:8000/hello
Concurrency Level:      6
Complete requests:      10000
Requests per second:    11595.52 [#/sec] (mean)

So on the face of it maybe something like 10x faster when it comes to delivering a simple response on a web based endpoint. Maybe a little misleading so just to clarify;

  • In this context, Python can’t (yet) make use of threads in the same way that Pascal can, if you reduce the number of threads from 6 to 1, the performance boost drops to x3.
  • This example will spend a lot of it’s time in system (networking) calls will will be common to each implementation, so it’s not really testing the language speed.

This was more about seeing how much work it would be to code up a web server in Pascal vs Python’s one-liner. The answer is, it’s pretty comparable. Pascal has some very similar libraries that do pretty much the same thing.

It’s quite ironic; looking at the code in the editor, it seems “nicer” than Python.
Hmm.