mIRC Home    About    Download    Register    News    Help

Print Thread
#139677 17/01/06 02:31 PM
Joined: Jul 2004
Posts: 59
S
synth7 Offline OP
Babel fish
OP Offline
Babel fish
S
Joined: Jul 2004
Posts: 59
These are just examples, but I'm wondering, which one is the fastest and best to use? I'm sure if there is a difference it's so insignificant it won't make much of a difference but I'd like to know nonetheless.

1.
Code:
    if ($1 == test) { msg # test }


2.
Code:
    if ($1 == test) msg # test 


3. (someone mentioned this to me but it doesn't seem like the best programming etiquette
Code:
    if $1 == test { msg # test }


4. Or just no () {} at all?

Thanks.



#139678 17/01/06 03:31 PM
Joined: Oct 2004
Posts: 72
C
Babel fish
Offline
Babel fish
C
Joined: Oct 2004
Posts: 72
According to what I have read: using ()'s and {} allows faster parsing. If they aren't present, mIRC has to do a bit of guess work as to where an operator ends and an argument starts etc.

#139679 17/01/06 03:44 PM
Joined: Feb 2004
Posts: 2,019
Hoopy frood
Offline
Hoopy frood
Joined: Feb 2004
Posts: 2,019
Which style you choose is up to you, as long as you don't pick the last one, because that is very bad programming practice, and is basically an invitation to parsing errors.

Apart from that, there are certain rules you must take into account:

if (condition) command | command
--> does not work as intended.

if (condition) { command | command }
--> works as intended

For single command statements such as:

if (condition) command
if condition { command }
if (condition) { command }

it doesn't matter what you pick, pick whichever one you think looks good for you, because as far as speed is concerned, these aren't the speed intense parts where you'll be able to save some processing time. Real speed gains will come from the way you code something, which algorithms you use, not if you use brackets or not. Only in tens of thousands of iterations will these parse speed differences become apparent, but the difference will still be near neglectible.

So, to conclude, pick whichever one you wish, but make sure that mIRC's parser can always know the boundary between where the condition stops, and the command starts.

In other words NOT like this:

if $1- == blah echo -a lol

--> Hard for the parser to know if the "echo" part is still part of the == check, or if it's the beginning of a command. Compared with the following, where mIRC can perfectly know where the condition ends, and the command begins:

if ($1- == blah) echo -a lol

or

if $1- == blah { echo -a lol }

or

if ($1- == blah) { echo -a lol }

If you're really interested to see the speed differences (just for fun), then do some benches, or look around with the Search feature, although the speeds of parsing might vary from mIRC version to version.

To conclude, what style you choose shouldn't be based on speed, but on what looks good for you, and what makes sure that mIRC will parse it in the way that you intended it.

I used to prefer the if condition { command } way, but now I've changed to if (condition) command because I think it looks neater. My code hasn't gotten slower or faster because of this choice though wink


Gone.
#139680 17/01/06 03:50 PM
Joined: Jan 2003
Posts: 2,523
Q
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
The order of speed (from fastest example to slowest) is 2,3,1. So if you have only one command to execute in an /if statement, the fastest alternative is #2. If you have more than 1 commands, #2 obviously cannot be used, so the fastest way in this case is #3. As for etiquette, I don't believe it's a matter of etiquette but of personal preference (actually what you're used to from other languages). mirc parses statements that lack () or {}, but NOT both, just fine. Of course, when you have more than one conditions combined with && or ||, grouping ()'s may be necessary.

Not surprisingly, FiberOPtics beat me by 6 minutes and with a more detailed explanation.

Last edited by qwerty; 17/01/06 03:55 PM.

/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
#139681 17/01/06 04:17 PM
Joined: Jul 2004
Posts: 59
S
synth7 Offline OP
Babel fish
OP Offline
Babel fish
S
Joined: Jul 2004
Posts: 59
I have more questions along the same lines as my first one....

1. Which is better etiquette or easier for mIRC to parse?
Code:
a. var %var blah

b. var %var = blah


2. I know this next one is retarded but oh well, it came up when I was coding a few minutes ago and I figured I'd throw it in here. Which is better?
Code:
a. var %whatever $iif($condition,1,0)

b. if ($condition) var %whatever 1
elseif (!$condition) var %whatever 0



#139682 17/01/06 04:44 PM
Joined: Jan 2003
Posts: 2,523
Q
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
1. Definitely b. /var %var value can break in many cases (try //var %a 1, %b $str(c,%a) | echo -a %b and then //var %a = 3, %b = $str(c,%a) | echo -a %b). The proper (and documented) way is /var %var = value

2. This is again a matter of personal preference but in this case, $iif() looks like the natural choice. I could go on about suggesting in which cases $iif() would probably be a better choice than /if, but these are things that you figure out yourself as you gain more experience.


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
#139683 17/01/06 05:13 PM
Joined: Sep 2005
Posts: 2,881
H
Hoopy frood
Offline
Hoopy frood
H
Joined: Sep 2005
Posts: 2,881
Actually, mIRC will parse statements without either as long as you have only one token on each side of the operator. Try these:

Code:
//if a == a echo -a this is fine
//if a == a && b != c echo -a this is fine too
//if a == a && b != c && fg == fg echo -a c


I still wouldn't recommend using this syntax, obviously.

#139684 17/01/06 06:02 PM
Joined: Nov 2003
Posts: 227
H
Fjord artisan
Offline
Fjord artisan
H
Joined: Nov 2003
Posts: 227
I couldn't put it any better than FiberOPtics and qwerty.

It's simple enough to test these things for yourself
just by putting the code in an alias that times the parse time
of the while loop.

Just remember that your OS is sharing the CPU with other things too. So you'd really need to take an mean time after
running the alias a few times.

Keep in mind, its very important to have an equal number of true and false tests in the if. I'm using $and to check for odd and even numbers in the loop, so it'll be 50/50 true false with an even number of loops.

Yes, as it was said, you'll not notice much difference most of the time and it would be better to design faster code in the first place.

You can play around with the following code to test it out
for yourself.
Just change the if in the while loop to the code you want to test.

Code:
alias benchif {
  var %t = $ticks
  var %n = 4000
  while (%n) {
    if ($and(%n,1) == 0) { .echo -q %n }
    dec %n
  }
  echo -a $calc($ticks - %t) ms.
}


Link Copied to Clipboard