would multi threading help this project? lazarus / 21-General ---------------------------------------------------------------------- speter · 2026-01-21 07:56 I have a program which analyses sudoku puzzles - in job lots. At present my "creator" takes about 20minutes to create a file with ~950k puzzles. I then sort the output (by difficullty) and run a program that removes duplicate puzzles. Finally, I though the ~650k puzzles at my analyser, which takes about an hour and uses about 15% of the computers cpu (according the windows task manager). My question is: if I were to rewrite the analyser to use threads, would that help? And I guess a second question, would it require a total rewrite of the application? cheers S. ---------------------------------------------------------------------- PierceNg · 2026-01-21 08:08 How about some way of recognizing patterns in the puzzles, and have your program cache those patterns for reuse? Might also speed the program up even when run single threaded. ---------------------------------------------------------------------- H₂SO₄ · 2026-01-21 08:14 Quote from: speter on January 21, 2026, 08:56:13 am[...] uses about 15% of the computers cpu [..] To answer your original question: Yes, as long as your CPU usage is below 100% you aren't trying hard enough  Does your CPU happen to have 6 cores by any chance? ---------------------------------------------------------------------- speter · 2026-01-21 08:42 Quote from: Khrys on January 21, 2026, 09:14:35 amDoes your CPU happen to have 6 cores by any chance?According the task manager it has 24 cores. cheers S. ---------------------------------------------------------------------- MathMan · 2026-01-21 14:10 The answer to your second question is - it depends  If your current analyser has a single entry where you hand over a sudoku and only uses local (or very low number of global) variables, chances are good that you can make that multi-threaded without much effort. It would help though, if you provide some source. Cheers, MathMan ---------------------------------------------------------------------- speter · 2026-01-21 21:53 Quote from: MathMan on January 21, 2026, 03:10:30 pmThe answer to your second question is - it depends  If your current analyser has a single entry where you hand over a sudoku and only uses local (or very low number of global) variables, chances are good that you can make that multi-threaded without much effort. It would help though, if you provide some source. Cheers, MathMan Thanks everyone for your help. The TPuzzle.solve method includes the following: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   repeat  ok := NakedSingles or          HiddenSingles or          LockedCandidates or          NakedPairs or          HiddenPairs or        XWing or          NakedTriples or          NakedQuads or        HiddenTriples or          HiddenQuads or          // ... and another 24 patternsSo *I think* threading might be sensible / possible. Though I might need to reorganise things since the functions above are, at present, in various units (and not part of the TPuzzle class). cheers S. ---------------------------------------------------------------------- speter · 2026-01-22 03:27 I have created a very simple multithreaded app (attached), which works ok; but I'd like to get some help / advice on whether this approach will work with a more complex example. Bascially, each thread adds 2 numbers from an array (declared in the form object), the result is assigned into the array. All of which works, but is it safe!? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {  https:// forum.lazarus.freepascal.org/index.php?topic=44274.msg311652#msg311652 }unit umain; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; const  num=10;type  Trec = record a,b,c : integer; end;  Tarray = array[0..num-1] of Trec;   { TMyThread }   TMyThread = class(TThread)    index : integer;  protected    procedure Execute; override;  end;   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    Button2: TButton;    Memo1: TMemo;    procedure Button1Click(Sender: TObject);    procedure Button2Click(Sender: TObject);    procedure FormCreate(Sender: TObject);  private   public    myarray : Tarray;  end; var  Form1: TForm1;  implementation {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject);begin  randomize;end; procedure TForm1.Button1Click(Sender: TObject);var  a : integer;  MyThreads : array [0..num-1] of TMyThread;begin  for a := 0 to num-1 do    begin    myarray[a].a := random(10);      myarray[a].b := random(10);      myarray[a].c := 0;       MyThreads[a]  := TMyThread.create(true);    MyThreads[a].index := a;      MyThreads[a].FreeOnTerminate := true;    MyThreads[a].Start;    end;   memo1.append('all done');end; procedure TForm1.Button2Click(Sender: TObject);var  a: Integer;begin  for a := 0 to num-1 do    with myarray[a] do      memo1.append(format('a=%d; b=%d; c=%d',[a,b,c]));end; { TMyThread } procedure TMyThread.Execute;begin  with form1.myarray[index] do    c := a + b;end; end. ---------------------------------------------------------------------- 440bx · 2026-01-22 04:20 What you've shown in the previous post is safe but will very likely take noticeably longer (relatively speaking) than a single thread performing the entire task. The reason for this is simple, you're creating a thread for every element in the array.  Thread creation isn't a cheap operation.  Certainly _much_ more expensive than adding two integers together which is what a single threaded program would do. For what you're trying to do yield a noticeable improvement, the first thing you need is to calculate what the optimal number of threads is.   That number depends on two things: 1. the global task that is to be performed and 2. the number of processors in the system. In the example you presented, no improvement can be obtained because there are too few elements in the array and the operation is very simple.  The point is, you need to create something that is relatively speaking simple but that still represents the original problem.  This is necessary in order to be able to _measure_ the improvement, if any, a particular approach yields. In the case of adding a couple of integers, in today's processors it probably takes a number of elements in the hundreds of thousands in order to be able to have a repeatable and measurable difference.  Also, for your example, you're using random numbers which makes it difficult to verify the result is correct, considering the simplicity of the task it probably is but, it would be better to use a pattern that yields a value that can be pre-determined, that way _you_ can verify the results. As a rule of thumb, a well behaved application should create, at most, 2/3 the number of threads as there are processors in the system.  This because, in general, you want leave some processor power available to run other tasks to ensure the system keeps running smoothly.  Generally speaking, give the user the option of selecting how many processors the task should use and "recommend" no more than 2/3 for the reason mentioned but, if the user want maximum speed at the expense of the system still running smoothly, I'd allow a maximum of (number of processors - 1) (always leave one processor available to run the O/S and other apps.) Lastly, the threads should _not_ terminate when they are done with their task, they should suspend instead and be activated by inserting a task in their queue (which they check) before _resuming_ them.  That way you have a pool of threads that perform work as it is needed.  How this is controlled also depends on global task that is to be performed. The above means that, one of the preliminary/setup steps the program carries out is to create a thread pool upfront, which is later used to accomplish the main task. HTH. ---------------------------------------------------------------------- speter · 2026-01-22 04:29 Thanks for the reply 440bx. The application that I want to make multithreaded took about 2 hours to run yesterday - reviewing about 650k sudoku puzzles. I was thinking of giving each thread 100k puzzles (so 7 threads); though I guess I could make it more even by using 10 threads each having 65k puzzles. My concern with the simple program I posted was that, when I read up on threads this morning, I got the impression that accessing "global" variables was a bad idea... cheers S. ---------------------------------------------------------------------- 440bx · 2026-01-22 05:56 Quote from: speter on January 22, 2026, 05:29:22 amI got the impression that accessing "global" variables was a bad idea... Generally speaking, global variables are "not desirable", that said, there are cases where they may be the only way to share information. The solution you mention of splitting the workload evenly (or close to evenly) among threads, isn't necessarily optimal but, it is simple and reasonable and should cut the total time it takes to create the puzzles. ---------------------------------------------------------------------- H₂SO₄ · 2026-01-22 06:02 Apart from multithreading - have you tried profiling your program? After all, spreading (potentially) slow code onto more cores only increases the total CPU time wasted ---------------------------------------------------------------------- speter · 2026-01-22 11:18 Thanks for the replies. Quote from: Khrys on January 22, 2026, 07:02:19 am... have you tried profiling your program? After all, spreading (potentially) slow code onto more cores only increases the total CPU time wasted  No, I haven't (nor do I know how I might do it). At present I am timing the solving of each puzzle, though. It seems to me, solving sudoku puzzles is inherantly slow and inefficient. My program searches for a range of patterns which could help solve a part of the puzzle. What I am seeking to do is to work out how the puzzle can be solved (the actual solution is irrelevant). Added to the above I haven't implemented 3 or so of the most complex patterns / techniques, which means my app can't solve the most complex puzzles (at the moment). The most difficult puzzle solved is rated 8.3 (and 11 patterns are used in the solving. The current data set has 653,608 puzzles, 652,273 are solved (1,335 not solved) with a difficulty rating of 1.2 to 9.0 (the most difficulty puzzles rate 10+), the average time taken to solve a puzzle is 0.002 seconds (the max is 0.359). The majority (70%) of the puzzles are trivial to solve. cheers S. ---------------------------------------------------------------------- MathMan · 2026-01-22 11:38 Quote from: 440bx on January 22, 2026, 05:20:22 am The reason for this is simple, you're creating a thread for every element in the array.  Thread creation isn't a cheap operation.  Certainly _much_ more expensive than adding two integers together which is what a single threaded program would do. Just to give speter a ballpark figure here - on modern CPU cores, modern OS, the creation and start of a thread is in the range of multiple 10,000 CPU cycles. One can do substantial work in that time indeed ... ---------------------------------------------------------------------- creaothceann · 2026-01-22 14:07 Quote from: speter on January 22, 2026, 12:18:12 pmQuote from: Khrys on January 22, 2026, 07:02:19 am... have you tried profiling your program?No, I haven't (nor do I know how I might do it) You can use the SysUtils.Now function before and after doing some work, if the difference isn't too small. For smaller/more precise durations you can get the time from the CPU's built-in timer, in Windows via QueryPerformanceCounter/-Frequency: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Timer;  {$ModeSwitch AdvancedRecords} interface type Timer = record        private        class var _LatestStart    : Int64;        class var _TicksPerSecond : Int64;        public        class property  TicksPerSecond : Int64  read _TicksPerSecond;        class function  GetCounter     : Int64;   inline;  // in ticks        class procedure Start;                    inline;        class function  Stop         : Double;  inline;  // in seconds        end; implementation {$ifdef Windows}function QueryPerformanceCounter  (out lpPerformanceCount : Int64) : LongBool;  external 'kernel32' name 'QueryPerformanceCounter';function QueryPerformanceFrequency(out lpFrequency        : Int64) : LongBool;  external 'kernel32' name 'QueryPerformanceFrequency';{$endif} class function Timer.GetCounter : Int64;  inline;begin        {$ifdef Windows}  QueryPerformanceCounter(Result);        {$else}           {$fatal}  // todo        {$endif}end; class procedure Timer.Start;          inline;  begin  _LatestStart := GetCounter;                                  end;class function  Timer.Stop : Double;  inline;  begin  Result       := (GetCounter - _LatestStart) / TicksPerSecond;  end; initialization        Timer._TicksPerSecond := Timer.GetCounter; end. Quote from: MathMan on January 22, 2026, 12:38:36 pmJust to give speter a ballpark figure here - on modern CPU cores, modern OS, the creation and start of a thread is in the range of multiple 10,000 CPU cycles. One can do substantial work in that time indeed ... obligatory "Operation Costs in CPU Clock Cycles" ---------------------------------------------------------------------- LeP · 2026-01-22 14:14 Quote from: MathMan on January 22, 2026, 12:38:36 pmJust to give speter a ballpark figure here - on modern CPU cores, modern OS, the creation and start of a thread is in the range of multiple 10,000 CPU cycles. One can do substantial work in that time indeed ... I don't think we should think this way. Initializing something always has its burden, obviously, but it's also necessary to consider the benefits it can bring... 20 minutes of processing time can easily be shaved off using threads, and initialization would have absolutely no impact on the process. If we assume that only 10 threads can process 10 puzzles in parallel, each lasting 359 ms (the time claimed by the author), we would have a load of 370 ms (considering that each thread takes 1 ms to initialize and schedule... a HUGE and improbable amount of time) versus 3590 ms, or just over 1/9 of the time. If we increase the threads to 16 (as is likely), it's easy to see that the 20 minutes would drop to a few minutes (perhaps even less). The author should have a processor with 8 power cores (16 threads) and 16 ec cores. Using the 16 power core threads alone would significantly reduce processing time. And I doubt the processor would saturate to 100% (it could be around 65%). ---------------------------------------------------------------------- LeP · 2026-01-22 14:21 Quote from: creaothceann on January 22, 2026, 03:07:04 pmYou can use the SysUtils.Now function before and after doing some work, if the difference isn't too small. For smaller/more precise durations you can get the time from the CPU's built-in timer, in Windows via QueryPerformanceCounter/-Frequency: You can use this (diagnostic.pas) that have TStopWatch implementation: https://forum.lazarus.freepascal.org/index.php/topic,71540.msg558558.h tml#msg558558 ---------------------------------------------------------------------- MathMan · 2026-01-22 14:43 Quote from: creaothceann on January 22, 2026, 03:07:04 pm obligatory "Operation Costs in CPU Clock Cycles" Oh, nice one! Thanks for sharing. ---------------------------------------------------------------------- MathMan · 2026-01-22 14:49 Quote from: LeP on January 22, 2026, 03:14:21 pmQuote from: MathMan on January 22, 2026, 12:38:36 pmJust to give speter a ballpark figure here - on modern CPU cores, modern OS, the creation and start of a thread is in the range of multiple 10,000 CPU cycles. One can do substantial work in that time indeed ... I don't think we should think this way. Initializing something always has its burden, obviously, but it's also necessary to consider the benefits it can bring... 20 minutes of processing time can easily be shaved off using threads, and initialization would have absolutely no impact on the process. If we assume that only 10 threads can process 10 puzzles in parallel, each lasting 359 ms (the time claimed by the author), we would have a load of 370 ms (considering that each thread takes 1 ms to initialize and schedule... a HUGE and improbable amount of time) versus 3590 ms, or just over 1/9 of the time. If we increase the threads to 16 (as is likely), it's easy to see that the 20 minutes would drop to a few minutes (perhaps even less). The author should have a processor with 8 power cores (16 threads) and 16 ec cores. Using the 16 power core threads alone would significantly reduce processing time. And I doubt the processor would saturate to 100% (it could be around 65%). I agree with the above. However you seem to have misread my statement. It was given in light of 440bx comment where I just wanted to give a watermark below which one might not gain from multi-threading without further careful analysis. ---------------------------------------------------------------------- BeniBela · 2026-01-22 15:59 If you're creating files anyway, you can use multi processes. Simply start the program ten times, create ten files (of 95k puzzles each), and then merge them. ---------------------------------------------------------------------- speter · 2026-01-23 01:14 My thanks to everyone, but particularly Lep and BeniBela. Food for thought. cheers S. PS: at present I am using GetTickCount64() for timing. ---------------------------------------------------------------------- LeP · 2026-01-23 08:48 Quote from: speter on January 23, 2026, 02:14:14 amMy thanks to everyone, but particularly Lep and BeniBela. Food for thought. cheers I'm happy to help. Quote from: speter on January 23, 2026, 02:14:14 amPS: at present I am using GetTickCount64() for timing. The resolution is in ms. (milliseconds) and depend from system. Typically it has the granularity between 10 to 16 milliseconds. This granularity cannot be changed even with the dedicated system APIs (like multimedia). If you use TStopWatch you can have a view on timing more accurate. Bye ---------------------------------------------------------------------- speter · 2026-01-23 10:34 Quote from: LeP on January 23, 2026, 09:48:31 amIf you use TStopWatch you can have a view on timing more accurate.Thanks, I had thought GetTick was more accurate. cheers S. ---------------------------------------------------------------------- Thaddy · 2026-01-23 11:14 Yes, that is misleading: GetTickCount64 does not count "ticks" QueryPerformanceCounter on Windows does...As does the unix kernel which has depending on CPU nanosecond resolution or better. Timing is more accurate on Linux, but a bit over the top for everyday use. Also note that in every case long running timers loose their precision. These high resolution timers are meant for short timings, unless you run a RTOS. ---------------------------------------------------------------------- 440bx · 2026-01-23 12:48 Just for the record, for reasonably accurate measurements that would provide a reasonable idea of how well a particular algorithm performs, neither GetTickCount nor querying the performance counter (rdtsc and/or rdtscp) will be truly accurate (however, they'll provide a good ballpark figure.) A more accurate measurement can be obtained by querying the number of clock cycles a thread has used from one instant in time to another.  That way, unlike rdtsc/rdtscp, the time when the thread was not executing is _not_ included in the counter thereby yielding a more accurate measurement. Unfortunately, IIRC, the API(s) that return the number of clock cycles used by the process and each thread are not documented (they are in ntdll) but, they can be seen in action in utilities such as Process Explorer (go to the threads tab) and Process Hacker/System Informer. Anyway, in the majority of cases, rdtsc/rdtscp can provide a reasonably good idea of relative performance. ---------------------------------------------------------------------- LeP · 2026-01-23 14:15 Quote from: 440bx on January 23, 2026, 01:48:19 pmAnyway, in the majority of cases, rdtsc/rdtscp can provide a reasonably good idea of relative performance. You're right, but keep in mind that massive use of these instructions overloads the processor. If you have 16 threads and use those instructions for each thread (for example, to monitor small parts like an algorithm), you risk using a lot of CPU time. The rdtsc/rdtscp instructions are "heavy" to execute. When I used them, for example, with about twenty (maybe even 24) threads, the processor load increased by 10-12% on an Intel I9 14900K CPU (already loaded with 50% processing) compared to using TStopWatch (which uses QueryPerformanceCounter). ---------------------------------------------------------------------- LV · 2026-01-23 16:10 If the algorithm can be parallelized, and as the thread's author mentions, "At present my "creator" takes about 20minutes" then using multithreading will clearly be beneficial. Additionally, a wall clock without a second hand will be adequate for tracking time. 🕰️  😉 ---------------------------------------------------------------------- 440bx · 2026-01-23 17:48 Quote from: LeP on January 23, 2026, 03:15:34 pmThe rdtsc/rdtscp instructions are "heavy" to execute. Yes, they are.  If a "relative" measurement is acceptable, QueryPerformanceCounter is fine.  OTH, it isn't as cheap as I'd like it to be given that it causes a ring transition and, that can also end up consuming a fair number of clock cycles if done "too often". rdtsc (not the p version) should be ok because it doesn't cause serialization which is the reason for the high overhead. For those interested in measurement accuracy, the following links provide interesting reading: https://stackoverflow.com/questions/12631856/difference-between- rdtscp-rdtsc-memory-and-cpuid-rdtsc https://stackoverflow.com/questions/27693145/rdtscp-versus-rdtsc-cpuid I haven't checked but, off hand, I believe that rdtsc _without_ the use of CPUID _may_ provide the best performance/methodology if instruction re-ordering is acceptable (allowed to be reflected in the measurements.) The point I'm trying to make is that, the pursuit of precise measurement is not as simple as it may initially appear.  GetTickCount64() is definitely quite "coarse" but, could definitely be acceptable in some cases where additional precision and accuracy wouldn't change the final _relative_ results. ---------------------------------------------------------------------- LV · 2026-01-24 08:49 This code is simply for morning exercise. I based it on reply #6 and made slight modifications to warm up the processor. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program threadbenchmark; {$mode objfpc}{$H+} uses  SysUtils, Classes; const  num = 100000000;  Tests: array[0..8] of Integer = (1, 2, 4, 6, 8, 10, 12, 14, 16); type  TRec = record    a, b, c: Integer;  end;  TArray = array of TRec;  PRec = ^TRec; type  TWorkerThread = class(TThread)  private    FL, FR: Integer;    FData: PRec;  protected    procedure Execute; override;  public    constructor Create(L, R: Integer; Data: PRec);  end; constructor TWorkerThread.Create(L, R: Integer; Data: PRec);begin  inherited Create(True);  FL := L;  FR := R;  FData := Data;end; procedure TWorkerThread.Execute;var  i: Integer;begin  for i := FL to FR do  begin    FData[i].a := i mod 10;    FData[i].b := (i + 5) mod 10;    FData[i].c := FData[i].a + FData[i].b;  end;end; function RunTest(var Arr: TArray; CurrentThreads: Integer): QWord;var  Threads: array of TWorkerThread;  i, L, R: Integer;  t0: QWord;begin   FillChar(Arr[0], Length(Arr) * SizeOf(TRec), 0);   SetLength(Threads, CurrentThreads);  t0 := GetTickCount64;   for i := 0 to CurrentThreads - 1 do  begin    L := (i * Length(Arr)) div CurrentThreads;    R := ((i + 1) * Length(Arr)) div CurrentThreads - 1;    Threads[i] := TWorkerThread.Create(L, R, @Arr[0]);    Threads[i].Start;  end;   for i := 0 to High(Threads) do  begin    Threads[i].WaitFor;    Threads[i].Free;  end;   Result := GetTickCount64 - t0;end; procedure Benchmark;var  Arr: TArray;  tSingle, tCurrent: QWord;  Speedup, Efficiency: Double;  i: Integer;begin  Writeln('Allocating ', (Int64(num) * SizeOf(TRec)) div (1024*1024), ' MB RAM...');  SetLength(Arr, num);   Writeln('Running benchmarks...');  Writeln;  Writeln('Threads | Time (ms) | Speedup | Efficiency');  Writeln('-------------------------------------------');   tSingle := RunTest(Arr, 1);  Writeln(1:7, ' | ', tSingle:9, ' |  1.00x  |   100.0%');   for i := 1 to High(Tests) do  begin    tCurrent := RunTest(Arr, Tests[i]);     if tCurrent > 0 then    begin      Speedup := tSingle / tCurrent;      Efficiency := (Speedup / Tests[i]) * 100;      Writeln(Tests[i]:7, ' | ', tCurrent:9, ' | ', Speedup:6:2, 'x  | ', Efficiency:7:1, '%');    end;  end;   Writeln('-------------------------------------------');  Writeln('Done. Press Enter to exit.');  Readln;end; begin  Benchmark;end.  On my old guy i7 8700 output: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Allocating 1144 MB RAM...Running benchmarks... Threads | Time (ms) | Speedup | Efficiency-------------------------------------------      1 |      1328 |  1.00x  |   100.0%      2 |       641 |   2.07x  |   103.6%    4 |       359 |   3.70x  |    92.5%      6 |       250 |   5.31x  |  88.5%      8 |       235 |   5.65x  |    70.6%     10 |       188 | 7.06x  |    70.6%     12 |       172 |   7.72x  |    64.3%     14 |     218 |   6.09x  |    43.5%     16 |       234 |   5.68x  |    35.5%-------------------------------------------Done. Press Enter to exit. ---------------------------------------------------------------------- LeP · 2026-01-24 12:38 This is from mine (14900HX): Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Allocating 1144 MB RAM...Running benchmarks... Threads | Time (ms) | Speedup | Efficiency-------------------------------------------      1 |       360 |  1.00x  |   100.0%      2 |       188 |   1.91x  |    95.7%    4 |       125 |   2.88x  |    72.0%      6 |       140 |   2.57x  |    42.9%      8 |        93 |   3.87x  |    48.4%     10 |        94 | 3.83x  |    38.3%     12 |        78 |   4.62x  |    38.5%     14 |      62 |   5.81x  |    41.5%     16 |        62 |   5.81x  |    36.3%-------------------------------------------Done. Press Enter to exit. ---------------------------------------------------------------------- BrunoK · 2026-01-24 15:48 I modified a bit LV's test program and made a few change so it runs also on my Linux mint installation. At the bottom, some timings obtained running that program (-O1 and -OoREGVAR optimizations). The objective of modifying LV's test was to compare various ways of getting sub millisecond time values. Using my uRdtsc, column RDTSC gives time measured using RDTSC that  has been calibrated with Windows QueryPerformance counter. The linux version uses the much finer grained (in linux) GetTickCount64 ms for calibration. In the Windows run, RDTSC and QPerfC columns are very near in values hence showing RDTSC can be quite safely used for short timing benchmarks. In the Linux run, the "Time (ms)" column matches RDTSC column indicating that RDTSC can be quite safely used for short timing benchmarks. Windows 10, on my system, the maximum throughput is obtained for thread count = GetCPUCount. CPU count  8 --> Windows Allocating 1144 MB RAM... Running benchmarks... Threads | Time (ms) |    RDTSC   |  QPerfC  |  Speedup | Efficiency -------------------------------------------------------------------       1 |       516 | 514.277 ms |  514.276 |   1.00x  |     0.0%       1 |       531 | 518.190 ms |  518.188 |   0.97x  |    97.2%       2 |       265 | 266.587 ms |  266.586 |   1.95x  |    97.4%       3 |       172 | 183.179 ms |  183.177 |   3.00x  |   100.0%       4 |       203 | 187.850 ms |  187.849 |   2.54x  |    63.5%       5 |       172 | 171.002 ms |  171.001 |   3.00x  |    60.0%       6 |       156 | 168.870 ms |  168.869 |   3.31x  |    55.1%       7 |       141 | 147.154 ms |  147.153 |   3.66x  |    52.3%       8 |       141 | 137.597 ms |  137.596 |   3.66x  |    45.7%       9 |       172 | 178.021 ms |  178.020 |   3.00x  |    33.3%      10 |       156 | 148.710 ms |  148.709 |   3.31x  |    33.1%      11 |       156 | 147.953 ms |  147.952 |   3.31x  |    30.1%      12 |       156 | 153.956 ms |  153.955 |   3.31x  |    27.6%      13 |       140 | 140.507 ms |  140.506 |   3.69x  |    28.4%      14 |       157 | 146.378 ms |  146.378 |   3.29x  |    23.5%      15 |       141 | 139.950 ms |  139.949 |   3.66x  |    24.4%      16 |       140 | 145.673 ms |  145.672 |   3.69x  |    23.0% ------------------------------------------------------------------- Done. Press Enter to exit. CPU count  1 --> Linux Allocating 1144 MB RAM... Running benchmarks... Threads | Time (ms) |    RDTSC   |  QPerfC  |  Speedup | Efficiency -------------------------------------------------------------------       1 |       501 | 500.970 ms |  ???.??? |   1.00x  |     0.0%       1 |       501 | 501.097 ms |  ???.??? |   1.00x  |   100.0%       2 |       301 | 301.004 ms |  ???.??? |   1.66x  |    83.2%       3 |       201 | 200.685 ms |  ???.??? |   2.49x  |    83.1%       4 |       201 | 200.786 ms |  ???.??? |   2.49x  |    62.3%       5 |       201 | 200.822 ms |  ???.??? |   2.49x  |    49.9%       6 |       201 | 200.892 ms |  ???.??? |   2.49x  |    41.5%       7 |       201 | 201.131 ms |  ???.??? |   2.49x  |    35.6%       8 |       201 | 201.185 ms |  ???.??? |   2.49x  |    31.2%       9 |       202 | 201.354 ms |  ???.??? |   2.48x  |    27.6%      10 |       208 | 207.612 ms |  ???.??? |   2.41x  |    24.1%      11 |       205 | 204.436 ms |  ???.??? |   2.44x  |    22.2%      12 |       209 | 208.845 ms |  ???.??? |   2.40x  |    20.0%      13 |       207 | 207.361 ms |  ???.??? |   2.42x  |    18.6%      14 |       207 | 207.307 ms |  ???.??? |   2.42x  |    17.3%      15 |       211 | 210.910 ms |  ???.??? |   2.37x  |    15.8%      16 |       211 | 210.598 ms |  ???.??? |   2.37x  |    14.8% ------------------------------------------------------------------- Done. Press Enter to exit. Attached, project that outputs above timings. ---------------------------------------------------------------------- creaothceann · 2026-01-24 15:53 AMD 7800X3D, -O3 Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Allocating 1144 MB RAM...Running benchmarks... Threads | Time (ms) | Speedup  | Efficiency-------------------------------------------      1 |       281 |   1.00x  |   100.0%      2 |       140 |   2.01x  |   100.4%    4 |        93 |   3.02x  |    75.5%      6 |        62 |   4.53x  |  75.5%      8 |        63 |   4.46x  |    55.8%     10 |        47 | 5.98x  |    59.8%     12 |        47 |   5.98x  |    49.8%     14 |      47 |   5.98x  |    42.7%     16 |        47 |   5.98x  |    37.4%-------------------------------------------Done. Press Enter to exit. ---------------------------------------------------------------------- LeP · 2026-01-24 16:50 With Delphi ..... near half time respect mine in Lazarus 4.4 FPC 3.2.2 (@LV approach)  Strange, usually FPC is more performant. Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Allocating 1144 MB RAM...Running benchmarks... Threads | Time (ms) | Speedup | Efficiency-------------------------------------------      1 |       147 |  1.00x  |   100.0%      2 |        81 |   1.81x  |    90.7%    4 |        60 |   2.45x  |    61.3%      6 |        37 |   3.97x  |    66.2%      8 |        43 |   3.42x  |    42.7%     10 |        37 | 3.97x  |    39.7%     12 |        36 |   4.08x  |    34.0%     14 |      37 |   3.97x  |    28.4%     16 |        35 |   4.20x  |    26.3%-------------------------------------------Done. Press Enter to exit. ---------------------------------------------------------------------- cdbc · 2026-01-24 22:11 Hi Right, so I had to dig deep into my old "Sandbox / Testbed", to find a project that might be of use to you... It's an app that resamples images, with the help of BGRABitmap, that's a process that can take a while to complete, so threading is in order... It creates a threadpool with your choice count and then utilizes the pool in the resampling of more images at once... To compile it you'll ofc. need BGRABitmap (I fetched the one in OPM tonight). edit: You don't have to install BGRABitmap, just enter its path in "-Fu"   (I hate installing comps) The attached project compiles on FPC 3.2.2 & Laz 4.4 On my old lappy, it takes a while to build, due to the sheer size of BGRABitmap... But I think there should be something in there you can study and rewrite to your own needs...  Well Have fun & happy coding Regards Benny ---------------------------------------------------------------------- creaothceann · 2026-01-25 11:24 i7-4790K ---------------------------------------------------------------------- cdbc · 2026-01-25 13:05 Hi See edit in post #32 Regards Benny ---------------------------------------------------------------------- LV · 2026-01-25 13:54 It looks like we're encountering the "Memory Wall  " effect in this benchmark. i7-12700H. Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Pascal FPC 3.2.2 -O3------------------------------------------------------------- Allocating 1144 MB RAM... -------------------------------------------------------------Threads | Time (ms) | Speedup | Efficiency | Bandwidth (GB/s)-------------------------------------------------------------    1 |       187 |  1.00x   |   100.0%  |           5.98      2 |     110 |   1.70x  |    85.0%  |          10.16      4 |        62 |   3.02x  |    75.4%  |          18.03      6 |        47 |   3.98x  |    66.3%  |          23.78      8 |        47 |   3.98x  |    49.7%  |        23.78     10 |        47 |   3.98x  |    39.8%  |          23.78     12 |        47 |   3.98x  |    33.2%  |          23.78     14 |        47 |   3.98x  |    28.4%  |          23.78     16 |      47 |   3.98x  |    24.9%  |          23.78     18 |        46 |   4.07x  |    22.6%  |          24.30     20 |        47 |   3.98x  |    19.9%  |          23.78     22 |        47 |   3.98x  |    18.1%  |        23.78     24 |        47 |   3.98x  |    16.6%  |          23.78 Pascal FPC 3.2.2 -O3 ------------------------------------------------------------- Allocating 4577 MB RAM... -------------------------------------------------------------Threads | Time (ms) | Speedup | Efficiency | Bandwidth (GB/s)-------------------------------------------------------------    1 |       828 |  1.00x   |   100.0%  |           5.40      2 |     422 |   1.96x  |    98.1%  |          10.59      4 |       250 |   3.31x  |    82.8%  |          17.88      6 |       172 |   4.81x  |    80.2%  |          25.99      8 |       172 |   4.81x  |    60.2%  |        25.99     10 |       156 |   5.31x  |    53.1%  |          28.66     12 |       172 |   4.81x  |    40.1%  |          25.99     14 |       156 |   5.31x  |    37.9%  |          28.66     16 |     172 |   4.81x  |    30.1%  |          25.99     18 |       172 |   4.81x  |    26.7%  |          25.99     20 |       172 |   4.81x  |    24.1%  |          25.99     22 |       187 |   4.43x  |    20.1%  |        23.91     24 |       172 |   4.81x  |    20.1%  |          25.99  Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i C++ gcc 14.2.0 -O3 ------------------------------------------------------------- Allocating 1144.41 MB RAM...Threads | Time (ms) | Speedup | Efficiency | Bandwidth (GB/s)-------------------------------------------------------------    1 |       213 |  1.00x   |   100.0%  |           5.25      2 |      53 |   4.02x  |  200.94%  |          21.09      4 |        44 |   4.84x  |  121.02%  |          25.40      6 |        43 |   4.95x  |   82.56%  |          25.99      8 |        44 |   4.84x  |   60.51%  |        25.40     10 |        44 |   4.84x  |   48.41%  |          25.40     12 |        44 |   4.84x  |   40.34%  |          25.40     14 |        44 |   4.84x  |   34.58%  |          25.40     16 |      44 |   4.84x  |   30.26%  |          25.40     18 |        43 |   4.95x  |   27.52%  |          25.99     20 |        44 |   4.84x  |   24.20%  |          25.40     22 |        43 |   4.95x  |   22.52%  |        25.99     24 |        44 |   4.84x  |   20.17%  |          25.40 C++ gcc 14.2.0 -O3 ------------------------------------------------------------- Allocating 4577.64 MB RAM...Threads | Time (ms) | Speedup | Efficiency | Bandwidth (GB/s)-------------------------------------------------------------    1 |       894 |  1.00x   |   100.0%  |           5.00      2 |     212 |   4.22x  |  210.85%  |          21.09      4 |       167 |   5.35x  |  133.83%  |          26.77      6 |       167 |   5.35x  |   89.22%  |          26.77      8 |       172 |   5.20x  |   64.97%  |        25.99     10 |       170 |   5.26x  |   52.59%  |          26.30     12 |       166 |   5.39x  |   44.88%  |          26.93     14 |       166 |   5.39x  |   38.47%  |          26.93     16 |     165 |   5.42x  |   33.86%  |          27.09     18 |       165 |   5.42x  |   30.10%  |          27.09     20 |       168 |   5.32x  |   26.61%  |          26.61     22 |       165 |   5.42x  |   24.63%  |        27.09     24 |       166 |   5.39x  |   22.44%  |          26.93 ---------------------------------------------------------------------- LV · 2026-01-25 22:04 🤔 To demonstrate multithreaded scalability, let's burden the threads with intensive algebraic operations. i7-12700H. Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Pascal FPC 3.2.2 -O3 (Double Precision + Heavy Algebra)-------------------------- ---------------------------------------------------Allocating 2288 MB RAM...---------------------------------------------------------------- -------------Threads | Time (ms) | Speedup    | Efficiency  | Bandwidth (GB/s) | Validate------------------------------------------- ----------------------------------      1 |      9109 |   1.00x    | 100.00%   |             0.25 | OK      2 |      4750 |    1.92x   |    95.88%   |             0.47 | OK      4 |      2594 |    3.51x   |    87.79%   |             0.86 | OK      6 |      1797 |    5.07x   |    84.48%   |             1.24 | OK      8 |      1656 |    5.50x   |    68.76%   |             1.35 | OK     10 |      1453 |    6.27x   |    62.69%   |             1.54 | OK     12 |      1344 |    6.78x   |    56.48%   |             1.66 | OK     14 |      1250 |    7.29x   |    52.05%   |             1.79 | OK     16 |      1187 |    7.67x   |    47.96%   |             1.88 | OK     18 |      1172 |    7.77x   |    43.18%   |             1.91 | OK     20 |      1187 |    7.67x   |    38.37%   |             1.88 | OK     22 |      1141 |    7.98x   |    36.29%   |             1.96 | OK     24 |      1125 |    8.10x   |    33.74%   |             1.99 | OK------------------------------------- ----------------------------------------  Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i C++ gcc 14.2.0 -O3 (Double Precision + Heavy Algebra)------------------------- ----------------------------------------------------Allocating 2288 MB RAM...---------------------------------------------------------------- -------------Threads | Time (ms) | Speedup  | Efficiency  | Bandwidth (GB/s) | Validate----------------------------------------------------- ------------------------      1 |      9340 |   1.00x  |    100.00%  |             0.24 | OK      2 |      4571 |   2.04x  |    102.17%  |           0.49 | OK      4 |      2504 |   3.73x  |     93.25%  |           0.89 | OK      6 |      1739 |   5.37x  |     89.52%  |           1.29 | OK      8 |      1535 |   6.08x  |     76.06%  |           1.46 | OK     10 |      1410 |   6.62x  |     66.24%  |             1.59 | OK     12 |      1312 |   7.12x  |     59.32%  |             1.70 | OK     14 |      1244 |   7.51x  |     53.63%  |             1.80 | OK     16 |      1161 |   8.04x  |     50.28%  |             1.93 | OK     18 |      1086 |   8.60x  |     47.78%  |             2.06 | OK     20 |      1174 |   7.96x  |     39.78%  |             1.90 | OK     22 |      1256 |   7.44x  |     33.80%  |             1.78 | OK     24 |      1292 |   7.23x  |     30.12%  |             1.73 | OK------------------------------------------------------------- ----------------  The P-Core Zone (1–6 Threads) Perfect Scaling: Efficiency remains above 85-90% up to 6 threads. These are 6 high-performance cores working at full tilt. C++ is slightly ahead here (5.37x Speedup vs. Pascal's 5.07x), likely due to GCC 14’s more aggressive instruction scheduling. The E-Core Entry (8–14 Threads) This is where it gets interesting. After the 6th thread, the speedup stops being linear. Heterogeneity in Action: At 14 threads (6P + 8E), the execution time hits 1244–1250ms. Note that adding the 8 small cores provided a significant boost, but Efficiency naturally dropped to ~50-53%. This is expected: E-cores are slower, which reduces the "average" performance per thread. Hyper-Threading & Over-Saturation (16–24 Threads) Peak Performance: C++ peaks at 18 threads (1086ms), while Pascal peaks at 24 threads (1125ms). C++ Performance Drop: Look at the C++ results after 18 threads—the time begins to increase (1086 -> 1174 -> 1292). This is a classic "pipeline stall" effect. Hyper-Threading on the P-cores starts fighting for the same DIVSD (Division) units that are already 100% saturated. Pascal’s Stability: Pascal remains more stable at extremely high thread counts (1125ms at 24 threads). This suggests the code generated by FPC might be creating less "noise" in the instruction cache or interacting differently with the Windows thread scheduler. 🚴