replace
int str_rev(char* s)
{
   char* s_end;
   char tmp;

   if (!s) return -1;

   s_end = s + strlen(s) - 1;

   for (; s < s_end; s++, s_end--)
   {
      tmp = *s;
      *s = *s_end;
      *s_end = tmp;
   }

   return 0;
}

with something like this...

int str_rev(char*s)
{
   int length = strlen(s);
   int pos = 0;
   while(length > 0)
   {
     s[length--] = s[pos++];
   }
}


Untested but I would think it should be a little bit faster.


On Thu, May 2, 2013 at 8:25 AM, Sasha Pachev <[email protected]> wrote:

> OK, the gauntlet has been thrown.
>
> http://asksasha.com/strrev.c
>
> coded in 25 minutes. When compiled with -O3 gives me this on my laptop:
>
> bash-4.1$ time ./strrev /usr/share/dict/words > /dev/null
>
> real    0m0.055s
> user    0m0.053s
> sys    0m0.002s
>
> my /usr/share/dict/words can be downloaded from here:
>
> http://asksasha.com/words
>
> when compiled with default flags (with gcc 4.4.5) I got 0.09 s of real time
>
> Levi, beat this in Haskell. Barry, Dale, beat this in Java. Anybody
> else, beat this in your language of choice including C - I do not
> claim that my implementation is the best. Post your development time.
> If you think the rules are not fair, feel free to protest.
>
> For those who cannot read C,  the program reads the strings from the
> file given by the first argument delimited by \n, reverses each and
> prints the result to stdout.
>
> To compile it:
>
> gcc -O3 -o strrev strrev.c
>
> Yes, and for those who are too smart for their own good - your program
> must dump out the correct result when you print to stdout instead of
> redirecting to /dev/null, and it must work correctly on any file that
> has lines shorter than 512 bytes. If the line is longer than that it
> can truncate the string, but must not crash.
>
>
> --
> Sasha Pachev
>
> Fast Running Blog.
> http://fastrunningblog.com
> Run. Blog. Improve. Repeat.
>
> /*
> PLUG: http://plug.org, #utah on irc.freenode.net
> Unsubscribe: http://plug.org/mailman/options/plug
> Don't fear the penguin.
> */
>

/*
PLUG: http://plug.org, #utah on irc.freenode.net
Unsubscribe: http://plug.org/mailman/options/plug
Don't fear the penguin.
*/

Reply via email to