As3 code optimization skills

Source: Internet
Author: User

Code of this post
Efficiency Testing is based on the framework provided by this post.
Run:
Http: // bbs.9ria
. Com/thread-57015-1-2.html


Test environment: flashcs5 professional

System: windowsxp_sp2
CPU: AMD athlon (TM) 64 processor 1.81 GHz
Memory: 2.50 GB

All firewalls are disabled

Basic principles for optimization of mathematical code:
1) bitwise operations and conditional operations are faster than four operations. addition and subtraction are faster than multiplication and division. multiplication and division are faster than modulo operations. integers are faster than floating-point operations, and INT is faster than uint operations.
2) math functions are mostly slow, and should be replaced by lower-level operators or native functions as much as possible.
3) In some mathematical operations, it is more convenient and readable to use string functions, but it is not recommended for efficiency-first scenarios.

This post discusses three integer algorithms.
(Down, rounding, up), the next post explores the algorithm optimization of the sine function.

1. Obtain the entire function:

You can use math. Floor, int and bitwise operations to perform down Rounding:

Function mathfloor (_ num: Number): int {// math. Floor
Return math. Floor (_ num );
}
Function intfloor (_ num: Number): int {// int
Return int (_ num );
}
Function bitleftfloor (_ num: Number): int {// left shift operation
Return (_ num) <0;
}
Function bitrightfloor (_ num: Number): int {// right shift operation
Return (_ num)> 0;
}

Function convert (_ num: Number): int {// direct return value, only for type conversion
Return _ num;
}



---------- Efficiency test started ----------
Math. Floor takes 300000 milliseconds to execute the entire function.
It takes 300000 milliseconds for int to take the whole function to execute 142 times.
It takes 300000 milliseconds for the left displacement to take the whole function to execute 141 times.
It takes 300000 milliseconds for the right displacement to take the whole function to execute 134 times.
It takes 300000 ms to execute the entire function for 135 times.
---------- Efficiency test ended ----------

---------- Efficiency test started ----------

Math. Floor takes 300000 milliseconds to execute the entire function.
It takes 300000 milliseconds for int to take the whole function to execute 164 times.
It takes 300000 milliseconds for the left displacement to take the whole function to execute 138 times.
It takes 300000 milliseconds for the right displacement to take the whole function to execute 127 times.
It takes 300000 ms to execute the entire function for 126 times.
---------- Efficiency test ended ----------

---------- Efficiency test started ----------

Math. Floor takes 300000 milliseconds to execute the entire function.
It takes 300000 milliseconds for int to take the whole function to execute 138 times.
It takes 300000 milliseconds for the left displacement to take the whole function to execute 145 times.
It takes 300000 milliseconds for the right displacement to take the whole function to execute 151 times.
It takes 300000 ms to execute the entire function for 137 times.
---------- Efficiency test ended ----------

Apparently, math. floor is the slowest, while int and bit operations are basically close to the direct conversion speed. In as3, int functions are not integer operations, but type conversion, so the speed is not inferior to bit operations.

Rounding to an integer. If you do not want to use math. round, you can add 0.5 to the current value and then round down the value:

Function mathround (_ num: Number): int {
Return math. Round (_ num );
}

Function intround (_ num: Number): int {
Return int (_ num + 0.5 );
}

Function bitleftround (_ num: Number): int {
Return (_ num + 0.5) <0;
}

Function bitrightround (_ num: Number): int {
Return (_ num + 0.5)> 0;
}

Function convertround (_ num: Number): int {
Return _ num + 0.5;
}



---------- Efficiency test started ----------
Math. Round rounding the function to execute 300000 times takes 217 milliseconds
It takes 300000 milliseconds for int to take the whole function to execute 131 times.
It takes 300000 milliseconds for the left-shift rounding function to execute 155 times
It takes 300000 milliseconds for the right-shift rounding function to execute 138 times
It takes 300000 milliseconds to perform the type conversion rounding function 157 times
---------- Efficiency test ended ----------

---------- Efficiency test started ----------

Math. Round rounding the function to execute 300000 times takes 222 milliseconds
It takes 300000 milliseconds for int to take the whole function to execute 141 times.
It takes 300000 milliseconds for the left-shift rounding function to execute 137 times
It takes 300000 milliseconds for the right-shift rounding function to execute 132 times
It takes 300000 milliseconds to perform the type conversion rounding function 142 times
---------- Efficiency test ended ----------

---------- Efficiency test started ----------

Math. Round rounding the function to execute 300000 times takes 268 milliseconds
It takes 300000 milliseconds for int to take the whole function to execute 188 times.
It takes 300000 milliseconds for the left-shift rounding function to execute 166 times
It takes 300000 milliseconds for the right-shift rounding function to execute 170 times
It takes 300000 milliseconds to perform the type conversion rounding function 168 times
---------- Efficiency test ended ----------

The result is similar to the rounded down integer.

It is relatively difficult to integer the number up. You need to check whether the current number is an integer. You can also use int (Value + 1-number.min_value) instead. Note: Number. min_value is a constant and does not have to worry about affecting efficiency.

Function mathceil (_ num: Number): int {
Return math. Ceil (_ num );
}

Function intceilcondition (_ num: Number): int {
Return (INT (_ num) =__ num )? (_ Num): int (_ num + 1 );
}

Function intceil (_ num: Number): int {
Return int (_ num + 1-number.min_value );
}

Function bitleftceilcondition (_ num: Number): int {
Return (_ num <0) =__ num )? (_ Num): int (_ num + 1) <0 );
}

Function bitleftceil (_ num: Number): int {
Return (_ num + 1-number.max_value) <0;
}

Function bitrightceilcondition (_ num: Number): int {
Return (_ num> 0) =__ num )? (_ Num): int (_ num + 1)> 0 );
}

Function bitrightceil (_ num: Number): int {
Return (_ num + 1-number.max_value)> 0;
}


-------- Test result of passing decimals as parameters ---------
---------- Efficiency test started ----------
Math. Ceil the time it takes to execute the entire function for 300000 times is 217 milliseconds.
It takes 300000 milliseconds for the condition + int to take the whole function to execute 154 times.
Int + number. min_value takes 300000 milliseconds to execute the entire function.
It takes 300000 milliseconds to execute the entire function with the left displacement + condition.
The left displacement + number. min_value takes 300000 milliseconds to execute the entire function.
It takes 300000 milliseconds for the right displacement + condition to take the whole function to execute 151 times.
The right displacement + number. min_value takes 300000 milliseconds to execute the entire function.
---------- Efficiency test ended ----------

---------- Efficiency test started ----------

Math. Ceil the time it takes to execute the entire function for 300000 times is 199 milliseconds.
It takes 300000 milliseconds for the condition + int to take the whole function to execute 155 times.
Int + number. min_value takes 300000 milliseconds to execute the entire function.
It takes 300000 milliseconds to execute the entire function with the left displacement + condition.
The left displacement + number. min_value takes 300000 milliseconds to execute the entire function.
It takes 300000 milliseconds for the right displacement + condition to take the whole function to execute 150 times.
The right displacement + number. min_value takes 300000 milliseconds to execute the entire function.
---------- Efficiency test ended ----------

---------- Efficiency test started ----------

Math. Ceil the time it takes to execute the entire function for 300000 times is 215 milliseconds.
It takes 300000 milliseconds for the condition + int to take the whole function to execute 150 times.
Int + number. min_value takes 300000 milliseconds to execute the entire function.
It takes 300000 milliseconds to execute the entire function with the left displacement + condition.
The left displacement + number. min_value takes 300000 milliseconds to execute the entire function.
It takes 300000 milliseconds for the right displacement + condition to take the whole function to execute 149 times.
The right displacement + number. min_value takes 300000 milliseconds to execute the entire function.
---------- Efficiency test ended ----------
--------***********---------

-------- Test result of passing an integer as a parameter ---------

---------- Efficiency test started ----------
Math. Ceil the time it takes to execute the entire function for 300000 times is 216 milliseconds.
It takes 300000 milliseconds for the condition + int to take the whole function to execute 164 times.
Int + number. min_value takes 300000 milliseconds to execute the entire function.
It takes 300000 milliseconds to execute the entire function with the left displacement + condition.
The left displacement + number. min_value takes 300000 milliseconds to execute the entire function.
It takes 300000 milliseconds for the right displacement + condition to take the whole function to execute 146 times.
The right displacement + number. min_value takes 300000 milliseconds to execute the entire function.
---------- Efficiency test ended ----------

---------- Efficiency test started ----------

Math. Ceil the time it takes to execute the entire function for 300000 times is 225 milliseconds.
It takes 300000 milliseconds for the condition + int to take the whole function to execute 157 times.
Int + number. min_value takes 300000 milliseconds to execute the entire function.
It takes 300000 milliseconds to execute the entire function with the left displacement + condition.
The left displacement + number. min_value takes 300000 milliseconds to execute the entire function.
It takes 300000 milliseconds for the right displacement + condition to take the whole function to execute 152 times.
The right displacement + number. min_value takes 300000 milliseconds to execute the entire function.
---------- Efficiency test ended ----------

---------- Efficiency test started ----------

Math. Ceil the time it takes to execute the entire function for 300000 times is 224 milliseconds.
It takes 300000 milliseconds for the condition + int to take the whole function to execute 150 times.
Int + number. min_value takes 300000 milliseconds to execute the entire function.
It takes 300000 milliseconds to execute the entire function with the left displacement + condition.
The left displacement + number. min_value takes 300000 milliseconds to execute the entire function.
It takes 300000 milliseconds for the right displacement + condition to take the whole function to execute 184 times.
The right displacement + number. min_value takes 300000 milliseconds to execute the entire function.
---------- Efficiency test ended ----------
--------***********---------

When passing decimal numbers as parameters, min_value is similar to the conditional operation result. When passing integers, the conditional operation speed is slightly higher than min_value. The difference between the two algorithms is that the former has one more step and the latter has one more subtraction operation. Therefore, this result is also a manifestation of a condition over the four arithmetic operations. Therefore, in practical application
, We still recommend that you use conditional operators to perform rounded up.

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.