Daklu Posted March 27, 2008 Report Share Posted March 27, 2008 In my code I often have arrays of clusters in which I need to find the element that matches one piece of data in the cluster but the other pieces of data don't matter. For example, if I have an array of clusters in which each cluster contains a number and a string, I'll need to find the element in which the string matches a specific value but the number doesn't matter. (Often I won't even know the value of the number.) I've always handled this by iterating through the array, unbundling each cluster, and comparing the strings. (Upper code line.) My benchmarking shows the search array prim to be ~20% faster than iterating for arrays that don't use clusters. Is there a way use a wildcard in place of a value in a cluster that would allow me to use the search array prim? (Lower code line.) Quote Link to comment
Aristos Queue Posted March 27, 2008 Report Share Posted March 27, 2008 Swap the order of your numeric and your string in the cluster, and I'll bet the Search 1D Array prim slows down to be a lot closer to your for loop. Currently the primitive is comparing that integer first, and if that doesn't match, it goes on to the next element. Comparing integers is always faster than comparing strings, so it's doing a lot less work than your for loop. No, I don't know of any generic solution within LV. I'm pretty sure that various users have written various public tools to do something like this, but I'm not sure. Perhaps one of them will post here. Quote Link to comment
Daklu Posted March 27, 2008 Author Report Share Posted March 27, 2008 QUOTE (Aristos Queue @ Mar 26 2008, 01:18 PM) Swap the order of your numeric and your string in the cluster, and I'll bet the Search 1D Array prim slows down to be a lot closer to your for loop. That's a good tip. Thanks! I hadn't thought about the order in which the cluster elements would get searched. Kind of a moot point in this particular case though as the search prim doesn't work for what I'm trying to do. (I coded it up as an example of how I imagine it would work.) Quote Link to comment
Popular Post Mellroth Posted March 28, 2008 Popular Post Report Share Posted March 28, 2008 Sometimes when I need to search through cluster arrays with some element values excluded, I do like this Download File:post-5958-1206627103.vi 1. Turn cluster array into a boolean cluster array by checking equality 2. Filter the active element by performing an AND operation 3. Search for the desired boolean cluster pattern In this case the resulting index is 4, since the first element wasn't activated. Hope this helps. /J 5 Quote Link to comment
Wire Warrior Posted March 28, 2008 Report Share Posted March 28, 2008 Nice! :thumbup: I'll be using that. Thanks Jason Quote Link to comment
orko Posted March 30, 2008 Report Share Posted March 30, 2008 QUOTE (JFM @ Mar 27 2008, 07:13 AM) 1. Turn cluster array into a boolean cluster array by checking equality2. Filter the active element by performing an AND operation 3. Search for the desired boolean cluster pattern That is genius. Thanks! I was trying to work out some sort of masking algorithm, but wasn't successful without some serious hack. Your way is slick! Quote Link to comment
Daklu Posted April 1, 2008 Author Report Share Posted April 1, 2008 I did some benchmarking to compare the standard unbundle method to JFM's boolean mask method. Initially the unbundle method was faster with increased benefit if the item was near the beginning of the array. This makes sense as it will stop searching as soon as the item is found while the masking method iterates through the entire array twice before searching. However, later testing with slightly different test code had the mask method consistently faster. Odd. Unfortunately my thumbstick appears to have digested my code. Quote Link to comment
Mellroth Posted April 2, 2008 Report Share Posted April 2, 2008 QUOTE (Daklu @ Mar 31 2008, 07:53 PM) Initially the unbundle method was faster with increased benefit if the item was near the beginning of the array. This makes sense as it will stop searching as soon as the item is found while the masking method iterates through the entire array twice before searching. However, later testing with slightly different test code had the mask method consistently faster. Odd. I think it is safe to say that the unbundle version should be faster for small arrays, and when only a few elements in the cluster is checked. The main goal for me when I first started to use this, was not speed, but a way to easily update the code to include many cluster elements and to let a user select elements to search from a GUI. The search algorithm is pretty much unchanged regardless of the size of the cluster, making it easier to maintain. /J Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.