I ran into this issue when loading long HTML pages into components such as TIpHtmlPanel and THtmlViewer, and I discovered that the same operation (loading a web page) on the Mac was hundreds of times slower than on Windows. (Same CPU)
After a lot of digging, I isolated the "culprit": the GetTextExtent method. It calculates the area that a text string will occupy on a TCanvas. This method is called many times when rendering HTML pages on the screen, to calculate where the lines should break, the height of various elements, etc.
On Windows, the method call looks like this:
Windows.GetTextExtentPointW(DC, Str, Count, Size);
And on MacOS, it looks like this:
LclIntf.GetTextExtentPoint32(DC, PChar(s), Length(s), Size);
I wrote a test program that can be compiled on both Windows and MacOS and that will show the difference in performance. The source code is here:
Source code:
https://tinyurl.com/win-mac-testAnd here is a screenshot with performance times on both Windows and Mac:
Screenshot:
https://imgur.com/a/fGLXXDYHow to read the screenshot:
First of all, compare data points C. That's the time (milliseconds) spent in performing generic operations. Note that this time is about the same on both systems. This shows that the Windows and the MacOS have a similar speed.
Now, data point A shows that the GetTextExtent call was performed 6600 times on both machines. On Windows, this took a total of 1.2+ milliseconds. On MacOS, this took a total of 435.8+ milliseconds. That's more than 300 times slower.
Additional data on the same line shows you the slowest time, the average time, and the slowest time. For example, the fastest call time on Windows was 0.000099 msec while on MacOS it was 500 times slower, at 0.05 msec. And so on...
Data point B shows a different test: The GetTextExtent method is called 1000 times in a row, with the same parameters, and the time is measured. For some mysterious reason, when all these calls are packed together, instead of being done in the middle of some other code, the difference between Windows and Mac is less, but even then MacOS is still at least 10 times slower than Windows.
Now, the obvious question: can anything be done to improve the situation?
Opening long HTML pages on the Mac is really slow, with pages taking several seconds on the Mac, which instead open instantly on Windows.
I assume that GetTextExtent (and variations thereof) were originally Windows API, and that they needed to be somehow emulated on other platforms. Some loss of performance may be inevitable. But hundreds of times slower?
Also note this: In data point B, when repeating the same call over and over, with the same parameters, the difference between the slowest and the fastest time is a factor of 10 on Windows, but it's a factor of 150 on MacOS.
I compiled both versions without debugging, etc. And again, data points C show that the performance on both machines is similar, so it's not about CPU speed at all.
So, why this huge difference, and most importantly, is it somehow possible to improve things on the Mac?
Can anyone shed some light, please?
Source code:
https://tinyurl.com/win-mac-testScreenshot:
https://imgur.com/a/fGLXXDYThank you!