25-10-2018, 08:26 PM
اقتباس من الموضوع الأصلي لـ GamingMasteR
[b]-----------------------------------------------
Use Initialization Lists
Always use initialization lists in constructors. For example, use
[/b]
كود PHP:
rather than
كود PHP:
Without initialization lists, the variable's default constructor is invoked behind-the-scenes prior to the class's constructor, then its assignment operator is invoked. With initialization lists, only the copy constructor is invoked.
[b]-----------------------------------------------
Optimize For Loops
Wherever possible, count down to zero rather than up to n. For example, use
[/b]
كود PHP:
rather than
كود PHP:
The test is done every iteration and it's faster to test against zero than anything else. Note also that
كود PHP:
is faster than
كود PHP:
when it appears in the third part of the for loop statement.
[b]-----------------------------------------------
Use 'int'
Always use the int data type instead of char or short wherever possible. int is always the native type for the machine.
-----------------------------------------------
Make Local Functions Static
Always declare local functions as static, e.g.,
[/b]
كود PHP:
This means they will not be visible to functions outside the .cpp file, and some C++ compilers can take advantage of this in their optimizations.
[b]-----------------------------------------------
Optimize If Statements
Factor out jumps. For example, use
[/b]
كود PHP:
rather than
كود PHP:
Use a profiler and good judgement to decide if undoing the bar() operation is faster than jumping.
[b]-----------------------------------------------
Optimize Switch Statements
Put the most common cases first.
-----------------------------------------------
Avoid Expensive Operations
Addition is cheaper than multiplication and multiplication is cheaper than division. Factor out expensive operations wherever possible.
-----------------------------------------------
Initialize on Declaration
Wherever possible, initialize variables at the time they're declared. For example,
[/b]
كود PHP:
is faster than
كود PHP:
Declaration then initialization invokes the object's default constructor then its assignment operator. Initializing in the declaration invokes only its copy constructor.
[b]-----------------------------------------------
Pass By Reference
Always try to pass classes by reference rather than by value. For example, use
[/b]
كود PHP:
rather than
كود PHP:
[b]-----------------------------------------------
Delay Variable Declarations
Leave variable declarations right until the point when they're needed. Remember that when a variable is declared its constructor is called. This is wasteful if the variable is not used in the current scope.
-----------------------------------------------
Use 'op='
Wherever possible, use 'op=' in favour of 'op'. For example, use
[/b]
كود PHP:
rather than
كود PHP:
The first version is better than the second because it avoids creating a temporary object.
[b]-----------------------------------------------
Inline Small Functions
Small, performance critical functions should be inlined using the inline keyword, e.g.,
[/b]
كود PHP:
This causes the compiler to duplicate the body of the function in the place it was called from. Inlining large functions can cause cache misses resulting in slower execution times.
[b]-----------------------------------------------
Use Nameless Objects
Wherever possible, use nameless objects. For example,
[/b]
كود PHP:
is faster than
كود PHP:
because, in the first case, the parameter and the object share memory
السلام عليكم
وجدت صفحة مكتوب بها هذا الموضوع عن تحسين طريقة كتابة كود السي :
وجدت صفحة مكتوب بها هذا الموضوع عن تحسين طريقة كتابة كود السي :
[b]-----------------------------------------------
Use Initialization Lists
Always use initialization lists in constructors. For example, use
[/b]
كود PHP:
rather than
كود PHP:
Without initialization lists, the variable's default constructor is invoked behind-the-scenes prior to the class's constructor, then its assignment operator is invoked. With initialization lists, only the copy constructor is invoked.
[b]-----------------------------------------------
Optimize For Loops
Wherever possible, count down to zero rather than up to n. For example, use
[/b]
كود PHP:
rather than
كود PHP:
The test is done every iteration and it's faster to test against zero than anything else. Note also that
كود PHP:
is faster than
كود PHP:
when it appears in the third part of the for loop statement.
[b]-----------------------------------------------
Use 'int'
Always use the int data type instead of char or short wherever possible. int is always the native type for the machine.
-----------------------------------------------
Make Local Functions Static
Always declare local functions as static, e.g.,
[/b]
كود PHP:
This means they will not be visible to functions outside the .cpp file, and some C++ compilers can take advantage of this in their optimizations.
[b]-----------------------------------------------
Optimize If Statements
Factor out jumps. For example, use
[/b]
كود PHP:
rather than
كود PHP:
Use a profiler and good judgement to decide if undoing the bar() operation is faster than jumping.
[b]-----------------------------------------------
Optimize Switch Statements
Put the most common cases first.
-----------------------------------------------
Avoid Expensive Operations
Addition is cheaper than multiplication and multiplication is cheaper than division. Factor out expensive operations wherever possible.
-----------------------------------------------
Initialize on Declaration
Wherever possible, initialize variables at the time they're declared. For example,
[/b]
كود PHP:
is faster than
كود PHP:
Declaration then initialization invokes the object's default constructor then its assignment operator. Initializing in the declaration invokes only its copy constructor.
[b]-----------------------------------------------
Pass By Reference
Always try to pass classes by reference rather than by value. For example, use
[/b]
كود PHP:
rather than
كود PHP:
[b]-----------------------------------------------
Delay Variable Declarations
Leave variable declarations right until the point when they're needed. Remember that when a variable is declared its constructor is called. This is wasteful if the variable is not used in the current scope.
-----------------------------------------------
Use 'op='
Wherever possible, use 'op=' in favour of 'op'. For example, use
[/b]
كود PHP:
rather than
كود PHP:
The first version is better than the second because it avoids creating a temporary object.
[b]-----------------------------------------------
Inline Small Functions
Small, performance critical functions should be inlined using the inline keyword, e.g.,
[/b]
كود PHP:
This causes the compiler to duplicate the body of the function in the place it was called from. Inlining large functions can cause cache misses resulting in slower execution times.
[b]-----------------------------------------------
Use Nameless Objects
Wherever possible, use nameless objects. For example,
[/b]
كود PHP:
is faster than
كود PHP:
because, in the first case, the parameter and the object share memory
implicit conversion (think forward)