• Welcome to Vice City Multiplayer.
 

Saving CPU/ Memory usage with loops

Started by Ginarrbrik, August 29, 2008, 03:07:50 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Ginarrbrik

Saving CPU/ Memory usage with loops.


  alias iniloop {
    var %a = 0,%b = 0,%c
    while (%a < $ini(test.ini,MYSECTION,0)) {
      %b = $readini(test.ini,MYSECTION,$ini(test.ini,MYSECTION,%a))
      %c = $ini(test.ini,MYSECTION,%a)
      inc %a
      if (%b == MyItem2) echo -a Item: %b - Value: %c
    }
  }


The initialization file: (Called Test.ini)

[MYSECTION]
MyItem1=MyValue1
MyItem2=MyValue2
MyItem3=MyValue3
MyItem4=MyValue4
MyItem5=MyValue5




How to Test your loops:

The best way to check yourself, is to make a loop that will repeat itself about 10000 times,
and in it try all sorts of ways to do what you want, and then see how many ticks it takes for the loop to finish.


alias test {
   var %ticks = $ticks
   var %a = 1
   while (%a < 10000) {

       ;The method you're checking comes here

   }
   echo ?a $calc($ticks - %ticks)
}


Just remember that 1000 ticks are equal to about 1 second.


Note:
Allot of coders are under the impression that the more lines a script holds, the more inefficient it is.

They are presuming incorrect.

The Memory eating and cpu eating elements inside a script are usually down to poorly designed loops.


Try it for yourself.

Ginarrbrik

#1
Lesson Two:

Identifiers and Loops:

Try not to use $identifiers in a while loop's argument.

If you know that the $identifier will return a constant value that won't be changed in the loop's process, then it's better to set the $identifiers value to a %variable, and then use the %variable as the argument.

That's because the loop will evaluate the $identifier on each run to check if the value hasn't changed.

Just to show you how much CPU you can change:

alias test {

  var %ticks = $ticks

  var %a = 1

  while (%a < $lines(test.ini)) {

    inc %a

  }

  echo -a $calc($ticks - %ticks)

}

This little alias will take 1297 ticks (test.ini has 976 lines)


But if I'd put the $lines value in a %variable, and use the %variable in the argument:

alias test {

  var %ticks = $ticks

  var %a = 1

  var %lines = $lines(test.ini)

  while (%a < %lines) {

    inc %a

  }

  echo -a $calc($ticks - %ticks)

}

This takes only 16 ticks to process.
And that's because it doesn't have to process the $lines identifier 976 times.

Ginarrbrik

Lesson Three:

Efficient HashTable Searching with loops:

Sometimes it's better to use a regular loop and a regular If (V1 iswm V2) sentence,
rather than looping through certain $identifiers that have Wildmatch options in them.


The best way to explain this is by an example using $hget to find certain items in a hash table.

For example, look at both these loops:

Loop One:

var %a = 1

while ($hfind(HashTable,*wildcard*,%a,w)) { 

  ;Do stuff here

  inc %a

}


Loop Two:

var %a = 1

while ($hget(HashTable,%a).item) {

  var %item = $v1

  if (*wildcard* iswm %item) {

    ;Do stuff here

  }

  inc %a

}


Although the first loop looks much neater and exploits the $hfind identifier to find you all the matching items, it will still take about twice as much time to resolve.

That happens because the $hfind identifier tries each time to find the next matching item, but it starts each time from the beginning of the Hash table. So if you have a lot of items that match the wildcard it will go over the the entire Hash table over and over.

But the second loop will just go over the Hash table once, and find all the matches.

Tommis

Hello,

TanaX01 will be dealt with accordingly very soon. I have deleted the posts off topic. Please continue this topic and disregard this message. :)

Good post by the way, I am sure it will help out people greatly.

Retards,
Tommis

Windlord

Indeed you have helped me realise how easy it is to make scripts more efficient ;D

Thanks a lot Ginarrbrik.

[OffTopic] I guess you meant 'Regards' there Tommis :P

Chezor

off topic: rofl @ tommis.. retards hahh  :D

Scripts: Fuel system in vehicles, Speedometer, Spawnback, Improved Copwork, Improved Jailing, Improved phone with sms, and many more. Coming soon..