Tuesday, March 6, 2012

Intraweb TextToHTMLStringLiteral in a multi-threaded test

In a recent blog post (Speeding Intraweb up again) I wrote that I suspected that in a multi-threaded test, running in a multi-core computer, the modified version of TextToHTMLStringLiteral would perform even better. So I decided to test it in this context. Now I can tell you that I was right ;-)

The test

Today I created a new multi-threaded test. Two threads running in parallel doing the same thing: Taking a string and using TextToHTMLStringLiteral to convert it multiple times, inside a tight loop. During the first test, both threads used the original TextToHTMLStringLiteral implementation. In the second test, I used my modified version. Both threads execute the following code:
procedure TMyThread.Execute; 
  FCount := 0;
    FStr := 'ABCD XPTO %^&* ABCD XPTO %^&* ABCD XPTO %^&*';
    FStr := TextToHTMLStringLiteral(FStr);
  until FCount = MaxCount;

CPU Utilization

The fist thread CPU utilization chart can be seen below.

Note that there are two threads but CPU utilization is only slightly above 50%.

Now the second CPU utilization chart:

Note that now CPU utilization is almost 100% (and always above 90%). Of course there are a few LOCKed instructions here and there, but it is clearly more multi-core friendly.

The results

Original TextToHTMLStringLiteral time: 24.4 seconds (using ~ 55% CPU x 2)

Modified TextToHTMLStringLiteral time: 3.6 seconds (using ~ 98% CPU x 2)
The time measures showed that the modified code performs almost 7 times better than the original, using 2 threads (It was 6 times better in a single thread app). We can expect that the more cores you have, the bigger will be time difference.


We can see clearly that Delphi code written with special care, specially code dealing with string writing/concatenation, can greatly improve performance and make the compiled code much more multi-core friendly.

No comments: