Hel Posted December 7, 2016 Share Posted December 7, 2016 (edited) Hi, I have two pieces of code which I've been trying to optimize for the last couple of hours, I've looked through the API, I've fiddled in eclipse and I just can't seem to find a logical answer. In this first piece, I'm grabbing all the NPCs around the player, checking if they're a fishing spot, & if they are, I'm adding them to an arraylist called fishingSpots. (I'm doing this to detect which kind of fishing spot is the type I want to fish at, where I then iterate over the fishingSpots & check to see if they have the correct interactions) List<NPC> objects = main.getNpcs().getAll(); ArrayList<NPC> fishingSpots = new ArrayList<NPC>(); for (int i = 0; i < objects.size(); i++) { if (objects.get(i).getName().equalsIgnoreCase("fishing spot")) { fishingSpots.add(objects.get(i)); } main.randSleep(8, 12); // This is just a void that sleeps(random(min,max)), I used this as an attempt to reduce CPU load, but when iterating over objects, there are some 6000, having a sleep causes incredible delay } & Inventory slots -- I'm checking to see if an item is in the slot x, where x is a random whole number from 0-27 inclusive. int i = (int) Math.round(Math.random() * 27); while (main.getInventory().getItemInSlot(i) == null) { i = (int) Math.round(Math.random() * 27); main.randSleep(8, 12); } These loops cause an incredible amount of CPU load, jumping from some 8% up to, what I've seen, 60% load.Any and all help would be appreciated. Edited December 7, 2016 by viri Quote Link to comment Share on other sites More sharing options...
Token Posted December 7, 2016 Share Posted December 7, 2016 Naming a List of NPCs "objects" will increase your CPU usage by 100x Expand your condition in a CNF formula which gradually filters a large amount of items with lowest CPU usage at every step. Remove that sleep. 3 Quote Link to comment Share on other sites More sharing options...
Hel Posted December 7, 2016 Author Share Posted December 7, 2016 I'm unsure if you're just upset with me because I named the list of NPCs "objects" (Originally was using bank booths, hence the name) or if it actually bothers the CPU?CNF Formula? Never heard of that before. Is this something I should be investing my time into researching? Quote Link to comment Share on other sites More sharing options...
Explv Posted December 7, 2016 Share Posted December 7, 2016 (edited) I'm unsure if you're just upset with me because I named the list of NPCs "objects" (Originally was using bank booths, hence the name) or if it actually bothers the CPU? CNF Formula? Never heard of that before. Is this something I should be investing my time into researching? Use Filters to filter npcs: Filter class: http://osbot.org/api/org/osbot/rs07/api/filter/Filter.html Filter API: http://osbot.org/api/org/osbot/rs07/api/filter/FilterAPI.html Example usage: List<NPC> fishingSpots = getNpcs().filter(getNpcs().getAll(), new Filter....);There is also a .singleFilter method that will return a single NPC Edited December 7, 2016 by Explv 1 Quote Link to comment Share on other sites More sharing options...
Hel Posted December 7, 2016 Author Share Posted December 7, 2016 Alright, thankyou both for your help Quote Link to comment Share on other sites More sharing options...
Lemons Posted December 7, 2016 Share Posted December 7, 2016 (edited) Hi, I have two pieces of code which I've been trying to optimize for the last couple of hours, I've looked through the API, I've fiddled in eclipse and I just can't seem to find a logical answer. In this first piece, I'm grabbing all the NPCs around the player, checking if they're a fishing spot, & if they are, I'm adding them to an arraylist called fishingSpots. (I'm doing this to detect which kind of fishing spot is the type I want to fish at, where I then iterate over the fishingSpots & check to see if they have the correct interactions) List<NPC> objects = main.getNpcs().getAll(); ArrayList<NPC> fishingSpots = new ArrayList<NPC>(); for (int i = 0; i < objects.size(); i++) { if (objects.get(i).getName().equalsIgnoreCase("fishing spot")) { fishingSpots.add(objects.get(i)); } main.randSleep(8, 12); // This is just a void that sleeps(random(min,max)), I used this as an attempt to reduce CPU load, but when iterating over objects, there are some 6000, having a sleep causes incredible delay } & Inventory slots -- I'm checking to see if an item is in the slot x, where x is a random whole number from 0-27 inclusive. int i = (int) Math.round(Math.random() * 27); while (main.getInventory().getItemInSlot(i) == null) { i = (int) Math.round(Math.random() * 27); main.randSleep(8, 12); } These loops cause an incredible amount of CPU load, jumping from some 8% up to, what I've seen, 60% load. Any and all help would be appreciated. For the last loop, hard to understand what you're trying to accomplish. It seems you're trying to wait until you have caught a fish? If so, you method currently would continuously loop until it found a non-empty inventory slot, then I assume go right back into that loop again. This will ofc use a lot of CPU. Try using a conditional sleep: int lastSlots = getInventory().getEmptySlots(); new ConditionalSleep(5000) { // Sleeps are in milliseconds, sleep for 5 secs @0verride public boolean condition() throws InterruptedException { return getInventory().getEmptySlots() == lastSlots; } }; Edited December 7, 2016 by Lemons 1 Quote Link to comment Share on other sites More sharing options...
Hel Posted December 7, 2016 Author Share Posted December 7, 2016 What I was trying to do with that was generate a random whole number from 0 to 27 inclusive (the inventory slots) Then check if that random number (random item slot) had an item in it, if it didn't, it entered a while loop & kept regenerating a random inventory slot until there was an item in the randomly generated slot number. I'd rather if it wasn't spoon fed to me either, I'm actually interested in this and would prefer to just get a directional nudge, so I can still benefit from this, thanks Quote Link to comment Share on other sites More sharing options...