2

strcmp, at least using g++, has many optimitzations for many architectures. In my pc, a Core2Duo E8400, strcmp is two times faster than use a straigforward implementation.

My question if it exists some library that provides a function that compares two "reverse strings". A reverse string char *s1 starts in s1 and ends at some s1-n such that s1-n == '\0' (where n >= 0 and for all 0 <= n' < n, s1-n' != '\0').

Of course, the requirements are that this function must be so efficient and portable as strcmp.

edit: I just need know if two strings are equals (so i do not need know which are greater. Then the same optimitzations for strcmp, in principle, will work fine for a reverse strings).

Joe
  • 41,484
  • 20
  • 104
  • 125
user1476999
  • 177
  • 1
  • 8
  • 2
    I'm willing to bet quite a bit that the answer is NO. Zero-terminated strings are common. Strings that *start* with `\0` are virtually unheard of. So, who writes a library for such unusual things? – MSalters Sep 24 '12 at 11:15
  • 1
    This is one of the weirdest requirements I've seen! :) – unwind Sep 24 '12 at 11:18
  • I'm sure it can be done, but can't seem to find such an implementation. Good luck. – Bob Jarvis - Слава Україні Sep 24 '12 at 11:24
  • 1
    Why can't you take the existing source code for `strcmp()` and decrement the two input pointers, instead of increment? http://fossies.org/dox/glibc-2.16.0/strcmp_8c_source.html – Alex Reynolds Sep 24 '12 at 11:35
  • @AlexReynolds this is not the optimized version. Reading the executable with objdump, the strcmp (with -O3 in my enviroment) I can see that take profit from vector extensions (in fact, first do some check for know which are available). – user1476999 Sep 24 '12 at 11:52
  • @MSalters suppose you need compare a lot of strings, and you know that with high probability they have a long commom prefix. Then the best strategy is start comparing from the end to the start. I think that is not an unusual problem. – user1476999 Sep 24 '12 at 11:56
  • 1
    @user1476999: Oh, certainly not. But still you'd store those strings with `\0` LAST. I.e. `X-xiferp\0, Y-xiferp\0` etc. And then you can use the regular `strcmp`. – MSalters Sep 24 '12 at 11:59
  • Or, if you're allowed to change the representation, perhaps you could store pointers to the start *and* end of the string (equivalently, start and length). Saves having to actually reverse it as in MSalter's representation. Then write a reverse `memcmp`. The compiler might do a better job of auto-vectorizing that than your reverse `strcmp` (I'm assuming it failed on your reverse-strcmp). – Steve Jessop Sep 24 '12 at 12:29

2 Answers2

1

From what i know most optimizations on strcmp for specific architectures use the trick of comparing multiple bytes together such as casting into a long with aligned pointers and the like. These sort of optimizations are not probable to work with comparing a reverse of string i.e. the reverse won't form the same for e.g. long and reversing it yet again to generate the same number would result in efficiency loss. So the answer in my opinion is no.

fkl
  • 5,412
  • 4
  • 28
  • 68
0

If I had your problem, a reverse strcmp, I would just write it. Don't worry about the speed, for a couple reasons.

First, programs having strcmp as a significant "bottleneck" are rare.

Second, strcmp stops on the first unequal character, so unless strings are long and nearly equal, it's likely on average to compare very few characters. Any program that spends a large fraction of its time comparing strings that are equal or nearly so is begging for some performance tuning.

Mike Dunlavey
  • 40,059
  • 14
  • 91
  • 135
  • I already wrote a reverse strcmp. I've measured the performacne of my program, and if I can use a function optimized as strcmp I will reduce about 5% of CPU usage, not more, but without negative counterparts. – user1476999 Sep 24 '12 at 12:21
  • From where I sit, [*5% is pretty small*](http://scicomp.stackexchange.com/a/2719/1262). Chances are there is something bigger to fix. – Mike Dunlavey Sep 24 '12 at 12:38