In this post, we’ll look at how to use a Windows compiler to improve Delphi applications and shorten development time. Furthermore, we learn how to debug more quickly and intelligently by utilizing lesser-known but valuable debugger features such as groups, thread support, data breakpoints, and others.
- Faster Parallel Programs
- Compiler settings for effectively debug
- Breakpoints
- Passcount
- Debugging Multithreaded Programs
- Exceptions
- Hardware breakpoints
- and much more
Table of Contents
How to write fast parallel programs in Delphi?
If you have been following CodeRage Online Developer Conferences, you know about FastMM4 and FastMM5. FastMM5 is a fast replacement memory manager for Embarcadero Delphi applications that scales well across multiple threads and CPU cores, is not prone to memory fragmentation, and supports shared memory without the use of external files.
How can I achieve fast programs in Delphi using FastMM?
With the newest update of FastMM that is FastMM5, you can achieve 15% higher performance on the single-threaded, and 30% higher on the multithreaded benchmarks.
Reasons for slowdown
If your Delphi application does not scale on a new multiprocessor, multicore, or multithreaded hardware, the obstacle might be lock contention in the memory allocator. Because threads are fighting for allocators.
The obvious solution when the memory manager is slowing you down is to utilize fewer memory allocations that are either preload the memory and then reuse it without releasing it back to the system or change the algorithm in your code.
You can find different methods to improve your application. Now it is demonstration time. We have an application here that do some calculations and we do that calculation with several workers to achieve higher performance
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
procedure TForm1.Button1Click(Sender: TObject); begin SpeedTest := TSpeedTest.create(100); SpeedTest.NumWorkers := inpNumWorkers.Value; Button1.Enabled := false; Screen.Cursor := crHourGlass; Form1.Caption := 'Time = ' + SpeedTest.Execute.ToString; SpeedTest.Free; Button1.Enabled := true; Screen.Cursor := crDefault; end; |
As you can see, this is the driver code. It creates a test with some parameters, then sets the number of workers and executes the test with workers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
function TSpeedTest.Execute: integer; var i: Integer; StopWatch: TStopwatch; begin Stopwatch := TStopwatch.Create; Stopwatch.Reset; Stopwatch.Start; if fNumWorkers > 1 then Parallel.For(0, fList.Count - 1) .NumTasks(fNumWorkers) .Execute( procedure(i: integer) begin fList[i].Execute; end) else for i := 0 to fList.Count - 1 do fList[i].Execute; Stopwatch.Stop; result := Stopwatch.ElapsedMilliseconds; end; |
In the execute function, it just goes over the list. fList contains 100 items that we created in the constructor. As you can see, it goes over using parallel for loop if there is the worker number given.
It is like we are doing some stress test on memory manager to benchmark.
Here are the results with 1 and 2 workers:
Now with 8 workers, you can see a little better result:
If you would like to learn more about this, you can watch our CodeRage session here on FastMM and Delphi: [https://www.youtube.com/watch?v=p-5mJyXvmrc]
How to effectively debug Delphi programs?
To effectively debug a program you should set few project options which can be found in the project options dialog. Here are some important settings are stored in the Delphi compiling tab. Follow the picture here:
- It is usually preferred to disable the optimization in the debug mode. It can help to better watch and evaluate values.
- Turn on Debug Information property if you want to tap into RTL along with Use debug .dcus options.
Besides, there are other configurations you should know about. Go to the Tools menu and click Options. Then at the bottom, click the Debugger tab.
Most experienced Delphi developers prefer to check some options to make debugging processes easier. For instance, you can check the Rearrange editor local menu on the run option. So that you can have debug menus available at the top of the rick click menu. Furthermore, allowing the Allow side effects and function calls in new watches, so that your watch is fully functional.
How to use Breakpoints in RAD Studio Delphi?
Each breakpoint which you can set by pressing F5 or with the mouse has a bunch of properties associated with it. To access properties for breakpoints you can right-click the breakpoint or breakpoint list window.
Overall, breakpoints pause program execution at a certain location or when a particular condition occurs. You can set source breakpoints and module load breakpoints in the Code Editor before and during a debugging session.
Besides, there are several options available in the IDE, for instance:
- Breakpoint modification
- Breakpoint group
- Enabling and disabling certain breakpoint or a breakpoint group
- Conditional breakpoint
- The breakpoint on a specific thread
- Providing different colors for breakpoints to highlight the process
- and more
Be sure to read the official documentation on Embarcadero DocWiki.
How to use the pass count breakpoint option in Delphi?
By default, the pass count is set to 1. So, if the pass count equals 1 when the breakpoint line is encountered, the breakpoint is activated and pauses the program execution on that line of code.
Pass count – provides you to specify a particular number of times that a breakpoint needs to be passed for the breakpoint to be activated. So the pass counter tells the debugger to pause the program execution the nth time that the breakpoint is encountered during the program run.
How to debug multithreaded Delphi programs?
When debugging multithreaded applications there will be some limitations on debugging sessions/threads. And it works with only one thread.
For instance, when some code is called from more than one thread, and we put a breakpoint that is only activated from one thread and not from other available threads.
One of the best practices in naming threads. By setting a name for threads you can evaluate the process easily. Here is a sample code on naming threads:
1 2 3 4 5 6 7 8 9 10 11 |
procedure ThreadProc; var I: Integer; begin TThread.NameThreadForDebugging('Demo Thread'); for I := 1 to 100 do begin Sleep(100); DoSomething(55); end; end; |
How to use Hardware Breakpoints?
In addition to software breakpoints which always slow down the execution of a program, RAD Studio provides hardware-level breakpoints.
Hardware breakpoints – make use of dedicated registers and hence are limited in number. These can be set on both volatile and nonvolatile memory.
They are triggered when a value in memory is modified this is incredibly useful when you want to find out why some variable is changing its value when it shouldn’t have to downsides of hardware breakpoints out.
Learn more about breakpoints on Embarcadero DocWiki.
Where to learn even more about the secrets of super-fast Delphi
To finish off, you can follow this webinar to dive even deeper into the secrets of Delphi!
RAD Studio Delphi is the fastest way to create applications which can run on Windows, macOS, Linux, iOS and Android. Download a free trial copy today and turn your ideas into applications your users will love.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition
I just loaded embarcadero. I wrote a program to read an text file into an array. it says file not found. I cannot find the exe file. it compiles correct. where do I find the exe file. thank you
While this is not really the place for support – you can try the following:
1. In RAD Studio Delphi IDE go to the project manager window (use CTRL + ALT + F11 to show it if it’s not aleady)
2. Now right click on your project’s name (it should end in .exe)
3. Now an Explorer window should open. Scroll to the top of it and you should see an entry saying Win32 (or iOS or Android depending on your app’s target)
4. Double click on that folder. There should be one or two sub-folders in there: one for Debug and one for Release depending on how you were compiling your app. In release mode the “Release” folder would be the exe or app package you want to distribute to users.