okay not sure if there is a better way than I’m already doing. Right now I have a list(int[]) that has a over 10,000 values that equal 0 in it and I’m looking for only the non-zero items.
My current approach is just to do a for loop and capture all the non-zeros but I do this a lot, and profiling shows its taking a large amount of my cpu time(since I’m doing this so often). Is there a way I can get the same results without the expensive cpu process(since out 10,000 items, only less than 100 will be non-zeros)?
Here’s an example of my data:
int[] list = {0,0,0,1,0,10 }
int[] list_names = {a,b,c,d,e,f}
All I need to ultimately do is use these two lists to create another two lists with only the non-zero values and their names(so D=1 and F=10). I’ve seen some solutions where I need to sort the results before it works but thats a problem because if I sort the data list then I cannot identify its name.
Is this possible and is there a faster way compared to a for loop?
Sorry I should mention that this large lists remain in my program for processing and I’m trying to do this to reduce the memory footprint of them. I have a queue of a few hundred million of these lists being stored in their entirety when all I really need is the non-zero values, so doing this to save memory(which seems to be working) but I’m also trying not to take a bit hit on the cpu to get it to that point(since I need the cpu for my processing).
If you don’t know anything else about the array, then you’re going to have to look at every value. That means that looping over the whole array is basically the best you can hope for. If it’s sorted, or if you know some other information about it, that might help — but otherwise, there are no alternatives.