for寰幆瀹炴垬鎬ц兘浼樺寲
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了for寰幆瀹炴垬鎬ц兘浼樺寲相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/%e5%bc%80%e5%8f%91' title='寮€鍙?>寮€鍙?/a> nano 瑙掑害 寰幆缁撴瀯 璁$畻 缂撳瓨 ndt 鎬ц兘 缂栧啓
瀹屾垚鍚屾牱鐨勫姛鑳斤紝鐢ㄤ笉鍚岀殑浠g爜鏉ュ疄鐜帮紝鎬ц兘涓婂彲鑳戒細鏈夋瘮杈冨ぇ鐨勫樊鍒紝鎵€浠ュ浜庝竴浜涙€ц兘鏁忔劅鐨勬ā鍧楁潵璇达紝瀵逛唬鐮佽繘琛屼竴瀹氱殑浼樺寲杩樻槸寰堟湁蹇呰鐨勩€備粖澶╁氨鏉ヨ涓€涓媕ava浠g爜浼樺寲鐨勪簨鎯咃紝浠婂ぉ涓昏鑱婁竴涓嬪浜巉or锛坵hile绛夊悓鐞嗭級寰幆鐨勪紭鍖栵紝瀹冧綔涓轰笁澶х粨鏋勪箣涓€鐨勫惊鐜紝鍦ㄦ垜浠紪鍐欎唬鐮佺殑鏃跺€欎細缁忓父鐢ㄥ埌銆傚惊鐜粨鏋勮鎴戜滑鎿嶄綔鏁扮粍銆侀泦鍚堝拰鍏朵粬涓€浜涙湁瑙勫緥鐨勪簨鐗╁彉寰楁洿鍔犵殑鏂逛究锛屼絾鏄鏋滄垜浠湪瀹為檯寮€鍙戝綋涓繍鐢ㄤ笉鍚堢悊锛屽彲鑳戒細缁欑▼搴忕殑鎬ц兘甯︽潵寰堝ぇ鐨勫奖鍝嶃€傛墍浠ユ垜浠繕鏄渶瑕佹帉鎻′竴浜涙妧宸ф潵浼樺寲鎴戜滑鐨勪唬鐮佺殑銆?/span>
1 宓屽寰幆
private static void bigSmall() {
long stratTime = System.nanoTime();
for (int i = 0; i < 10000000; i++) {
for (int j = 0; j < 100; j++) {
}
}
long endTime = System.nanoTime();
System.out.println("澶栧ぇ鍐呭皬鑰楁椂锛? + (endTime - stratTime));
}
涓婇潰鏄ぇ寰幆椹卞姩灏忓惊鐜紝浼樺寲鍚庢敼涓哄皬寰幆椹卞姩澶у惊鐜細
private static void smallBig() {
long stratTime = System.nanoTime();
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 10000000; j++) {
}
}
long endTime = System.nanoTime();
System.out.println("澶栧皬鍐呭ぇ鑰楁椂锛? + (endTime - stratTime));
}
涓よ€呰€楁椂瀵规瘮锛?/span>
澶栧ぇ鍐呭皬鑰楁椂锛?56859726
澶栧皬鍐呭ぇ鑰楁椂锛?/span>451345484
鐢变互涓婂姣斿彲鐭ワ紝浼樺寲鍚庢€ц兘鏄捐憲鎻愬崌銆傚祵濂楀惊鐜簲璇ラ伒寰?ldquo;澶栧皬鍐呭ぇ”鐨勫師鍒欙紝杩欏氨濂芥瘮浣犲鍒跺緢澶氫釜灏忔枃浠跺拰澶嶅埗鍑犱釜澶ф枃浠剁殑鍖哄埆銆?/span>
2 寰幆鍙橀噺鐨勫疄渚嬪寲搴旀斁鍦ㄥ惊鐜
鍦ㄧ1鑺備紭鍖栧悗鐨勪唬鐮佸熀纭€涓婏紝杩涜浜屾浼樺寲锛?/span>
private static void smallBigBetter() {
long stratTime = System.nanoTime();
int i, j;
for (i = 0; i < 100; i++) {
for (j = 0; j < 10000000; j++) {
}
}
long endTime = System.nanoTime();
System.out.println("澶栧皬鍐呭ぇ骞朵笖鎻愬彇鍑哄惊鐜唴鍙橀噺鑰楁椂锛? + (endTime - stratTime));
}
鎵ц缁撴灉锛?/span>
澶栧皬鍐呭ぇ骞朵笖鎻愬彇鍑哄惊鐜唴鍙橀噺鑰楁椂锛?45302240
铏界劧浼樺寲鏁堟灉骞朵笉鏄庢樉锛屼絾鏄殢鐫€寰幆娆℃暟鐨勫鍔狅紝鑰楁椂浼氳秺鏉ヨ秺澶э紝浼樺寲鏁堟灉鍒欎細瓒婃潵瓒婃槑鏄俱€傚垎鏋愶細浼樺寲鍓嶉渶瑕佸疄渚嬪寲1+i=101娆★紝浼樺寲鍚庝粎浠?娆°€?/span>
3 鎻愬彇涓庡惊鐜棤鍏崇殑琛ㄨ揪寮?/span>
private static void calculationInner() {
int a = 3;
int b = 7;
long stratTime = System.nanoTime();
for (int i = 0; i < 10000000; i++) {
i = i * a * b;
}
long endTime = System.nanoTime();
System.out.println("鏈彁鍙栬€楁椂锛? + (endTime - stratTime));
}
浼樺寲鍚庯細
private static void calculationOuter() {
int a = 3;
int b = 7;
int c = a * b;
long stratTime = System.nanoTime();
for (int i = 0; i < 10000000; i++) {
i = i * c;
}
long endTime = System.nanoTime();
System.out.println("宸叉彁鍙栬€楁椂锛? + (endTime - stratTime));
}
浠g爜涓璦*b涓庡惊鐜棤鍏筹紝鎵€浠ュ簲璇ユ妸瀹冩斁鍒板闈紝閬垮厤閲嶅璁$畻銆備粠鐞嗚瑙掑害鍒嗘瀽锛岀敱浜庡噺灏戜簡璁$畻娆℃暟锛屾晠浼樺寲鍚庢€ц兘浼氭洿楂樸€?/span>
4 娑堥櫎寰幆缁堟鍒ゆ柇鏃剁殑鏂规硶璋冪敤
stratTime = System.nanoTime();
for (int i = 0; i < list.size(); i++) {
}
endTime = System.nanoTime();
System.out.println("鏈紭鍖杔ist鑰楁椂锛?+(endTime - stratTime));
浼樺寲鍚庣殑浠g爜锛?/span>
stratTime = System.nanoTime();
int size = list.size();
for (int i = 0; i < size; i++) {
}
endTime = System.nanoTime();
System.out.println("浼樺寲list鑰楁椂锛?+(endTime - stratTime));
姣忔寰幆锛宭ist.size()閮戒細琚墽琛屼竴娆★紝杩欐棤鐤戜細褰卞搷绋嬪簭鐨勬€ц兘锛屾墍浠ュ簲璇ュ皢鍏舵斁鍒板惊鐜闈紝鐢ㄤ竴涓彉閲忔潵缂撳瓨鍏秙ize锛屼笉瑕佽杩欎竴鐐圭偣浠g爜鑰屾秷鑰楁垜浠繖涔堝鎬ц兘銆?/span>
5 寮傚父鎹曡幏
鍦ㄥ唴閮ㄦ崟鑾峰紓甯革細
private static void catchInner() {
long stratTime = System.nanoTime();
for (int i = 0; i < 10000000; i++) {
try {
} catch (Exception e) {
}
}
long endTime = System.nanoTime();
System.out.println("鍦ㄥ唴閮ㄦ崟鑾峰紓甯歌€楁椂锛? + (endTime - stratTime));
}
鍦ㄥ閮ㄦ崟鑾峰紓甯革細
private static void catchOuter() {
long stratTime = System.nanoTime();
try {
for (int i = 0; i < 10000000; i++) {
}
} catch (Exception e) {
}
long endTime = System.nanoTime();
System.out.println("鍦ㄥ閮ㄦ崟鑾峰紓甯歌€楁椂锛? + (endTime - stratTime));
}
缁撴灉瀵规瘮锛?/span>
鍦ㄥ唴閮ㄦ崟鑾峰紓甯歌€楁椂锛?105716
鍦ㄥ閮ㄦ崟鑾峰紓甯歌€楁椂锛?/span>5465381
鎹曡幏寮傚父寰堝崰鐢ㄨ祫婧愶紝鎵€浠ヤ笉瑕佹妸try catch鏀惧埌寰幆鍐呴儴锛屼紭鍖栧悗鎬ц兘鍚屾牱鏈夊ソ鍑犱釜鏁伴噺绾х殑鎻愬崌銆傚彟澶栵紝 銆奅ffective Java銆?/strong>涓€涔︽寚鍑?strong>for-each寰幆浼樺厛浜庝紶缁熺殑for寰幆锛屽畠鍦ㄧ畝娲佹€у拰棰勯槻bug鏂归潰鏈夌潃浼犵粺for寰幆鏃犳硶濯茬編鐨勪紭鍔匡紝骞朵笖锛屾病鏈夋€ц兘鏂归潰鐨勬崯澶憋紝鍥犳锛屾帹鑽愪娇鐢╢or-each寰幆銆?/p>
以上是关于for寰幆瀹炴垬鎬ц兘浼樺寲的主要内容,如果未能解决你的问题,请参考以下文章