Friday, April 29, 2016

Is (Embarcadero) Delphi's code optimized for speed? No? Here is how to optimize string speed

The short answer is a big NO!

No the long answer:
Today I needed a function that will wrap a string (a contiguous block of characters with no spaces) after 80 characters. Not only that I have found SysUtils.WrapText unsuitable (it can only wrap text IF the text contains spaces) but it is also terrible slow.

So I build my own function:

function WrapString(CONST s: string; RowLength: integer): string;
VAR i, Row: Integer;
Begin
 Row:= 0;
 Result:= '';
 for i:= 1 TO Length(s) DO
  begin
   inc(Row);
   Result:= Result+ s[i];
   if Row >= RowLength then
    begin
     Result:= Result+ CRLF; 

     Row:= 0;
    end;
  end;
End;


Works nice but is is also slow. If you look into the code the problem is Result:= Result+ CRLF . It involves too many memory allocations.

Solution. The solution is to pre-allocate space for the result.
For this I created a new class TCStringBuilder:


TYPE
 TCStringBuilder = class(TObject)
  private
   s: string;
   CurBuffLen, BuffPos: Integer;
  public
   BuffSize: Integer;
   constructor Create(aBuffSize: Integer= 10*Kb);
   procedure AddChar(Ch: Char);
   procedure AddEnter;

   function  AsText: string;
   procedure Clear;
 end;



IMPLEMENTATION

constructor TCStringBuilder.Create(aBuffSize: Integer= 10*Kb);
begin
 BuffSize:= aBuffSize;
 Clear;
end;


procedure TCStringBuilder.Clear;
begin
 BuffPos:= 1;
 CurBuffLen:= 0;
 s:= '';
end;


function TCStringBuilder.AsText: string;
begin
 SetLength(s, BuffPos-1);                    { Cut down the prealocated buffer that we haven't used }
 Result:= s;
end;


procedure TCStringBuilder.AddChar(Ch: Char);
begin
 if BuffPos > CurBuffLen then
  begin
   SetLength(s, CurBuffLen+ BuffSize);
   CurBuffLen:= Length(s)
  end;

 s[BuffPos]:= Ch;
 Inc(BuffPos);
end;


procedure TCStringBuilder.AddEnter;
begin
 if BuffPos+1 > CurBuffLen then    { +1 because we enter two characters into the string instead of 1 }
  begin
   SetLength(s, CurBuffLen+ BuffSize);
   CurBuffLen:= Length(s)
  end;

 s[BuffPos  ]:= CR;
 s[BuffPos+1]:= LF;
 Inc(BuffPos, 2);
end;



Speed test:
  • 500x loop
  • test file: TesterForm.pas 2.7K
  • wrap after 20 chars
Speed test results:
  •   484ms  SysUtils.WrapText - unbuffered
  •   5788ms WrapString        - unbuffered (Result:= Result+ s[i])
  •   31ms   WrapString        - buffered (using cStrBuilder)

I used a buffer of 10K. but the ideal buffer size would be the size of the input text plus 3%.

Please let me know if you can further improve this. Enjoy.

____

Further reading:
https://www.delphitools.info/2013/10/30/efficient-string-building-in-delphi/2/

Wednesday, April 20, 2016

Animated desktop wallpaper


BioniX Wallpaper Animator v3 is ready for download. That is great about this new version is that it is (should be) able to paint the animation UNDER desktop icons.

However, since we cannot test it on all Windows operating systems we need some help. If you can run it on any of the following systems please let us know the results.


Download link:
www.bionixwallpaper.com/downloads/Animated Desktop Wallpaper


Win 98 Win 2K Win Me Win XP Win Vista Win 7 Aero Win 7 Aero disabled Win 8 Win 10
? ? ? ? ? OI ? (should work) ? UI

Legend:
   UI = under icons
   OI = over icons
    ?  = unknown (not tested)

______________________________________

Plans for the next version:
  • Support for AVI files
  • Lower memory footprint



Friday, April 8, 2016

Three software programs that [used to] plague our computers: Acrobat, Java, Flash

There used to be three programs that really made our computer miserable. They were slow, bloated, intrusive and a  lot of security holes opened with them when you installed.



Java - Probably Java is the nastiest of all

Until few years ago you could not browse some web sites because some derailed 'web developers' decided to put some 'cool' animations and menus on their web site that required Java, forcing YOU to install Java, this way.
Now, Java is a really intrusive mammoth that starts at computer start up, installs background services, make your browser literary crawl eating A LOT of memory and keep wasting your Internet bandwidth by checking and downloading updates. And boy, Java needs updates because every 3-4 days a MAJOR security hole was discovered! And every security hole in Java is a security hole in YOUR computer.

ACTUALLY, Java is so dangerous for your computer that "The U.S. Department of Homeland Security is advising people to temporarily disable the Java software on their computers to avoid potential attacks" floridatoday.com
It is so bad that Firefox won't even let you activate it.

I uninstalled Java maaaaany years ago from my computer and I vehemently refuse to install it. Not a single time! And many others like me have forced the web designer community not to use Java on their web site. Now probably less than 1 in 5000 web sites still have Java. So, you miss it a bit.



Acrobat Reader - Big deal for nothing

Adobe Acrobat Reader is basically a notepad. Its task should be to open a PDF file and show it on screen. It does this in such a bombastic way that 3 minutes later after Acrobat, but it is still crunching data, you forgot why you wanted to open that PDF anyway.

Same bubonic 'features' as Java: eats up lots or memory and disk space, invades you with background services, system tray icons and installs itself to run at computer start up.
Security wholes? Plenty.

Alternatives: Foxit PDF (once a decent and fast PDF viewer, now starting to step on exactly the same sick path as Acrobat Reader), Sumatra PDF. More about these Acrobat Reader alternatives.

And something else: Next time somebody sends you a PDF send him an email back and say "I politely request to send me the document as DOC or RTF. They are as good as PDF" then optionally sign: "Fuck you and best regards retard".



Adobe Flash Player

The Flash Player literary built and shaped the web as we know it. And this is not the web should be.
Firefox will also deactivate the Flash plugin when a MAJOR security whole is discovered. And boy, coming from Adobe, there are lots of those!
Flash is so wrong in every possible way: difficult to program, terribly slow (and I mean it), bulky and unsafe. But Adobe really pushed money into it so they managed to shovel it down to our throats. 

Alternatives: HTML5 - After all those years, the web community is finally coming to its senses trying to replace Flash with HTML 5, a major update of (too) old HTML. This change should have been done 25 years ago (which on Internet time-scale means 2 eons ago).

Most web sites already abandoned Flash and switched to HTML 5. So, it is not the time to turn off the Flash plugin in your browser and finally take a fresh breath of Flash-free Internet.

Also see "Flash is dying a death by 1,000 cuts, and that's a good thing" by TheGuardia.