• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
RE: Fastest/smallest search+replace
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Fastest/smallest search+replace


  • Subject: RE: Fastest/smallest search+replace
  • From: Kevin Murray <email@hidden>
  • Date: Mon, 14 May 2001 10:34:54 -0700

First, get away from printf(). printf() is the slowest possible way to
output anything in any computer language ever invented (except using PRINT$
on a Timex Sinclair) :) printf() has a tremendous amount of overhead while
searching, processing, and replacing embedded print commands (like "%c").
Use functions like puts, putc, putchar, etc to greatly speed up your C code
since they just blast raw data into an output buffer then return immediately
(unless the buffer is full).

Look at something more like:
char* Test = "Hello\nYou\nLittle\nTest ;)\n";
char* cp;

puts(Test);

for (cp = Test; *cp != '\0'; ++cp) {
#if 1
// if you only want to replace with one character, use
putchar(*cp == '\n' ? '\r' : *cp);
#else
// if you really want to replace a character with a string, use
if (*cp == '\n') puts("*line break*\n");
else putchar(*cp);
#endif
}
return 0;

The fastest way if you can afford to destroy the buffer and want single
character substitution is probably
char* Test = "Hello\nYou\nLittle\nTest ;)\n";
char* cp;

for (cp = Test; *cp != '\0'; ++cp) if (*cp == '\n') *cp = '\r';
puts(Test);

Someone more familiar with Cocoa may know a trick or two specific to the
framework.

A couple of other nit-picky things: I always avoid arrays in favor of
pointers, but most modern compilers will optimize that for you (this wasn't
always the case). Also, you are comparing a character (curchar) with a
pointer (NULL is usually defined as something like "((void*)0L)"), this
won't hurt anything since the compiler will clean it up for you, but it is a
bad habit to get into. "curchar != '\0'" is generally prefered.

(Yes, I also comment my code and explicitly compare pointers with "if (ptr
!= NULL)" too. I know this flies in the face of C tradition, but I'm just
too darn anal.)

> -----Original Message-----
> From: Stefan Lange-Hegermann [mailto:email@hidden]
> Sent: Monday, May 14, 2001 9:44 AM
> To: email@hidden
> Subject: Fastest/smallest search+replace
>
>
> 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! *
>
> **************************************************************
> ********/
> _______________________________________________
> cocoa-dev mailing list
> email@hidden
> http://www.lists.apple.com/mailman/listinfo/cocoa-dev


  • Prev by Date: Re: Best way to authenticate ?..
  • Next by Date: Re: Best way to authenticate ?..
  • Previous by thread: Re: Fastest/smallest search+replace
  • Next by thread: Re: Fastest/smallest search+replace
  • Index(es):
    • Date
    • Thread