Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Efficiency! (https://forums.graalonline.com/forums/showthread.php?t=71288)

Twinny 01-06-2007 03:59 PM

Efficiency!
 
Debate between Kuji and I :Which method is more efficient? My example first then his.

PHP Code:

function ChatBar.onAction()
{
  if (
ChatBar.text != "")
  {
    
temp.toks ChatBar.text.tokenize();
    
temp.args temp.toks;
    
temp.args.delete(0);
    switch(
temp.toks[0])
    {
    
/* Clientside Commands */
      
case "/tailor":
        
Tailor.showTailor();
        return;
      case 
"/reconnect":
        
serverwarp("Theed");
        return;

        ... 

        
/* Serverside Commands */
        
case "mass":
        case 
"admin":
        case 
"test":
          
          ...
          
          
triggerserver("gui"this.nametemp.toks[0], temp.args);
          return;
    }
  }


Clientside commands would be done before the triggerserver while all serverside commands will be triggered.

Compared to:

PHP Code:

function ChatBar.onAction()
{
  if (
ChatBar.text.starts("/"))
  {
    
temp.toks ChatBar.text.tokenize();
    
    
temp.args temp.toks;
    
temp.args.delete(0);
    
    if (
temp.toks[0].substring(1in this.clientcmd)
    {
      
onDoClientCommand(temp.toks[0].substring(1), temp.args);
    }
      else
    {
      
triggerserver("gui"this.nametemp.toks[0], temp.args);
    }
    
    
ChatBar.text "";
  }
}

function 
onDoClientCommand(actionargs)
{
  switch (
action)
  {
    case 
"reconnect":
    {
      
serverwarp("Theed");
    
      return 
true;
    }
  } 

The battle here is between switches and if/else and index() ^^. My method may look longer now but his would have alot of clientside commands as well.

Of course a possibility might be to switch the clientside commands. If not there, check if the command is in an array of serverside commands: if so, serverside. Else displays an error message :) This may server only make the script look nicer and may have no benefits in efficiency

xXziroXx 01-06-2007 05:54 PM

I like Example 1 more, and Id say its the most efficient way to do it.

Chompy 01-06-2007 06:25 PM

I think example 1 looks more orginazed, and most efficient.

Tolnaftate2004 01-06-2007 08:55 PM

In all technicality, example 2 is the most efficient in that it is not evaluating everything that is passed from ChatBar. However, it could still be improved, such as calling temp.toks[0].substring(1) only once and using a variable the multiple times it is needed. In all actuality, the substring isn't necessary.

Another note is I don't think example 2 is working like it is supposed to be, but that is merely speculation. If the serverside commands do not start with "/", they are not being evaluated in that example, and if they do begin with "/", then the "/" is being passed along to the server. Doesn't make much of a difference in script performance, but I must remind that removing the "/" from the command on the serverside as well is silly.

If example 1 could be tweaked to filter out non-commands from ChatBar, I would go with that one.

Crono 01-06-2007 09:08 PM

well, which one takes the least CPU usage?

yeah im a noob whatever

excaliber7388 01-06-2007 09:54 PM

Not that bad of a question though, I've made scripts that increased actual cu usage.

Twinny 01-07-2007 04:30 AM

Quote:

Originally Posted by Tolnaftate2004 (Post 1261876)
In all technicality, example 2 is the most efficient in that it is not evaluating everything that is passed from ChatBar.

When you think about it, it's very similar to if/else. Starts at the first case and checks. If it's the case, it performs it then ends the whole script. If not, goes down. Once it meets it's case, doesn't have to evaluate any others.For the serverside part, it's just reading the case "example": but i'm not even sure if it's doing that =p

Rapidwolve 01-07-2007 05:05 AM

For more options, I would go for #1, for less #2

Tolnaftate2004 01-07-2007 05:14 AM

Quote:

Originally Posted by Twinny (Post 1262019)
When you think about it, it's very similar to if/else. Starts at the first case and checks. If it's the case, it performs it then ends the whole script. If not, goes down. Once it meets it's case, doesn't have to evaluate any others.For the serverside part, it's just reading the case "example": but i'm not even sure if it's doing that =p

I know what switch does, and I would argue that it's not particularly similar to if/else.

What I was getting at is that say ChatBar calls onAction. The second one is weeding out right off the bat all parameters from ChatBar that could never be the case. Option one loops through each of the cases for everything passed from ChatBar, regardless of if it might be a case. That is not particularly desired; it runs up CPU usage.

Twinny 01-07-2007 05:24 AM

Thus the major thing I've been wondering about. I was thinking cases would be similar to checking an array for a match since it's only comparing the case "blah": line. I think it would be very close in terms of efficiency. You could argue that once a match is found serverside, it has to go through all cases below it but does it actually process cases since it has found it's match already?

Also, my method doesn't send incorrect commands serverside >_<. Kuji would need another .index() for this thus less efficiency clientside ^^ .

Tolnaftate2004 01-07-2007 05:32 AM

Quote:

Originally Posted by Twinny (Post 1262064)
Thus the major thing I've been wondering about. I was thinking cases would be similar to checking an array for a match since it's only comparing the case "blah": line. I think it would be very close in terms of efficiency. You could argue that once a match is found serverside, it has to go through all cases below it but does it actually process cases since it has found it's match already?

Also, my method doesn't send incorrect commands serverside >_<. Kuji would need another .index() for this thus less efficiency clientside ^^ .

No, it stops processing cases after it has found a match and runs the code held therein regardless of case (that is, without breaking the loop). I agree in that they are close in efficiency, however from the post, #2 is still more efficient. And as I stated in my first post, simple changes could make both even better, eventually making #1 the more efficient.

An array would do the trick.

Twinny 01-07-2007 05:53 AM

Maybe this?

PHP Code:

function ChatBar.onAction()
{
  if (
ChatBar.text.starts("/"))
  {
    
temp.toks ChatBar.text.tokenize();
    
    
temp.args temp.toks;
    
temp.args.delete(0);
    switch(
temp.toks[0])
    {
       case 
"client1":
         
//stuff
         
return;
       case 
"client2":
         
//stuff
         
return;
       case 
"client3":
         
//stuff
         
return;
    }
    
triggerserver("gui"this.nametemp.toks[0], temp.args);
  }


Kuji made the point that there could be a fair amount of serverside commands so a case "": for all of them could get tedious...

Gambet 01-07-2007 06:07 AM

Cases are a lot cleaner and easier to modify, especially when you want multiple cases doing the same thing, without the need of calling the same function multiple times.

Tolnaftate2004 01-07-2007 06:35 AM

Quote:

Originally Posted by Twinny (Post 1262105)
PHP Code:

switch(temp.toks[0])
{
  
//blah
}
triggerserver("gui"this.nametemp.toks[0], temp.args); 


That, or you could do

PHP Code:

switch(temp.toks[0]) {
  
// blah
  
default:
    
triggerserver(so-and-so);
    return;



Angel_Light 01-07-2007 07:54 AM

Combination of the two XD


All times are GMT +2. The time now is 07:37 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.