if (!strcmp(str, "first")) { // Do first code. } else if (!strcmp(str, "second")) { // Do second code. } // etc...
switch (YHash(str)) { case _H<first>: { // Do first code. } case _H<second>: { // Do second code. } }
YHash(str[], bool:sensitive = true, e_HASH_TYPE:type = bernstein);
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_
printf("The hash of \"hello\" is: %d", _H<hello>);
switch (YHash(cmd, false)) { case _I<ban>: // Do the "/ban" command. case _I<kick>: // Do the "/kick" command. case _I<spawn>: // Do the "/spawn" command. }
switch (YHash(cmd, true, fnv1)) { case _H@f<ban>: // Do the "/ban" command. case _H@f<kick>: // Do the "/kick" command. case _H@f<spawn>: // Do the "/spawn" command. }
case _I<color22>:
case _I(c,o,l,o,r,2,2):
Comparing a string 1000 times in a row using switch(). Loop finished after: 6 milliseconds. .. Comparing a string 1000 times in a row using strcmp. Loop finished after: 2 milliseconds.
Comparing a string 100,000 times in a row using switch(). Loop finished after: 330 milliseconds. .. Comparing a string 100,000 times in a row using strcmp. Loop finished after: 134 milliseconds.
Comparing a string 500,000 times in a row using switch(). Loop finished after: 1636 milliseconds. .. Comparing a string 500,000 times in a row using strcmp. Loop finished after: 664 milliseconds.
#include <a_samp>
#include <YSI\y_stringhash>
main() {
printf("Comparing a string 1000 times in a row using switch().");
new count = GetTickCount();
for(new i = 0; i < 1001; i++) {
new string[144];
format(string, 144, "something random (%i)", random(10000));
switch(YHash(string)) {
case _H<dfsd>: {
print("waow");
}
case _H<fghfggfhgf>: {
print("waow");
}
case _H<fghfsdfsdfggfhgf>: {
print("waow");
}
}
}
printf("Loop finished after: %i milliseconds.", GetTickCount()-count);
print("..");
printf("Comparing a string 1000 times in a row using strcmp.");
count = GetTickCount();
for(new i = 0; i < 1001; i++) {
new string[144];
format(string, 144, "something random (%i)", random(10000));
if(!strcmp(string, "dfsd")) {
print("waow");
}
else if(!strcmp(string, "fghfggfhgf")) {
print("waow");
}
else if(!strcmp(string, "fghfsdfsdfggfhgf")) {
print("waow");
}
}
printf("Loop finished after: %i milliseconds.", GetTickCount()-count);
}
main()
{
new count,
MAX_LOOP = 100000,
something;
print("Starting...");
count = GetTickCount();
for(new i; i < MAX_LOOP; i++)
{
switch(YHash("Arrestation"))
{
case _H<Entreprise> : print("Oki");
case _H<Restaurent> : print("Oki");
case _H<Bar> : print("Oki");
case _H<Vehicule> : print("Oki");
case _H<Bar1> : print("Oki");
case _H<Vehicule1> : print("Oki");
case _H<Bar2> : print("Oki");
case _H<Vehicule2> : print("Oki");
case _H<Bar3> : print("Oki");
case _H<Vehicule3> : print("Oki");
case _H<Bar4> : print("Oki");
case _H<Vehicule4> : print("Oki");
case _H<Bar5> : print("Oki");
case _H<Vehicule5> : print("Oki");
case _H<Arrestation> : something++;
}
}
count = GetTickCount() - count;
printf("[1] Finished in %ims", count);
count = GetTickCount();
for(new i; i < MAX_LOOP; i++)
{
if(!strcmp("some_type", "Entreprise"))
print("Oki");
else if(!strcmp("some_type", "Restaurent"))
print("Oki");
else if(!strcmp("some_type", "Bar"))
print("Oki");
else if(!strcmp("some_type", "Vehicule"))
print("Oki");
else if(!strcmp("some_type", "Bar"))
print("Oki");
else if(!strcmp("some_type", "Vehicule"))
print("Oki");
else if(!strcmp("some_type", "Bar"))
print("Oki");
else if(!strcmp("some_type", "Vehicule"))
print("Oki");
else if(!strcmp("some_type", "Bar"))
print("Oki");
else if(!strcmp("some_type", "Vehicule"))
print("Oki");
else if(!strcmp("some_type", "Bar"))
print("Oki");
else if(!strcmp("some_type", "Vehicule"))
print("Oki");
else if(!strcmp("Arrestation", "Arrestation"))
something++;
}
count = GetTickCount() - count;
printf("[2] Finished in %ims", count);
}
[21:00:16] [1] Finished in 65ms [21:00:16] [2] Finished in 71ms
****** is right but I don't understand the purpose of comparing a lot of strings with this way
Код:
[21:00:16] [1] Finished in 65ms [21:00:16] [2] Finished in 71ms |
What's the "YHash" function string limit? Seems like I can't go over 16 characters. I am trying to do: 'case _H<COLT45_CROUCHFIRE>:'
|
Important note: If your compiler hangs, consider using the old style on longer strings. This is not well suited to hashing long strings. For example:
Код:
case _I<color22>: Код:
case _I(c,o,l,o,r,2,2): |
I think there is some confusion here - YHash and _H are not the same thing. _H is a compile-time macro that converts constant strings in to constant numbers. The results are not cached in an array as you suggested because there is no need, they are literally just numbers in the compiled code, no strings or hashings are present at run-time. YHash is the run-time equivalent, it takes unknown strings and hashes them while the server is running, for comparison with the pre-computed hashes of _H.
As for the length limits, I suspect you are not actually asking about YHash, which has no length limit for the reasons I just said, but _H, whose length limits are dictated by the compiler's line length limits. If you aren't already using Zeex's compiler, you should be, so try that on instead - it has a vastly increased line length limit. If you are, you could just be trying to has a very complex string, in which case I point you to this quote: The same applies to _H<> and _H(). The () syntax was the original version before I figured out how to parse arbitrary strings, but I kept it because the macros were MUCH simpler and can handle vastly longer strings. That second syntax is also the only one that supports spaces - you can't have them in the <> syntax. Also, adding two hashes probably will not give the same result as hashing the concatenation of the two strings (in fact I know it won't - that would totally break the security of hashes, though these are not cryptographically secure ones, just basic comparison ones). |