mIRC Home    About    Download    Register    News    Help

Print Thread
Speed up scripts? (tips wanted) #254593 19/08/15 12:24 AM
Joined: Feb 2015
Posts: 19
Y
Y4kuzi Offline OP
Pikka bird
OP Offline
Pikka bird
Y
Joined: Feb 2015
Posts: 19
Hi guys.

I was reading the mIRC Help file and I came across this info:
Quote:
Using brackets speeds up processing. If an alias uses too few brackets then the statement might be ambiguous and the alias will take longer to parse, might be parsed incorrectly, or might not be parsed at all.

I want some clarification on that.
Does this mean that if ($nick == $me) will execute faster than if $nick == $me?

Another example:
This: alias chtypes return #+
or: alias chtypes { return #+ }

And what about if-statements?

1. if (%mode.check) && (%flag.check) { return 1 }
2. if (%mode.check && %flag.check) { return 1 }
3. if %mode.check && %flag.check { return 1 }

4. if (%mode.check) && (%flag.check) return 1
5. if (%mode.check && %flag.check) return 1
6. if %mode.check && %flag.check return 1

I think method 1 will be fastest in this example, or does it not really matter?


Please provide some of your own experience and information, because if this is indeed the case I have 7000+ thousands of lines of code to adjust.
But if it will really improve the speed, I will gladly do it with pleasure!

Thanks in advance.


P.S. Other speed improvement tips are also welcome!


ProvisionIRCd - advanced mIRC based IRC daemon.
http://www.provisionircd.tk
Re: Speed up scripts? (tips wanted) [Re: Y4kuzi] #254595 19/08/15 08:53 AM
Joined: Sep 2014
Posts: 259
S
Sakana Offline
Fjord artisan
Offline
Fjord artisan
S
Joined: Sep 2014
Posts: 259
Hmm, I seem to get the opposite. The same code with less brackets and parenthesis appears to be faster..

Code:
alias test1 var %y = 0 | if 1 == 1 && %y == 0 return 1

alias test2 { var %y = 0 | if (1 == 1) && (%y == 0 ) { return 1 } }

alias runtest {

  var %i = 0
  var %t = $ticks

  while (%i < 100000) {
    test1
    inc %i
  }

  echo -a test1 --- $calc($ticks - %t) ms

  var %i = 0
  var %t = $ticks

  while (%i < 100000) {
    test2
    inc %i
  }

  echo -a test2 --- $calc($ticks -%t) ms
}



result
Quote:
test1 --- 3198 ms
test2 --- 3276 ms

Re: Speed up scripts? (tips wanted) [Re: Sakana] #254596 19/08/15 09:00 AM
Joined: Dec 2013
Posts: 779
N
Nillen Offline
Hoopy frood
Offline
Hoopy frood
N
Joined: Dec 2013
Posts: 779
Using the same script:
test1 --- 5156 ms
test2 --- 5812 ms

This is on Windows 10, SSD Drive using 7.41


Nillens @ irc.twitch.tv
Nillen @ irc.rizon.net
Re: Speed up scripts? (tips wanted) [Re: Y4kuzi] #254600 19/08/15 10:27 AM
Joined: Jul 2006
Posts: 3,671
W
Wims Offline
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 3,671
Hello, the fastest in order:

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

Regarding what the help file says, it only means that it might fail, in reality there are only a few cases which fail, involving || and &&:

if (condition || condition) command
reports an 'ambiguous' format error whereas
if (condition && condition) command
reports an 'echo' unknown operator format, this is inconsistent but not much of a problem.

Quote:
Does this mean that if ($nick == $me) will execute faster than if $nick == $me?
It depends on if you use { } or not, as said above.
In some cases, mIRC will guess correctly faster than it would perfectly understand otherwise because of the extra parsing of { }.
Basically this is just text processing, less text to process means it will be faster (not always true, but true 99% of the time).
For example for your next question, processing aliases isn't as complex as if statement, meaning parsing the extra { } is just slower, of course it's slower by some pico seconds, so you can only see the difference when benchmarking with 50000 iterations, which is nowhere close to a real case scenario, but still.

Any other speed improvement is based on the text processing story, the less you do with mIRC scripting, the faster it will be, always try to get mIRC to do the job internally, which is done in C.

For example, $iif doesn't have it's own function, it's just an internal alias which is calling /if and return the result based on what /if reports, this makes $iif significantly slower than /if:

Code:
alias testingiif1 {
  $iif(1 == 2,noop 1,noop 2)
}

alias testingiif2 {
  if (1 == 2) noop 1
  else noop 2
}


/testingiif1 looks appealing because you have only one line, it looks great.
Let's try:
Code:
//var %a 50000,%t $ticks | while (%a) { dec %a | testingiif1 } | echo -a $calc($ticks - %t)

followed by
Code:
//var %a 50000,%t $ticks | while (%a) { dec %a | testingiif2 } | echo -a $calc($ticks - %t)


Quote:
2262
1623
if you have an on text event with one $iif, changing it to /if isn't going to make the script faster, or by some pico seconds.. But if you have a while loop repeating an $iif statement in that on text event, it's a good idea to change that $iif to /if.

Speed improvement really depends on what you are trying to do, using COM object for socket is significantly faster than using mIRC's socket for example, using command which does a lot of stuff internally such as /filter, $hfind, are usually the way to go, a clear example is using $read's 'w' switch instead of looping on all line with $read itself and comparing with mirc scripting with if (expression iswm $read(,N)).

Edit:

@sakana, your benchmark is not fair because you're comparing two differents things at the same time ({} around aliases and around the if statement, () used etc)

It must be noted that mIRC doesn't accept that you don't use () if you don't use { } and if the condition doesn't involve the three elements: first operand, operator, second operand:

//if 1 == 1 echo -a ok -- works
//if 1 echo -a ok -- doesn't work
//if a !isnum echo -a ok -- doesn't works
all of the above works if you add { }, this even lead to quirk where mIRC does not parse the rest as you would expect:
//if 1 == 1 var -s %a ok -- doesn't work because with /var or /set etc, mIRC is supposed not to evaluate the variable name, otherwise it would never work, but in this case, although it validates the condition, it fails not to evaluate the variable:
//if 1 == 1 var -s % $+ a ok -- works.

With that in mind, you can benchmark all of these:

Code:
alias testingif {
  var %a 50000,%t $ticks
  while (%a) {
    dec %a
    if (1 == 1) && (1 == 1) noop
  }
  echo -a (condition) && (condition) command no bracket in $calc($ticks - %t)
  %a = 50000
  %t = $ticks
  while (%a) {
    dec %a
    if (1) && (1) { noop }
  }
  echo -a (condition) && (condition) command ye bracket in $calc($ticks - %t)

  %a = 50000
  %t = $ticks
  while (%a) {
    dec %a
    if (1 && 1) { noop }
  }
  echo -a (condition && condition) command ye bracket in $calc($ticks - %t)
  %a = 50000
  %t = $ticks
  while (%a) {
    dec %a
    if 1 == 1 && 1 == 1 noop 
  }
  echo -a condition && condition command no bracket in $calc($ticks - %t)

  %a = 50000
  %t = $ticks
  while (%a) {
    dec %a
    if 1 == 1 && 1 == 1 { noop }
  }
  echo -a condition && condition command ye bracket in $calc($ticks - %t)

  %a = 50000
  %t = $ticks
  while (%a) {
    dec %a
    if (1 == 1 && 1 == 1) { noop }
  }
  echo -a (condition && condition) command ye bracket in $calc($ticks - %t)
}



Quote:
(condition) && (condition) command no bracket in 1373
(condition) && (condition) command ye bracket in 1248
(condition && condition) command ye bracket in 1232
condition && condition command no bracket in 1357
condition && condition command ye bracket in 1482
(condition && condition) command ye bracket in 1482
So when && is involed, the fastest seems to be using both () and { }, so with && involved, mIRC is slower to guess even though there is less text to process whereas it's the other way around when && isn't involved.Edit : The fastest seems to be if (condition && condition) { command }, not if (condition) && (condition) { command } like the above implied.

Edit2: well after running this a bit more, both the 2nd and 3rd are equivalent (sometimes equal, sometimes one is ahead, sometimes it's the other), i extended the benchmark to 100000 iterations with still the same result, but clearly you should not observe that "if (condition) && (condition) commands" is faster, it takes 300 more millisecond than the fastest here :\


Last edited by Wims; 19/08/15 03:55 PM.

Looking for a good help channel about mIRC? Check #mircscripting @ irc.swiftirc.net
Re: Speed up scripts? (tips wanted) [Re: Wims] #254603 19/08/15 02:10 PM
Joined: Feb 2015
Posts: 19
Y
Y4kuzi Offline OP
Pikka bird
OP Offline
Pikka bird
Y
Joined: Feb 2015
Posts: 19
My results are a little different.

I get:

Code:
(1 == 1) && (1 == 1) command no bracket in 1157
1 == 1 && 1 == 1 command no bracket in 1093
(1 == 1) && (1 == 1) command no bracket in 1079
(1) && (1) { commands } in 953
(1) && (1) commands in 875
(1 && 1 || 2 == 3) { commands } in 1093
1 == 1 && 1 == 1 { commands } in 1219
(1 == 1 && 1 == 1) { commands } in 1235


which means (condition) && (conditions) commands is faster for me, but not for you. Am I missing something?

Last edited by Y4kuzi; 19/08/15 02:10 PM.

ProvisionIRCd - advanced mIRC based IRC daemon.
http://www.provisionircd.tk
Re: Speed up scripts? (tips wanted) [Re: Y4kuzi] #254604 19/08/15 02:52 PM
Joined: Jul 2006
Posts: 3,671
W
Wims Offline
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 3,671
As different as the code you used I'd say, why is there ||?
Did you use something to display the { }? that would change the result, can you give the code you used, it's clearly different from the one I gave.

Edit: it seems like I cannot tell which number is bigger, given two numbers, I edited my previous post wink

Last edited by Wims; 19/08/15 03:01 PM.

Looking for a good help channel about mIRC? Check #mircscripting @ irc.swiftirc.net
Re: Speed up scripts? (tips wanted) [Re: Wims] #254606 19/08/15 03:15 PM
Joined: Feb 2015
Posts: 19
Y
Y4kuzi Offline OP
Pikka bird
OP Offline
Pikka bird
Y
Joined: Feb 2015
Posts: 19
I adjusted it a bit, here it is:

Code:
alias testingif {
  echo -a -
  var %a 50000,%t $ticks
  while (%a) {
    dec %a
    if (1 == 1) && (1 == 1) noop
  }
  echo -a (1 == 1) && (1 == 1) command no bracket in $calc($ticks - %t)

  %a = 50000
  %t = $ticks
  while (%a) {
    dec %a
    if 1 == 1 && 1 == 1 noop 
  }
  echo -a 1 == 1 && 1 == 1 command no bracket in $calc($ticks - %t)

  %a = 50000
  %t = $ticks
  while (%a) {
    dec %a
    if (1 == 1) && (1 == 1) noop
  }
  echo -a (1 == 1) && (1 == 1) command no bracket in $calc($ticks - %t)

  %a = 50000
  %t = $ticks
  while (%a) {
    dec %a
    if (1) && (1) { noop }
  }
  echo -a (1) && (1) $chr(123) commands $chr(125) in $calc($ticks - %t)

  %a = 50000
  %t = $ticks
  while (%a) {
    dec %a
    if (1) && (1) noop
  }
  echo -a (1) && (1) commands in $calc($ticks - %t)

  %a = 50000
  %t = $ticks
  while (%a) {
    dec %a
    if (1 && 1 || 2 == 3) { noop }
  }
  echo -a (1 && 1 || 2 == 3) $chr(123) commands $chr(125) in $calc($ticks - %t)

  %a = 50000
  %t = $ticks
  while (%a) {
    dec %a
    if 1 == 1 && 1 == 1 { noop }
  }
  echo -a 1 == 1 && 1 == 1 $chr(123) commands $chr(125) in $calc($ticks - %t)

  %a = 50000
  %t = $ticks
  while (%a) {
    dec %a
    if (1 == 1 && 1 == 1) { noop }
  }
  echo -a (1 == 1 && 1 == 1) $chr(123) commands $chr(125) in $calc($ticks - %t)
  echo -a -

}


ProvisionIRCd - advanced mIRC based IRC daemon.
http://www.provisionircd.tk
Re: Speed up scripts? (tips wanted) [Re: Y4kuzi] #254608 19/08/15 03:43 PM
Joined: Jul 2006
Posts: 3,671
W
Wims Offline
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 3,671
Yes, this is biased, evaluating $chr() takes time, compared to not evaluating $chr() at all, that's why i used "ye" and "no", to make a fair test, although there are still extra processing of the eventual () in those echos, but that should not be significant


Looking for a good help channel about mIRC? Check #mircscripting @ irc.swiftirc.net
Re: Speed up scripts? (tips wanted) [Re: Y4kuzi] #254619 19/08/15 06:13 PM
Joined: Feb 2015
Posts: 19
Y
Y4kuzi Offline OP
Pikka bird
OP Offline
Pikka bird
Y
Joined: Feb 2015
Posts: 19
Thanks for the help, Wims.

I don't think I will edit over 7000 lines of code, for I do not know how beneficial it will be in the end.
It is a lot of work, and I'm still quite unsure what is better in certain situations.


Thanks again. smile


ProvisionIRCd - advanced mIRC based IRC daemon.
http://www.provisionircd.tk
Re: Speed up scripts? (tips wanted) [Re: Y4kuzi] #254625 19/08/15 11:50 PM
Joined: Jul 2006
Posts: 3,671
W
Wims Offline
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 3,671
No problem, to be honnest I'm surprised by the results regarding &&, they don't make much sense to me but oh well.
Eventually you can pastebin specific code if you want to know if they could be made better


Looking for a good help channel about mIRC? Check #mircscripting @ irc.swiftirc.net
Re: Speed up scripts? (tips wanted) [Re: Wims] #254649 21/08/15 05:08 PM
Joined: Apr 2003
Posts: 342
M
MeStinkBAD Offline
Fjord artisan
Offline
Fjord artisan
M
Joined: Apr 2003
Posts: 342
Not using opening and closing parentheses and/or brackets w/ your code is like never using punctuation in paragraphs. Probably faster to type, but it looks sloppy, and you'll regret it later on.

Last edited by MeStinkBAD; 21/08/15 05:10 PM.

Beware of MeStinkBAD! He knows more than he actually does!