[Tutorial] How to use switch statement on strings for faster string comparisons - Printable Version
+- SA-MP Forums Archive (
https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (
https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (
https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Tutorials (
https://sampforum.blast.hk/forumdisplay.php?fid=70)
+---- Thread: [Tutorial] How to use switch statement on strings for faster string comparisons (
/showthread.php?tid=327297)
How to use switch statement on strings for faster string comparisons - T0pAz - 20.03.2012
Introduction
In this tutorial, I will show you how to use switch statement for strings.
Getting the necessary things
All you need to have is this library only. Credits to ****** for creating the useful library. Make sure to include the library on your script.
How will it work
Well, it hashes the string into numbers which can be easily compared afterwards. Sounds easy eh.
How to script it
It's as easy as comparing numeric values.
pawn Code:
StrHash(
strz[], // String to hash.
bool:sensitive = true, // Case Sensitivity.
e_HASH_TYPE:type = bernstein // The type of hash to use as default to bernstein.
);
Let me show you an example code with guidance.
pawn Code:
switch(StrHash(cmd)) // Hashing the string into numberic value and switiching it
{
// _I - This is the macro to generate a compile time constant hash as stated on the y_stringhash official thread.
case _I<help>: // If command is help
{
// Code to execute on here
}
case _I<commands>: // If command is commands
{
// Code to execute on here
}
case _I<kill>: // If command is kill
{
// Code to execute on here
}
}
Very easy right. You can use this on several places even though you have a command processor and it's the fastest string comparisons.
If you have any question, please post it below.
Re: How to use switch statement on strings for faster string comparisons -
new121 - 20.03.2012
Very nice wasn't even aware you could do this at all
Re: How to use switch statement on strings for faster string comparisons - T0pAz - 26.03.2012
Quote:
Originally Posted by new121
Very nice wasn't even aware you could do this at all
|
Me too. Thanks to ****** for that library.
Re: How to use switch statement on strings for faster string comparisons - suhrab_mujeeb - 26.03.2012
But the question is how fast as compared to strcmp?
Re: How to use switch statement on strings for faster string comparisons - T0pAz - 26.03.2012
Quote:
Originally Posted by suhrab_mujeeb
But the question is how fast as compared to strcmp?
|
Test it out yourself.