mIRC Homepage

Which method is fastest

Posted By: synth7

Which method is fastest - 17/01/06 02:31 PM

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.

    if ($1 == test) { msg # test }

    if ($1 == test) msg # test 

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

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

Posted By: captain_comic

Re: Which method is fastest - 17/01/06 03:31 PM

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.
Posted By: FiberOPtics

Re: Which method is fastest - 17/01/06 03:44 PM

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


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


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
Posted By: qwerty

Re: Which method is fastest - 17/01/06 03:50 PM

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.
Posted By: synth7

Re: Which method is fastest - 17/01/06 04:17 PM

I have more questions along the same lines as my first one....

1. Which is better etiquette or easier for mIRC to parse?
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?
a. var %whatever $iif($condition,1,0)

b. if ($condition) var %whatever 1
elseif (!$condition) var %whatever 0
Posted By: qwerty

Re: Which method is fastest - 17/01/06 04:44 PM

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.
Posted By: hixxy

Re: Which method is fastest - 17/01/06 05:13 PM

Actually, mIRC will parse statements without either as long as you have only one token on each side of the operator. Try these:

//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.
Posted By: Hitchhiker

Re: Which method is fastest - 17/01/06 06:02 PM

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.

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.
© 2021 mIRC Discussion Forums