[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Another implementation for strrep
From: |
Thomas Treichl |
Subject: |
Another implementation for strrep |
Date: |
Sat, 10 Mar 2007 00:16:03 +0100 |
User-agent: |
Thunderbird 1.5.0.10 (Macintosh/20070221) |
Hi,
I did some tests with the strrep function and I think that I was able to
implement a very short and very fast code that does depend very little on string
sizes and the number of strings that have to be replaced:
octave:1> tic, for CNT = 1:10000, strrep ("This is a % test string", ...
> "%", "AAAA"); end, toc
Elapsed time is 10.135750 seconds.
octave:2> tic, for CNT = 1:10000, _strrep_ ("This is a % test string", ...
> "%", "AAAA"); end, toc
Elapsed time is 4.794319 seconds.
octave:3> tic, for CNT = 1:10000, strrep ("This is a %placeholder%", ...
> "%placeholder%", "AAAA"); end, toc
Elapsed time is 14.827904 seconds.
octave:4> tic, for CNT = 1:10000, _strrep_ ("This is a %placeholder%", ...
> "%placeholder%", "AAAA"); end, toc
Elapsed time is 4.650185 seconds.
octave:5> tic, for CNT=1:10000, strrep ("%placeholder% AA %placeholder%", ...
> "%placeholder%", "placeholder"); end, toc
Elapsed time is 16.657799 seconds.
octave:6> tic, for CNT=1:10000,_strrep_("%placeholder% AA %placeholder%", ...
> "%placeholder%", "placeholder"); end, toc
Elapsed time is 5.782204 seconds.
and I also think that this implementation is exactly doing the same then today's
implementation of octave:
octave:9> _strrep_ ("%A %A %", "%", "A")
ans = AA AA A
octave:10> _strrep_ ("%A %A %", "", "A")
ans = %A %A %
octave:11> _strrep_ ("%A %A %", "A", "")
ans = % % %
octave:12> _strrep_ ("%A %A %", "", "")
ans = %A %A %
octave:13> _strrep_ ("", "%A", "%B")
ans =
Code file attached.
Bye, Thomas
function t = _strrep_ (s, x, y)
if (nargin != 3), print_usage (); endif
if (! (ischar (s) && ischar (x) && ischar (y)))
error ("strrep: all arguments must be strings");
endif
[a, b] = regexp (s, x);
b = [1, b+1]; a = [a-1, (length (s))];
t = s(b(1):a(1));
for cnt = 2:length (a)
t = [t, y, s(b(cnt):a(cnt))];
end
endfunction
- Another implementation for strrep,
Thomas Treichl <=