Hi, I recently noticed that FPC has optimizations, and those can be set from the Lazarus IDE in Project Options, Package Options and IDE Build Modes.
My question is: Which are really useful for improving speed and reducing size?
For example, I can set:
- Project Options: Optimizations Level 3, remove debug and strip symbols from exe, smart linking, keep certain variables in registers.
- Package: Optimizations Level 3, remove debug, keep certain variables in registers.
- Lazarus IDE: compile as Optimized IDE (Optimizations Level 2). Also we can set the options we want (for example, the same as Project Options).
- Project options apply to the files in your unit directory (unit1.pas), but not the packages, like LCL
- Package options apply, to whatever unit belong to that packages.
--- Packages have a "usage" section, and you can put options (as command line switches "-O2"), in there ("Custom" memo), then they apply to other packages, IF the other packages uses/depends on this. "LCL" uses "FCL" => if FCL has -O2 in Usage, then LCL uses that too.
- Menu Tools, configure build Lazarus: Applies to how the IDE is build. (Similar to project)
IT does affect "SOME, but NOT all" packages. This is concrolled by a package using $(IDEBuildOptions) in its "custom" options ("Other" in the Tree)
In the IDE build modes there's a place for Packages in the 'Information' tab and it says 'Additional compiler info inherited from packages' What this means?
If a package, has options that will be used by the project... E.g. the project must include the Path of each package.
It shows options affecting the IDE/project units. The units in the package are build according to the package's options.
Put something into a packages "Usage" setting, and it should show up.
If I set optimizations for each package size will reduce? (FCL, LCL, LCLBase)..? Or the compiler build with the same as Lazarus?
You need to set for each package.
Or put -O2 and other into FCL usage (almost all packages use FCL). However setting -O2 and others, READ ABOUT DEBUGGING. You will run into trouble.
What I've noticed:
- executable size goes down when I set strip symbols from executable.
- executable size goes down when I set Lazarus Build: Optimized IDE.
strip affects debug info. You always want it off when building releases, but need it when debugging. Debugger will also NOT work, if you have optimization on. SO if you put that global for all packages, you can not debug inside packages, and if the global option happens to affect your project, then debugging will not work (you will see wrong values, if you look at what value a variable has)
There are "Build modes" so you can switch many settings at once. There is some info on the wiki, but maybe some one else can explain more.
Things that I don't know how to measure: speed optimization. How I can notice if the program is faster?
Use a profiler (valgrind on linux), There is fpprofiler: google.
On Windows profiling is hard...
Then again, if you do not notice you may not need it.
You can also put the code into a loop, and run it 100000 times.
Mind of course that the execution time also depends, on what else your PC does at the time. SO it will vary anyway.
Also speed is archived by choosing the right algorithm. e.g on a sorted list, do a binary search, instead of going through all elements of a list...