어제 강의를 듣는 중에 강사님께서 Shift 연산이 일반 나누는 연산보다 속도가 더 빠르다고 하셔서 얼마나 차이가 나길래… 라는 생각으로 코딩을 해봤습니다.
반복횟수를 적게 하니 소요 시간 차이가 거의 없어 반복 횟수를 1,000,000,000번 했습니다.
Result
171 2044 2057 2047 2028 2028 2012 2012 2013 2028
mod – 1844.0
16 0 0 0 0 0 0 0 0 0
Bit – 1.6
Test 환경
Java Version : jdk 1.6
Java IDE : Eclipse Indigo Release
CPU : Inter i7-2600K 3.4GHz
Memory : 8.0GB
OS : Windows 7 64Bit
Source
1: int i = Integer.MAX_VALUE;2: int temp = 0;3: long[] result = new long[10];4: long[] result2 = new long[10];5: for (int j = 0; j < 10; j++) {6: long startTime = System.currentTimeMillis();7: for (int t = 0; t < 1000000000; t++) {8: temp /= 32;9: temp %= 32;10: }11: result[j] = System.currentTimeMillis() - startTime;12: i = Integer.MAX_VALUE;13: startTime = System.currentTimeMillis();14: for (int t = 0; t < 1000000000; t++) {15: temp >>= 5;16: temp &= 31;17: }18: result2[j] = System.currentTimeMillis() - startTime;19: }20: long sum = 0;21: for (int j = 0; j < result.length; j++) {22: sum += result[j];23: System.out.print(result[j] + " ");24: }25: System.out.println("\n mod - " + sum / 10.0);26: sum = 0;27: for (int j = 0; j < result2.length; j++) {28: sum += result2[j];29: System.out.print(result2[j] + " ");30: }31: System.out.println("\n Bit - " + sum / 10.0);
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }