: : : : : Maximum thread limit...
: : : : :
: : : : : That's how strcmp worked. I had no idea.
: : : : :
: : : : : My way is compiled to just one instruction CMPS, wich is just 5 cycles on a Pentium.
: : : : : The strcmp func could, as I can see it, only be optimised to a loop, (since there is two comparisons to be made).
: : : : :
: : : : : I don't know how to say this but something like this:
: : : : : The case for it to match is small, one in 26 or similar.
: : : : : Since strcmp is slower than my way, and the probability for a match is low, the extra comparison for a match will not differ much.
: : : : :
: : : : : My way: 25 times cmps and one strcmp
: : : : : only strcmp: 26 times strcmp
: : : :
: : : : I agree with the fact that a call to strcmp() plus the extra comparison require a little more time to execute than your single comparison. However, unless you compare thousands of strings, do you think you'll notice the difference between the two methods?
: : : :
: : : : Steph
: : : :
: : : That's what I'm planning to do, compare thousands of strings.
: : : I'm making a compiler.
: : :
: : I have performed some tests in order to show the differences between strcmp() and your method.
: : The program is:
: :
: : #include <windows.h>
: : #include <stdio.h>
: : #include <string.h>
: : #include <stdlib.h>
: :
: :
: : int main(int argc,char **argv)
: : {
: : char s[]="a";
: : char t[sizeof s];
: : DWORD ticks,ticks0;
: : int i,max,temp;
: :
: : if (argc!=2) return 1;
: : max=atoi(argv[1]);
: : strcpy(t,"b");
: : ticks0=GetTickCount();
: : for (i=0;i<max;++i)
: : if (!strcmp(s,t)) temp=1;
: : ticks=GetTickCount()-ticks0;
: : printf("Time elapsed with strcmp() for %d tests: %u ms\n",max,ticks);
: : ticks0=GetTickCount();
: : for (i=0;i<max;++i)
: : if (*s==*t) temp=1;
: : ticks=GetTickCount()-ticks0;
: : printf("Time elapsed without strcmp() for %d tests: %u ms\n",max,ticks);
: : printf("%d\n",temp);
: : return 0;
: : }
: :
: :
: : The first character is different in each string, so strcmp() will only compare the first two characters and stops.
: : Here are the results with a Pentium4 at 2.8GHz:
: :
: :
: : Z:\Data\C\Test>test 1000000
: : Time elapsed with strcmp() for 1000000 tests: 16 ms
: : Time elapsed without strcmp() for 1000000 tests: 0 ms
: : 0
: :
: : Z:\Data\C\Test>test 10000000
: : Time elapsed with strcmp() for 10000000 tests: 78 ms
: : Time elapsed without strcmp() for 10000000 tests: 16 ms
: : 0
: :
: : Z:\Data\C\Test>test 1000000000
: : Time elapsed with strcmp() for 1000000000 tests: 7078 ms
: : Time elapsed without strcmp() for 1000000000 tests: 1454 ms
: : 0
: :
: :
: : As you can see, even with one billion comparisons between the two strings, the difference is not so huge: 7s instead of 1.4s.
: :
: : Steph
: :
: 5 times faster! Thats enough for me.
: Think if booting was five times faster.
:
: Thanks for the comparison.
:
: For the post:
: I'm using linux now, but this could easily be ported to windows, or anything I think. I hope my code is ansi, and if it is, it could in fact be ported to all computers.
:
Yes, 5 times faster. But the difference become noticeable only for a huge number of comparisons (it's not so common to have a source file containing 1 billion characters), which all fail. In fact, the gain will be less than 5. Furthermore, source files contain many blanks with which you won't call strcmp(). That's why I think it will be rare for your compiler to make so many string comparisons.
Are you writing a compiler in C? To compile C code? Anyway, I think there will be a part which depends on the OS. It cannot be 100% ANSI, can it?
Steph