C++ What is the fastest way to scan for certain elements within a unsigned char array and a unsigned char vector? -
I have a small question, what is the fastest way to scan for some large elements for large elements, which Is that a large unsigned four array and a vector only contains unsigned cell elements? The direct answer would be great, but the detailed answer to the depth would be amazing fast what do I mean? Basically, to search for some characters within at least one second, I know that this was not a very educated definition ...
Note: the array has not been sorted.
General Announcement: with random bytes, say, I want to search for letter 'A' in Array, I will just write this loop to find it: Note: The search process will search for more than one element, mainly, 256. Therefore, you can take advantage of that magical number. For loop method: std :: count method: Is there a faster way to search for some elements in the array? Some thoughts - please do not give me a thumb down for this! It is only an idea that I have some opinion. Sorting Do we copy the array better and sort it out? Why make a copy? OK, because we need to keep the original content. The goal is to basically scan and calculate the occurrence of a character. Remember, the point of motion means that the process of copying should be fast. Why? Well, let's read it: @ Kiril Kirov: Depends on if you want to find a single letter, not at all. Copying the array is a costly operation - it's sorted out - even more expensive. OK, if you have only one array and you want to find, then suppose that there are 100 different characters, then this method can give you better performance. Now, it really depends on your use. And for this matter you will not be able to give any correct answer. You need to run more profiles for this. * For more, scroll to @Kiril Kroov's informative post. Answer: So far, there is no concrete or no response, because to achieve this goal there is not really "fast" method, especially when it did not sort However, Threads may be possible solution but, look for our CPU! It was based on Andrea's present answer (scroll a bit more for more information) - I expected that I read it right. As others have written, the complexity of the best algorithm To make searching faster, you can split the array and scan each part separately in different threads . This will be linearly scale with the number of available CPU cores on your machine. For example, if you have four cores, then place four threads and scanned each thread one-fourth of the array. Perhaps this discussion can help: In any case (and it is true for any display related issues), you should profile your code Make a test case for you that approach, measure the time it takes, and take it as a baseline. Then, for every modification, repeat the measurement to check whether it actually improves execution time. Also make sure that to measure each measurement more than once (in the case of a single test) and average, make sure to reduce caching and other heating to the effect (ideally, the first measurement Execute the code at least once before starting) This is Java related, but gives some good feedback that it does not parallel in all cases: unsigned char * array = new unsigned char [50000]; Std :: vector & lt; Unsigned four & gt; Vace (50000); / * * Fill Array & amp; VAC * /
unsigned int count = 0; For the (unsigned index index = 0; index! = 50000; ++ index) if (array [index] == 'A') calculation ++;
unsigned int count = std :: count (Array, Array + 50000, ' a' );
Answer: No, and it's not worth it!
O (n) , especially when your array is not sorted.
Comments
Post a Comment