[fpc-pascal] FPC now 3rd in shootout
S. Fisher
expandafter at yahoo.com
Tue Nov 6 03:24:33 CET 2007
--- Peter Vreman <peter at freepascal.org> wrote:
> reasonable time. The updated source can be found in:
>
>
http://svn.freepascal.org/svn/fpc/trunk/tests/bench/shootout/src/regexdna.pp
>
> But the code is a lot slower than gcc so there is still a lot of
> performance tuning to do:
>
> 100000 300000 500000
> fpascal 1.169 4.581 11.754
> gcc-2 1.033 3.078 5.111
I made it somewhat faster by improving the speed of concatenating
to an ansistring.
{ The Computer Language Benchmarks Game
http://shootout.alioth.debian.org
contributed by Steve Fisher
modified by Peter Vreman
modified by Steve Fisher
compile with
fpc -O3 regex-dna.pp
}
uses regexpr,strutils;
// Append 2 strings to an ansistring rapidly. Note: the ansistring's
// length will be increased by a more than sufficient amount.
function append2( var dest: ansistring; len0: longint;
s1: pchar; len1: longint;
s2: pchar; len2: longint): longint; inline;
const quantum = 599000;
var newlength: longint;
begin
newlength := len0 + len1 + len2;
// Since setlength() is somewhat costly, we'll do it less
// often than you would think.
if length( dest ) < newlength then
setlength( dest, newlength + quantum );
move( s1^, dest[len0 + 1], len1 );
move( s2^, dest[len0 + 1 + len1], len2 );
exit( newlength );
end;
procedure replace_matches( const target: pchar; const repl: ansistring;
const str: ansistring; var dest: ansistring );
var
engine : tRegexprEngine;
starti, index, size, truelength : longint;
pstart : pchar;
begin
GenerateRegExprEngine( target, [], engine);
dest := ''; truelength := 0;
starti := 1;
pstart := pchar(str);
while starti <= length(str) do
if RegExprPos(engine, pstart, index, size ) then
begin
truelength := append2(
dest, truelength, @str[starti], index, @repl[1], length(repl) );
inc(pstart, index+size);
inc(starti, index+size);
end
else
break;
DestroyRegExprEngine( engine );
setlength( dest, truelength );
dest := dest + Copy( str, starti, length(str)-starti+1);
end;
function count_matches( target: pchar; const str: ansistring ): longint;
var
engine : tRegexprEngine;
pstart : pchar;
starti,
count, index, size : longint;
begin
GenerateRegExprEngine( target, [ref_caseinsensitive], engine);
count := 0;
pstart := pchar(str);
starti := 1;
while starti <= length(str) do
if RegExprPos(engine, pstart, index, size ) then
begin
inc(count);
inc(pstart,index+size);
inc(starti,index+size);
end
else
break;
DestroyRegExprEngine( engine );
exit(count)
end;
const
patterns : array[1..9] of pchar =
(
'(agggtaaa)|(tttaccct)',
'([cgt]gggtaaa)|(tttaccc[acg])',
'(a[act]ggtaaa)|(tttacc[agt]t)',
'(ag[act]gtaaa)|(tttac[agt]ct)',
'(agg[act]taaa)|(ttta[agt]cct)',
'(aggg[acg]aaa)|(ttt[cgt]ccct)',
'(agggt[cgt]aa)|(tt[acg]accct)',
'(agggta[cgt]a)|(t[acg]taccct)',
'(agggtaa[cgt])|([acg]ttaccct)'
);
replacements : array[1..11,1..2] of pchar =
(
('B', '(c|g|t)'), ('D', '(a|g|t)'), ('H', '(a|c|t)'), ('K', '(g|t)'),
('M', '(a|c)'), ('N', '(a|c|g|t)'), ('R', '(a|g)'), ('S', '(c|t)'),
('V', '(a|c|g)'), ('W', '(a|t)'), ('Y', '(c|t)')
);
var
pattern : pchar;
sequence, new_seq : ansiString;
line, tmp: string[255];
letter, repl : pchar;
i, count, init_length, clean_length, reps : longint;
inbuf : array[0..64*1024] of char;
begin
settextbuf(input,inbuf);
sequence := '';
init_length := 0;
while not eof do
begin
readln( line );
init_length += length( line ) + 1;
if line[1] <> '>' then
sequence := sequence + line;
end;
clean_length := length(sequence);
for i := low(patterns) to high(patterns) do
begin
pattern := patterns[i];
count := count_matches( pattern, sequence );
tmp := delChars( delChars(pattern,'('), ')' );
writeln( tmp, ' ', count);
end;
// Replace.
for i := low(replacements) to high(replacements) do
begin
letter := replacements[i][1]; repl := replacements[i][2];
replace_matches(letter,repl,sequence,new_seq);
sequence := new_seq;
end;
writeln;
writeln( init_length );
writeln( clean_length );
writeln( length(sequence) );
end.
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
More information about the fpc-pascal
mailing list