|23/08/2008, 11:12 AM||#1|
Join Date: Apr 2006
[HowTo] Fast command processor: DCMD (0.3 Ready)
jtp10181 showed in a topic that there are still person which use strtok to get a nice looking command processor. But it is slow and it won't work like it should if you have more commands behind each other.
Thatswhy I decided to show a way (dcmd) which also looks gentle but is much faster then strtok only and in my opinion better organized.
How To: [iurl=http://forum.sa-mp.com/index.php?topic=70925.msg461044#msg461044]Add Multiple Parameters[/iurl]
This way, is often used (I am not using params now, because that would decrease strtok-method again) (Remember it hasn't even protection if somebody uses commands like /meso or something like this!)
Our implementation will look like this (it has protection like /meso is not allowed, but /me hello is.):
I tested (calling 1000000 times) the implementations above, without print-stuff (this would make lag, which isn't called by the function itself!) and had this results:
63 Seconds for first implementation.
18 Seconds for my implementation.
Here is my testcase:
If I add two more /commands (for example /give and /me in both implementation) the results where: 19 seconds for mine, and already 70 seconds for the first implementation.
You can see that it is much faster without extra vars and stuff.
Why is it so much faster, even in this small example?
The magic thing is using procedures and good organized if-statement, instead of lots of strtok's.
#1 (strcmp(cmdtext, "/me", true, 2+1) == 0)
If #1 is true, the beginning is /me, but this doesn't filter stuff like /meso yet.
Now #2.1 or #2.2 must be true. The #2.1 is true, if "/me" is the whole string, and dcmd_me returns true. The #2.2 returns true, if "/me " has a space after /me, so its for example "/me hello." and transfers the array beginning from the "hello" (therefor we have cmdtext[2+2]). The cool thing is, that dcmd_me won't be executed if already #1 isn't true, so it doesn't take extra space/speed.
[Hint] This topic got lost, when updating/converting the forums, so you may find an archive at webarchive.org
|Thread||Thread Starter||Forum||Replies||Last Post|
|[Include] [INC] zcmd 0.3.1 | Fast & Simple Command Processor (updated 30/10/2009)||Zeex||Includes||536||14/09/2014 01:29 AM|
|[HowTo] Using dudb 2.4 to save money/stats/whatever (0.2#Ready)||DracoBlue||Help Archive||748||17/09/2010 10:14 AM|
|[HR/SR:FS] Fast rcon command processor: RCMD (modified DCMD)||Correlli||Archive||0||16/11/2009 10:20 PM|
|fast command processor||[mad]MLK||Help Archive||1||22/08/2009 01:32 AM|
|Fast rcon command processor: RCMD (modified DCMD)||Correlli||Filterscripts||7||01/08/2009 12:02 PM|