Recent

Author Topic: Free Pascal vs C++: The First Results Are In  (Read 26534 times)

syntonica

  • Full Member
  • ***
  • Posts: 120
Free Pascal vs C++: The First Results Are In
« on: December 30, 2019, 12:44:23 am »
I finally got my audio plugin limping along in Pascal to where I can do some preliminary tests. Once I got it that I was not debugging my program but rather my knowledge and understanding of Pascal, things started falling into place.  The code being tested is more  or less a direct translation.

Here are the results just from eyeballing the DAW's CPU meter while idling and then running a loop using a more demanding patch:

          C++    Opt. C++    Pascal    Opt. Pascal   
Idle    6%3%9%7%
Run    8%4.5%14%12%

As you can see, the code on C++ improves with optimization by 40-50%. Pascal improves by only 10-20% and is still slower than the unoptimized C++.

Here are the compiler switches that I've used for the test, which are basically on par with the clang settings, targeting 64-bit macOS 10.6+:

Code: Text  [Select][+][-]
  1.  fpc -O4 -OoAUTOINLINE -Xs -XX -Sv -Si -CfSSSE3 -CpCOREI

If I can improve the optimized Pascal to running at about 6%, then I would be satisfied with adopting it over C++ for cross-platform audio development.  The advantages I've found so far using just Geany+FPC over Mac+Xcode+Clang/LLDB + PC+CodeBlocks+GCC/GDB are huge! (64-bit Cocoa Lazarus just isn't there yet for me. :()

Short of hand-tuning assembly, are there any other tricks I can use to lure the compiler into better code? I have converted all the C++ consts to constrefs, made sure any records or arrays have been passed by reference, tried Move and Fill over plain for loops, -OoUnrollLoops, -OoFastMath, tried pointer math over arrays, etc. All the little things that used to work on older, less intelligent compilers.  I got about .25-.5% improvement with Move, which is used heavily, but FillQWord turned out to be probably slower than plain for loops. Nothing else seems to work. Using plain "fpc -O2" with nothing else gives me the exact same result!  :o

I'm not looking for miracles here, but my preliminary tests showed that the mathy bits were on par with C code, so there's probably other housekeeping stuff going on that could be minimized. 

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Free Pascal vs C++: The First Results Are In
« Reply #1 on: December 30, 2019, 12:53:06 am »
I think the problem is that your hear the wrong music.
If you have taken another genre pascal would have been number one ....

But serious: What do you want to say??? And don't you know how test results can be faked?

Winni
« Last Edit: December 30, 2019, 12:57:49 am by winni »

Laksen

  • Hero Member
  • *****
  • Posts: 802
    • J-Software
Re: Free Pascal vs C++: The First Results Are In
« Reply #2 on: December 30, 2019, 01:00:07 am »
What's your hotpath in the pascal code?

syntonica

  • Full Member
  • ***
  • Posts: 120
Re: Free Pascal vs C++: The First Results Are In
« Reply #3 on: December 30, 2019, 01:07:36 am »
I think the problem is that your hear the wrong music.
If you have taken another genre pascal would have been number one ....

But serious: What do you want to say??? And don't you know how test results can be faked?

Winni
lolz. It's not in anybody's best interest to fake the test results, especially when I wish to ditch the dreadful C++!

I'm looking for deep FPC knowledge that can help me reach my speed goals.  Hopefully, I've missed a compiler switch/option that can help shave the percentage points off.

syntonica

  • Full Member
  • ***
  • Posts: 120
Re: Free Pascal vs C++: The First Results Are In
« Reply #4 on: December 30, 2019, 01:11:01 am »
What's your hotpath in the pascal code?
I'm sorry, but I honestly don't understand the question. I have vague memories of setting a hotpath in Java once upon a time, but Java's always been fast enough for my use cases.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12030
  • Debugger - SynEdit - and more
    • wiki
Re: Free Pascal vs C++: The First Results Are In
« Reply #5 on: December 30, 2019, 01:16:01 am »
Well, what version of fpc?
As I understand it current trunk (3.3.1) has some improvements in the optimizer.

As far as 3.0.4 goes (and probably 3.2 too), loop optimization is not perfect (I do not know if (or to what extend) this has been addressed in 3.3.1):
So
Code: Pascal  [Select][+][-]
  1. for i := 0 to n do
  2.  x := x + foo[i]
may be better written as
Code: Pascal  [Select][+][-]
  1. pfoo := @foo[0];
  2. for i := 0 to n do begin
  3.   x := x + pfoo^;
  4.   inc(pfoo);
  5. end;
At least in older fpc that used to make a diff.
I believe gcc does optimize this for you. I am not sure fpc does (or which version of fpc...)


Also keep in mind, it is not only code. Do both use the exact same memory layout for data?
A tiny change in data layout, may lead to cache misses.



"hotpath" => if you profile your code, what part of your code is taking the most of the time?
« Last Edit: December 30, 2019, 01:19:09 am by Martin_fr »

Laksen

  • Hero Member
  • *****
  • Posts: 802
    • J-Software
Re: Free Pascal vs C++: The First Results Are In
« Reply #6 on: December 30, 2019, 01:16:35 am »
I mean what's the least performant part of the pascal code?

sash

  • Sr. Member
  • ****
  • Posts: 366
Re: Free Pascal vs C++: The First Results Are In
« Reply #7 on: December 30, 2019, 01:22:42 am »
Hmm... funny.
At the moment I'm working on crossplatform audio application (smth. like DAW/Sequencer) where audio part is written in C++ (mostly because of couple dependencies) and GUI part I gave to Lazarus. Both live happy together.

Why did you ditch C++? Pascal cannot compete with C in "bare performance" (for a couple of reasons).
But the strong parts of Lazarus are easy crossplatform GUIs, databases.
I think there should be a right tool for every task.
Lazarus 2.0.10 FPC 3.2.0 x86_64-linux-gtk2 @ Ubuntu 20.04 XFCE

syntonica

  • Full Member
  • ***
  • Posts: 120
Re: Free Pascal vs C++: The First Results Are In
« Reply #8 on: December 30, 2019, 01:29:20 am »
Oh, okay... I know where the code spends all its time from profiling in XCode, although it didn't tell me anything I already didn't know.  I did verify my ugly Case statement of 75 entries enumerated over 0..127 is being turned into a jump table, and it is.  I suspect that this the source of most of my 2% gain.

I'm using 3.0.4. I can try 3.3.1. Do I have to pull it through SVN and compile myself?

I tried the whole pointers vs arrays thing (it's huge in audio work) and got no improvement.  In the version of clang I'm using, it offers no improvement either.  I suspect the compiler is already turning those sequential array accesses into pointers with incs.

syntonica

  • Full Member
  • ***
  • Posts: 120
Re: Free Pascal vs C++: The First Results Are In
« Reply #9 on: December 30, 2019, 01:33:04 am »
Hmm... funny.
At the moment I'm working on crossplatform audio application (smth. like DAW/Sequencer) where audio part is written in C++ (mostly because of couple dependencies) and GUI part I gave to Lazarus. Both live happy together.

Why did you ditch C++? Pascal cannot compete with C in "bare performance" (for a couple of reasons).
But the strong parts of Lazarus are easy crossplatform GUIs, databases.
I think there should be a right tool for every task.
I handle GUI drawing myself to avoid any platform-dependency. I even use my own stripped down font.  Using Pascal would eliminate another huge chunk of platform-specific code (Objective-C++ anyone? :) )


I forgot to mention: I did some basic testing regarding my use case with FP vs C and FP was on par.  I tested a couple of tight, mathy audio loops to calculate values and stuff them into arrays.
« Last Edit: December 30, 2019, 01:35:11 am by syntonica »

PaulRowntree

  • Full Member
  • ***
  • Posts: 132
    • Paul Rowntree
Re: Free Pascal vs C++: The First Results Are In
« Reply #10 on: December 30, 2019, 01:50:24 am »
Are you being pushed into this investigation because the hardware isn't coping with the demands?  Is the audio quality suffering?
I recall the CP/M days where memory limits were a key constraint; with multi-GB RAM we don't even think about such issues.  I wonder at what point the multi-core multi-thread multi GHz processors will make speed constraints less important than programming quality and the programming effort needed to get it.  For my apps, and my age, we are already there.   
Paul Rowntree
- coding for instrument control, data acquisition & analysis, CNC systems

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Free Pascal vs C++: The First Results Are In
« Reply #11 on: December 30, 2019, 01:54:49 am »
@PaulRowntree

Bravo!

syntonica

  • Full Member
  • ***
  • Posts: 120
Re: Free Pascal vs C++: The First Results Are In
« Reply #12 on: December 30, 2019, 02:12:41 am »
Are you being pushed into this investigation because the hardware isn't coping with the demands?  Is the audio quality suffering?
I recall the CP/M days where memory limits were a key constraint; with multi-GB RAM we don't even think about such issues.  I wonder at what point the multi-core multi-thread multi GHz processors will make speed constraints less important than programming quality and the programming effort needed to get it.  For my apps, and my age, we are already there.
If I was making a cross-platform notebook TrapperKeeper, I'd be happy as a clam with Pascal. But yes, in certain domains, CPU power and RAM size are still concerns.  In the realm of audio/video processing, 16-core, 4Ghz CPUs are still not enough! Sometimes, 32GB RAM is not enough. Back in the CPM days, I always worried that if I didn't pedal hard enough, my 8" floppy wouldn't work...  :P

At 12% usage, on my machine, I can run about 6 instances of my plugin. At 4.5%, I can run about 16.

syntonica

  • Full Member
  • ***
  • Posts: 120
Re: Free Pascal vs C++: The First Results Are In
« Reply #13 on: December 30, 2019, 02:18:15 am »
@PaulRowntree

Bravo!
There's always one! Er, two...  :P

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Free Pascal vs C++: The First Results Are In
« Reply #14 on: December 30, 2019, 03:02:08 am »
Hi!

I don't know what your audio plugin is doing but I have a home grown audioplayer (work in progress) that uses BASS: 4.6% .. 5.9% CPU while playing .

Even the bad coded and full of useless features player Amarok needs 12% .. 14% while playing

Values measured by top

Ryzen 4 cores

Winni


 

TinyPortal © 2005-2018