This time, I focus on for loop and repeating code.
FunctionLength
Test code:
void test_xxxxx_speed(int x){ x+=x; x+=x; ... (repeat xxx times) x+=x; return x; }
Samsung Galaxy Note 2 run:
Linux OpenJDK 7 run:
com.luzi82.codeindex.FunctionLength START test_00001_speed: 10.3M lines, 96.8ns/line test_00010_speed: 103M lines, 9.67ns/line test_00100_speed: 658M lines, 1.52ns/line test_01000_speed: 1.33G lines, 751ps/line test_10000_speed: 141M lines, 7.06ns/line com.luzi82.codeindex.FunctionLength END
Observation:
- In Android, more repeating code is good.
- In OpenJDK, 1000 repeating code is good, 10000 repeating code is bad.
- Compiler does not allow me to do 100000 repeating code.
ForLoop
Test code:
void test_forloop_xxx_yy(){ // when xxx=imm, use i-- // when xxx=ipp, use i++ // when xxx=mmi, use --i // when xxx=ppi, use ++i // when yy=gt/lt, use > / < // when yy=ne, use != for(int i=0;i<1000;++i){ x+=x; } return x; } void test_repeat(){ x+=x; x+=x; ... (repeat 1000 times) x+=x; return x; }
Samsung Galaxy Note 2 run:
Linux OpenJDK 7 run:
com.luzi82.codeindex.ForLoop START test_forloop_imm_gt: 10.5G line/s, 94.6ps/line test_forloop_imm_ne: 10.0G line/s, 99.4ps/line test_forloop_ipp_lt: 10.5G line/s, 95.1ps/line test_forloop_ipp_ne: 10.2G line/s, 98.0ps/line test_forloop_mmi_gt: 10.0G line/s, 99.7ps/line test_forloop_mmi_ne: 10.1G line/s, 98.8ps/line test_forloop_ppi_lt: 10.4G line/s, 95.4ps/line test_forloop_ppi_ne: 10.0G line/s, 99.0ps/line test_repeat: 1.32G line/s, 759ps/line com.luzi82.codeindex.ForLoop END
Observation:
- In for-loop, using i++ / ++i / i-- / --i does not affect much.
- In for-loop, using i<MAX / i!=MAX does not affect much.
- In Android, repeating code is faster.
- In OpenJDK, for-loop is faster.