Some tips for improving algorithmic efficiency

Source: Internet
Author: User
Tags variable scope

1, good at writing variable scope, the use of variables released in time

{

int a = 0;

A range of operations

}//When executed here, a in curly braces is released.

2, the former self-increase self-reduction is slightly higher than the self-reduction efficiency.

++i Efficiency > i++

-I. Efficiency > i--

3. if (conditional expression)

A, the conditional expression here can be calculated first, get a result, put into if () parentheses inside;

B, can also be placed directly inside ()

Recommendation: where appropriate, conditional expressions are placed directly inside the parentheses () because: (The circuit breaker of a logical operation expression ^_^)

Logical Expression,&& | | , sometimes the left-hand value can represent the value of the entire expression, and the expression on the right is no longer calculated, saving time.

4, can use 1 if to try not to use 2 if

Such as: can use

if () {}

else {}

Or

if () {}

else if () {}

else {} Try not to use

if () {}

if () {}

Because, 2 if is to judge 2 times, and if (else if) else structure only need to judge a condition satisfies, other condition not to judge.

Some tips for improving algorithmic efficiency

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.