Fastest/smallest search+replace
Fastest/smallest search+replace
- Subject: Fastest/smallest search+replace
- From: Stefan Lange-Hegermann <email@hidden>
- Date: Mon, 14 May 2001 17:43:51 +0200
Hi!
First I'd like to introduce myself.
My name is Stefan and I am studying Information Technology at the
university of Dortmund in Germany. I am currently learning Cocoa by
coding on my application manThor, which some of you might know ;) I've
been addicted to Macs since I saw a PowerBook 140 in the early days. I
learned programming in GFA Basic on my beloved Atari ST and one awesome
Schneider CPC. Today I am pretty fluid in most of the more modern
languages including Java, C++ and PHP :p.
Now to my problem:
I am looking for the most efficient search and replace algorithm for
strings. It does not have to be strictly Cocoa, since I can convert my
NSStrings to cstrings easily. The problem is, that I need to replace the
\n s in a string, which is at least 150 pages long when printed out. All
the attempts I tried are somewhat slow and I want it to be (or appear)
instant.
I figured there would be no easier way than looping through every
character and looking for it's value (in plain c). In Cocoa I tried to
split an NSString and rejoin it with the replacement character in
between, but that's still very slow with the massive string I passed
(and as long as there is no better way than the loop it should do pretty
much the same I do in c).
This seems to be the fastest way in c:
int a=1;
char * Test="Hello\nYou\nLittle\nTest ;)\n";
char curchar;
printf(Test);
for (curchar=Test[0];curchar!=NULL;curchar=Test[a++]) {
if (curchar=='\n') {
printf("*line break*\n");
} else {
printf("%c",curchar);
}
}
return 0;
does anyone have an Idea how to make it even faster?
Thanks a lot
Stefan
/**********************************************************************
* ||| * Stefan Lange-Hegermann * email@hidden *
* (.) (.) *
http://www.blackmac.de/ *
http://www.der-klarmacher.de/ *
* ( _ ) * He who talks does not know, he who knows does not talk! *
**********************************************************************/