Page 1 of 1

SystemFindObjectBySerial V.S. FindObjtypeInContainer()

Posted: Fri Jun 09, 2006 2:18 am
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]

Posted: Fri Jun 09, 2006 2:43 am
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

Posted: Fri Jun 09, 2006 6:19 am
by Austin
Ever since Racalac switched POL's innards to a hash table, SFOBS() will be the fastest way to go.

Posted: Fri Jun 09, 2006 8:50 am
by Lagoon
Thank you both for your help :)

Posted: Fri Jun 09, 2006 6:02 pm
by MuadDib
And it'll be better once we get it seeing multis again, hehe.

Posted: Sat Jun 10, 2006 1:56 am
by Lagoon
Yep, I count on it for my house rental system :P