SystemFindObjectBySerial V.S. FindObjtypeInContainer()

Get Help on scripting in POL with configurations, config setups, script trouble shooting, etc.
Post Reply
Lagoon
Grandmaster Poster
Posts: 118
Joined: Sun Mar 05, 2006 7:25 am

SystemFindObjectBySerial V.S. FindObjtypeInContainer()

Post by Lagoon »

I have to find a specific item in a pc backpack. I have the pc ref, the item serial and the item objtype
Assuming the pc as a standard number of items in his backpack (say 15-20 items?) and that 1 of them is the searched item and 3 of them have the same objtype of the item, which of the two codes should be the fastest?

Code: Select all

-- Code 1 --
function SearchItemInBackpack(pc, searched_serial, searched_objtype)
  var searched_item := SystemFindObjectBySerial(searched_serial);
endfunction

Code: Select all

-- Code 2 --
function SearchItemInBackpack(pc, searched_serial, searched_objtype)
  var searched_item;
  var items := FindObjtypeInContainer(pc.backpack, searched_objtype);
  foreach item in item
    if(item.serial == searched_serial)
      searched_item := item;
      break;
    endif
  endforeach
endfunction
[/code]
Bytehawk
Apprentice Poster
Posts: 56
Joined: Fri Feb 03, 2006 2:25 am

Post by Bytehawk »

I'd try to find it out by using a stopwatch :)

Code: Select all

use polsys;
use uo;

program test(who)

  var found;
  var start:= 0;

  start:= ReadMillisecondClock ();
  found:= SearchItemInBackpack_1(pc, searched_serial, searched_objtype);
  SendSysMessage (who, "SystemFindObjectBySerial took " + (ReadMillisecondClock () - start) + " ms.");

  start:= ReadMillisecondClock ();
  found:= SearchItemInBackpack_2(pc, searched_serial, searched_objtype);
  SendSysMessage (who, "FindObjtypeInContainer took " + (ReadMillisecondClock () - start) + " ms.");

endprogram


function SearchItemInBackpack_1(pc, searched_serial, searched_objtype)
  return SystemFindObjectBySerial (searched_serial);
endfunction endfunction


function SearchItemInBackpack_2(pc, searched_serial, searched_objtype)

  foreach item in FindObjtypeInContainer (pc.backpack, searched_objtype)
    if (item.serial == searched_serial)
      return item;
    endif
  endforeach

endfunction
User avatar
Austin
Former Developer
Posts: 621
Joined: Wed Jan 25, 2006 2:30 am

Post by Austin »

Ever since Racalac switched POL's innards to a hash table, SFOBS() will be the fastest way to go.
Lagoon
Grandmaster Poster
Posts: 118
Joined: Sun Mar 05, 2006 7:25 am

Post by Lagoon »

Thank you both for your help :)
MuadDib
Former Developer
Posts: 1091
Joined: Sun Feb 12, 2006 9:50 pm

Post by MuadDib »

And it'll be better once we get it seeing multis again, hehe.
Lagoon
Grandmaster Poster
Posts: 118
Joined: Sun Mar 05, 2006 7:25 am

Post by Lagoon »

Yep, I count on it for my house rental system :P
Post Reply