Tips for improving search speed

Source: Internet
Author: User

We usually write the following code to find an element in an array or a character in a string. Although such code is concise and clear, it is not a good solution in the case of many array elements. Today I will share a tips to improve the search speed.

// Search for an int find (int A [], int N, int element) in an int Array {for (INT I = 0; I <n; I ++) {if (a [I] = element) return I;} return-1;} // search for a character int find (string & STR, char C) in a string) {for (INT I = 0; I <Str. length (); I ++) {If (STR [I] = c) return I;} return-1 ;}

Although such code is written every time, I always think that the <judgment in the for loop is a bit redundant. For example, if there are 100 elements in the array, we know that the first 99 do not cross the array, I <n is not needed at all, but we have made 99 more judgments. I found this trick when I watched programming Pearl last night. I will share it today.

The above two methods are transformed as follows:

// Search for an int find1 (int A [], int N, int element) {If (n <= 0) Return-1; if (A [-- N] = element) return N; int hold = A [n]; A [n] = element; int I = 0; (;; I ++) {if (a [I] = element) break;} A [n] = hold; return I <n? I:-1;} // search for a character int find1 (string & STR, char c) {int n = Str. length (); If (n <= 0) Return-1; if (STR [-- N] = c) Return N; int hold = STR [N]; STR [N] = C; int I = 0; For (; I ++) {If (STR [I] = C) break ;} STR [N] = hold; return I <n? I:-1 ;}

How can I keep it so long? But it does reduce the number of judgments. If the array is large, it must be faster. If you have to say that the array is small, maybe the speed will be lower, so you won't be able to write it like this. Well, let's just talk about it. Although the code is simple and clear, I 'd like to give it a simple idea.

Is to add a sentinel at the end of the array, even if I is not judged <nIt can also ensure that the array is not out of bounds. IfThe statement is bound to break.

First, judge whether the value of the last element is the number to be searched. If yes, return its subscript. If not, save the value of the last number, the number to be searched is assigned to the last element, and the specified element is searched cyclically. You do not need to determine whether the array is out of bounds. IfStatement must be breakTo restore the value of the last element, and finally only judge I <nIf it is IThat is, the request is obtained. Otherwise, the element to be searched is not in the array.

Finally, we are doing a simple performance test to check whether the search speed can be improved.

The test code is as follows:

Void testfind () {int n = 200000; int * A = new int [N]; A [N-2] = 1; DWORD start =: gettickcount64 (); for (INT I = 0; I <10000; I ++) Find (A, N, 1); DWORD end =: gettickcount64 (); cout <"before optimization: "<End-start <" millisecond "<Endl; Start =: gettickcount64 (); For (INT I = 0; I <10000; I ++) find1 (A, N, 1); End =: gettickcount64 (); cout <"after optimization:" <End-start <"millisecond" <Endl ;}

The running result is as follows:

Faster

Tips for improving search speed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.