I need a 3GB internal harddrive. Looking through statistic data from BackBlaze, it seems that Seagate is the worst solution because of it HUGE failure rate. Yes the prices are good for this drive but you don't want a cheap drive that will fail after 2 years. Right?
The next brand with high failure rate is Western Digital.
Both Seagate and WesternDigital drive have price in the 100 euros range.
Toshiba has a much better failure rate.
But the winner is HGST. It has a significant higher price (126 euro) but totally worth it.
You want to do amateur or power-level computing? Then there is no other choice than Windows! Period. As a Windows user you don't have to spend tons of a money for a Mac PC (sorry Mac guys but Mac is nothing else than an expensive PC now) and you don't have to waste time on forums to learn how install stuff. All that Linux/Mac can offer you can get on Windows and get is better. I spent many years working with Linux but it was always only my second choice. Sorry Linux friends, but I said it.
Showing posts with label speed. Show all posts
Showing posts with label speed. Show all posts
Tuesday, September 20, 2016
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:
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/
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
- 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/
Labels:
class,
cpu,
delphi,
memory allocation,
optimization,
speed,
string,
string builder
Friday, September 10, 2010
We all know DotNet sucks
We all know DotNet sucks. But how bad it sucks?
James S. Gibbons put this to the test. And the results are impressive. DotNet is 'coding horrors' came to life.
I cannot image the person that really chooses to use in interpreted language like DotNet.
James S. Gibbons put this to the test. And the results are impressive. DotNet is 'coding horrors' came to life.
I cannot image the person that really chooses to use in interpreted language like DotNet.
Subscribe to:
Posts (Atom)