mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Jul 2003
Posts: 655
Om3n Offline OP
Fjord artisan
OP Offline
Fjord artisan
Joined: Jul 2003
Posts: 655
Concider the following code, taken from part of a multiserver manager that stores data in a .csv file and uses fopen to send each line to a command/server line compiler.

%rtn = %rtn otherstuff $iif($regml(muse,6), -i $v1 $iif($regml(muse,7),$v1 $iif($regml(muse,8),$v1 $iif($regml(muse,9),$v1))))

if ($regml(muse,6)) {
%rtn = %rtn -i $v1
if ($regml(muse,7)) {
%rtn = %rtn $v1
if ($regml(muse,8)) {
%rtn = %rtn $v1
if ($regml(muse,9)) {
%rtn = %rtn $v1
}
}
}
}

I wrote the line compilar in a few different ways, for the most part $iif has proven the better choice, and reduces the code by about 20%, but in the case above would i be correct in assuming the if() method would be faster?

My thinking behind this is basically that with the $iif, even when the top level $iif fails, it must read the rest of the code still to find if/what code to perform for a false return. Where as with the if() method in this case mirc syntax parser has been built with the ability to jump out of current if statement, as it would do at the first instance of failure?

Is there a 'rule of thumb' per say for deciding when to use $iif and when to use if()?

Also, what is the best method of timing how long a script takes to run accurately? I have written the compiler identifier in several different methods and with different storage methods, there is conciderable and noticable difference with some of those (due to slower file reading method and larger code), but $ticks just doesn't seem to be accurate or precise enough.


"Allen is having a small problem and needs help adjusting his attitude" - Flutterby
Joined: Aug 2004
Posts: 7,252
R
Hoopy frood
Offline
Hoopy frood
R
Joined: Aug 2004
Posts: 7,252
I don't know about a rule of thumb, but I have found that there are times when $iif will NOT work, returning an INVALID parameters error, even though all of the parameters were provided.

I can't find an example of code where the above was true, as I've already corrected the error by using if/elseif/else


Basically the rule that I use is
A) is it a simple yes or no, rather than yes/no/maybe-so
B) Am I controlling a single function with it
Eg: from part of my WhoIs/WhoWas dialog
Code:
 menu * { 
  Who2 : .who2 $iif($snicks,$v1,$$?="Nick(s) to whois") 
} 
 

C) Do I need multiple statements executed upon the results of the decision?

If A or B is true, then use $iif, otherwise use If/elseif/else

Joined: Sep 2003
Posts: 4,230
D
Hoopy frood
Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
From memory /IF is faster than $iif outright. However that said, $iif offers inline multichoices within commands, while an IF must have two copies of the command one for each choice.
Not so hard if there is just one IF, but what if its 6 A or B options etc.

In your examples the $iif isnt being put to the full use of its ability of course, but is a good example of where a layered IF would have speed advantages, although as I said /IF is faster anyway, so this maybe just the panning out of that speed.
I created the following test to see what might come out
Code:
alias testx {
  var %max = 10000
  var %x = 5
  while (%x) {
    var %ticks = $ticks
    var %i = %max, %rtn
    while (%i) {
      %rtn = $iif(1 < %x, -i $v1 $iif(2 < %x,$v1 $iif(3 < %x,$v1 $iif(4 < %x,$v1))))
      dec %i
    }
    var %m1 = $calc($ticks - %ticks)
    ;
    var %ticks = $ticks
    var %i = %max, %rtn
    while (%i) {
      if (1 < %x) {
        %rtn = -i $v1
        if (2 < %x) {
          %rtn = %rtn $v1
          if (3 < %x) {
            %rtn = %rtn $v1
            if (4 < %x) {
              %rtn = %rtn $v1
            }
          }
        }
      }
      dec %i
    }
    var %m2 = $calc($ticks - %ticks)
    ;
    echo -a TEST %x : Method1 ticks %m1 : Method2 ticks %m2 ( $+ $calc(%m2 / %m1 * 100) $+ % $+ ) -- $calc(%m1 / %max) : $calc(%m2 / %max)
    ;
    dec %x
  }
}

results as follo0ws on my machine here
Code:
TEST 5 : Method1 ticks 2062 : Method2 ticks 1438 (69.738118%) -- 0.2062 : 0.1438
TEST 4 : Method1 ticks 2062 : Method2 ticks 1297 (62.900097%) -- 0.2062 : 0.1297
TEST 3 : Method1 ticks 1703 : Method2 ticks 1031 (60.540223%) -- 0.1703 : 0.1031
TEST 2 : Method1 ticks 1312 : Method2 ticks  766 (58.384146%) -- 0.1312 : 0.0766
TEST 1 : Method1 ticks  875 : Method2 ticks  500 (57.142857%) -- 0.0875 : 0.05


test 5 means all conditions were meet and progressing back to test1 meaning no conditions were meet (none becuase each condition relies apon the previous to be true to be checked)
The end two numbers are ticks for method1 and method2 per test

So at 10,000 occurances your saving between 0.375 and 0.624 seconds total, now this might be a significant saving if your calculating the gravity effects of a black hole on the movement of every particile within its displacement field, but is it this important in where you have it. (im not being smart here, I really dont know how time criticle or laggingly this code is)

* to me the choice of to use /IF or $iif comes down to what one makes the code easier to diagnose whats going wrong or right with it, if i have to come back to it.

Joined: Jul 2003
Posts: 655
Om3n Offline OP
Fjord artisan
OP Offline
Fjord artisan
Joined: Jul 2003
Posts: 655
Thanks for the reply, indeed its not very important at all as it will run only once, i just wrote several versions of the code using several different data storage methods to see what i could come up with as far as code optimization goes. Which led to wonder about the behavior of imbedded iif and if calls.

The alias in the script basically compiles a command line given information from a csv file.


"Allen is having a small problem and needs help adjusting his attitude" - Flutterby

Link Copied to Clipboard