java學(xué)習(xí)

          java學(xué)習(xí)

           

          java程序性能優(yōu)化2

          六、避免不需要的instanceof操作 如果左邊的對象的靜態(tài)類型等于右邊的,instanceof表達(dá)式返回永遠(yuǎn)為true。 例子: public class uiso { public uiso () {} } class dog extends uiso { void method (dog dog, uiso u) { dog d = dog;if (d instanceof uiso) // always true. system.out.println("dog is a uiso"); uiso uiso = u; if (uiso instanceof object) // always true. system.out.println("uiso is an object"); } } 更正: 刪掉不需要的instanceof操作。 class dog extends uiso { void method () { dog d; system.out.println ("dog is an uiso"); system.out.println ("uiso is an uiso"); } }
          七、避免不需要的造型操作 所有的類都是直接或者間接繼承自object。同樣,所有的子類也都隱含的“等于”其父類。那么,由子類造型至父類的操作就是不必要的了。 例子: class unc { string _id = "unc"; } class dog extends unc { void method () { dog dog = new dog (); unc animal = (unc)dog; // not necessary. object o = (object)dog; // not necessary. } } 更正: class dog extends unc { void method () { dog dog = new dog(); unc animal = dog; object o = dog; } }
          八、如果只是查找單個字符的話,用charat()代替startswith()
          用一個字符作為參數(shù)調(diào)用startswith()也會工作的很好,但從性能角度上來看,調(diào)用用string api無疑是錯誤的! 例子: public class pcts { private void method(string s) { if (s.startswith("a")) { // violation // ... } } } 更正 將'startswith()' 替換成'charat()'. public class pcts { private void method(string s) { if ('a' == s.charat(0)) { // ... } } }
          九、使用移位操作來代替'a / b'操作 "/"是一個很“昂貴”的操作,使用移位操作將會更快更有效。 例子: public class sdiv { public static final int num = 16; public void calculate(int a) { int div = a / 4; // should be replaced with "a >> 2". int div2 = a / 8; // should be replaced with "a >> 3". int temp = a / 3; } } 更正: public class sdiv { public static final int num = 16; public void calculate(int a) { int div = a >> 2; int div2 = a >> 3; int temp = a / 3; // 不能轉(zhuǎn)換成位移操作 } }
          十、使用移位操作代替'a * b'
          同上。 [i]但我個人認(rèn)為,除非是在一個非常大的循環(huán)內(nèi),性能非常重要,而且你很清楚你自己在做什么,方可使用這種方法。否則提高性能所帶來的程序晚讀性的降低將是不合算的。 例子: public class smul { public void calculate(int a) { int mul = a * 4; // should be replaced with "a << 2". int mul2 = 8 * a; // should be replaced with "a << 3". int temp = a * 3; } } 更正: package opt; public class smul { public void calculate(int a) { int mul = a << 2; int mul2 = a << 3; int temp = a * 3; // 不能轉(zhuǎn)換 } }
          十一、在字符串相加的時候,使用 ' ' 代替 " ",如果該字符串只有一個字符的話 例子: public class str { public void method(string s) { string string = s + "d" // violation. string = "abc" + "d" // violation. } } 更正: 將一個字符的字符串替換成' ' public class str { public void method(string s) { string string = s + 'd' string = "abc" + 'd' } }
          十二、將try/catch塊移出循環(huán) 把try/catch塊放入循環(huán)體內(nèi),會極大的影響性能,如果編譯jit被關(guān)閉或者你所使用的是一個不帶jit的jvm,性能會將下降21%之多! 例子: import java.io.fileinputstream; public class try { void method (fileinputstream fis) { for (int i = 0; i < size; i++) { try { // violation _sum += fis.read(); } catch (exception e) {} } } private int _sum; } 更正: 將try/catch塊移出循環(huán) void method (fileinputstream fis) { try { for (int i = 0; i < size; i++) {
          _sum += fis.read(); } } catch (exception e) {} }
          十三、對于boolean值,避免不必要的等式判斷 將一個boolean值與一個true比較是一個恒等操作(直接返回該boolean變量的值). 移走對于boolean的不必要操作至少會帶來2個好處: 1)代碼執(zhí)行的更快 (生成的字節(jié)碼少了5個字節(jié)); 2)代碼也會更加干凈 。 例子: public class ueq { boolean method (string string) { return string.endswith ("a") == true; // violation } } 更正: class ueq_fixed { boolean method (string string) { return string.endswith ("a"); } }
          十四、對于常量字符串,用'string' 代替 'stringbuffer' 常量字符串并不需要動態(tài)改變長度。 例子: public class usc { string method () { stringbuffer s = new stringbuffer ("hello"); string t = s + "world!"; return t; } } 更正: 把stringbuffer換成string,如果確定這個string不會再變的話,這將會減少運行開銷提高性能。
          十五、使用條件操作符替代"if (cond) return; else return;" 結(jié)構(gòu) 條件操作符更加的簡捷 例子: public class if { public int method(boolean isdone) { if (isdone) { return 0; } else { return 10; } } } 更正: public class if { public int method(boolean isdone) { return (isdone ? 0 : 10); } }
          十六、不要在循環(huán)體中實例化變量 在循環(huán)體中實例化臨時變量將會增加內(nèi)存消耗 例子: import java.util.vector; public class loop { void method (vector v) { for (int i=0;i < v.size();i++) { object o = new object(); o = v.elementat(i); } } } 更正: 在循環(huán)體外定義變量,并反復(fù)使用 import java.util.vector; public class loop { void method (vector v) { object o; for (int i=0;i<v.size();i++) { o = v.elementat(i); } } }

          posted @ 2013-02-22 14:16 楊軍威 閱讀(202) | 評論 (0)編輯 收藏

          java程序性能優(yōu)化

          一、避免在循環(huán)條件中使用復(fù)雜表達(dá)式 在不做編譯優(yōu)化的情況下,在循環(huán)中,循環(huán)條件會被反復(fù)計算,如果不使用復(fù)雜表達(dá)式,而使循環(huán)條件值不變的話,程序?qū)\行的更快。 例子: import java.util.vector; class cel { void method (vector vector) { for (int i = 0; i < vector.size (); i++// violation ; //  } } 更正: class cel_fixed { void method (vector vector) { int size = vector.size () for (int i = 0; i < size; i++) ; //  } }
          二、為'vectors' 和 'hashtables'定義初始大小 jvm為vector擴(kuò)充大小的時候需要重新創(chuàng)建一個更大的數(shù)組,將原原先數(shù)組中的內(nèi)容復(fù)制過來,最后,原先的數(shù)組再被回收??梢妚ector容量的擴(kuò)大是一個頗費時間的事。 通常,默認(rèn)的10個元素大小是不夠的。你最好能準(zhǔn)確的估計你所需要的最佳大小。 例子: import java.util.vector;
           
          public class dic { public void addobjects (object[] o) { // if length > 10, vector needs to expand for (int i = 0; i< o.length;i++) { v.add(o); // capacity before it can add more elements. } } public vector v = new vector(); // no initialcapacity. } 更正: 自己設(shè)定初始大小。 public vector v = new vector(20); public hashtable hash = new hashtable(10);

          三、在finally塊中關(guān)閉stream 程序中使用到的資源應(yīng)當(dāng)被釋放,以避免資源泄漏。這最好在finally塊中去做。不管程序執(zhí)行的結(jié)果如何,finally塊總是會執(zhí)行的,以確保資源的正確關(guān)閉。 例子: import java.io.*public class cs { public static void main (string args[]) { cs cs = new cs (); cs.method (); } public void method () { try { fileinputstream fis = new fileinputstream ("cs.java"); int count = 0while (fis.read () != -1) count++; system.out.println (count); fis.close (); } catch (filenotfoundexception e1) { } catch (ioexception e2) { } } } 更正: 在最后一個catch后添加一個finally塊

          四、使用'system.arraycopy ()'代替通過來循環(huán)復(fù)制數(shù)組 'system.arraycopy ()' 要比通過循環(huán)來復(fù)制數(shù)組快的多。 例子: public class irb { void method () { int[] array1 = new int [100]; for (int i = 0; i < array1.length; i++) { array1 [i] = i; } int[] array2 = new int [100]; for (int i = 0; i < array2.length; i++) { array2 [i] = array1 [i]; // violation } } } 更正: public class irb{ void method () { int[] array1 = new int [100]; for (int i = 0; i < array1.length; i++) { array1 [i] = i; } int[] array2 = new int [100]; system.arraycopy(array1, 0, array2, 0, 100); } }

          五、讓訪問實例內(nèi)變量的getter/setter方法變成”final” 簡單的getter/setter方法應(yīng)該被置成final,這會告訴編譯器,這個方法不會被重載,所以,可以變成”inlined” 例子: class maf { public void setsize (int size) { _size = size; } private int _size; } 更正: class daf_fixed { final public void setsize (int size) { _size = size; } private int _size; }

          posted @ 2013-02-22 14:06 楊軍威 閱讀(221) | 評論 (0)編輯 收藏

          checkbox選擇

          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
          <html xmlns="http://www.w3.org/1999/xhtml"> 
          <head> 
          <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
          <style type="text/css" media="all"> 
          label{ 
          cursor:pointer; 
          font-size:12px; 
          margin:0px 2px 0px 0px; 
          color:#2B86BD; 
          .d0{ 
          margin-bottom:30px; 
          .d0 input{ 
          cursor:pointer; 
          margin:0px; 
          padding:0px 2px; 
          </style> 
          <script language="javascript" type="text/javascript"> 
          var dr=document.getElementsByTagName("div"),i,t=""; 
          function submit1(num,type){ 
          t=""; 
          var dri=dr[num].getElementsByTagName("input"); 
          for(i=0;i<dri.length;i++){ 
          if(dri[i].checked){ 
          if(type==0){ 
          alert(dri[i].value); 
          break; 
          }else{ 
          t=t+dri[i].value+";"; 
          if(type==1) alert(t); 
          //ChangeSelect 
          submit1.allselect=function(){ 
          var drc=dr[1].getElementsByTagName("input"); 
          for(i=0;i<drc.length;i++){ 
          drc[i].checked=true; 
          //allNot 
          submit1.allNot=function(){ 
          var drc=dr[1].getElementsByTagName("input"); 
          for(i=0;i<drc.length;i++){ 
          drc[i].checked=false; 
          //reverse 
          submit1.reverseSelect=function(){ 
          var drc=dr[1].getElementsByTagName("input"); 
          for(i=0;i<drc.length;i++){ 
          if(drc[i].checked){ 
          drc[i].checked=false; 
          }else{ 
          drc[i].checked=true; 
          </script> 
          <title>js獲取單選框、復(fù)選框的值及操作</title> 
          </head> 
          <body> 
          <div class="d0"> 
          <input type="radio" name="day" id="r0" value="前天"/><label for="r0">前天</label> 
          <input type="radio" name="day" id="r1" value="昨天"/><label for="r1">昨天</label> 
          <input type="radio" name="day" id="r2" checked="checked" value="今天"/><label for="r2">今天</label> 
          <input type="radio" name="day" id="r3" value="明天"/><label for="r3">明天</label> 
          <input type="radio" name="day" id="r4" value="后天"/><label for="r4">后天</label> 
          <button type="button" onclick="submit1(0,0)" >提交</button> 
          </div> 
          <div> 
          <input type="checkbox" value="前年" onclick="alert(this.value);"/><label>前年</label> 
          <input type="checkbox" value="去年" onclick="submit1(1,1);"/><label>去年</label> 
          <input type="checkbox" value="今年" /><label>今年</label> 
          <input type="checkbox" value="明年"/><label>明年</label> 
          <input type="checkbox" value="后年"/><label>后年</label> 
          <button type="button" onclick="submit1(1,1)" >提交</button> 
          <button type="button" onclick="submit1.allselect()" >全選</button> 
          <button type="button" onclick="submit1.reverseSelect()" >反選</button> 
          <button type="button" onclick="submit1.allNot()" >全不選</button> 
          </div> 
          </body> 
          </html> 

          posted @ 2013-02-20 12:57 楊軍威 閱讀(404) | 評論 (0)編輯 收藏

          ext向后臺傳隱藏的id值

          var stroeName = new Ext.data.JsonStore({
                  autoLoad : true,
                  url : "BookAction_getName.action",
                  root : "options",
                  fields : [
                            'name','id'
                            ]
                  
              });

              
              var state = new Ext.form.ComboBox({
                  name : 'name',
                  fieldLabel : '圖書名',
                  allowBlank : false,
                  blankText : '請選擇',
                  emptyText : '請選擇',
                  editable : false,
                  triggerAction : 'all',
                  store : stroeName,
                  //加載本地數(shù)據(jù)框
                  mode : 'local',
                  displayField : 'name',
                  valueField : 'id',
                  hiddenName :'id',
                  //hiddenName :'id',
                  width : 125
              });

          posted @ 2013-01-30 13:24 楊軍威 閱讀(287) | 評論 (0)編輯 收藏

          Niagara采集溫濕度和控制燈的亮滅

          1.在采集溫濕度數(shù)據(jù)時,現(xiàn)在config下建立溫濕度的文件夾(view),

          2.在此文件夾下新建立NumericWritablewen節(jié)點,在view中建立kitPx中的Bargraph柱狀圖,編輯對應(yīng)wen節(jié)點中out中的value,

          3.在控制燈的開關(guān)時在kitPx中選擇ActionButton,為每個燈選擇2個ActionButton,一個開,一個關(guān),編輯開的開關(guān)選擇此燈節(jié)點中的emergencyActive,編輯關(guān)的開關(guān)選擇此燈節(jié)點中的emergencyInactive,

          4.Palette中找iopt,找host,,在config下建立iopt文件夾,Local Port設(shè)置6800,Dest Port設(shè)置1025

          posted @ 2013-01-25 12:40 楊軍威 閱讀(451) | 評論 (0)編輯 收藏

          Niagara新建station燈光報警

          1.在tools中選擇new station,新建一個station

          2.點擊Platform啟動新建的station

          3.在File中選擇open station(fox)點擊

          4.選擇station中的Config右鍵新建文件夾(如yang)

          5.在此文件夾下右鍵新疆Wire Sheet

          6.在Wire Sheet下右鍵選擇new 一個numericWritable

          7.在這個numericWritable右鍵action中set數(shù)值

          8.重復(fù)6.7再次建立一個標(biāo)準(zhǔn)值的numericWritable

          9.在Wire Sheet下右鍵選擇new 一個BooleanWritable

          10.在這個BooleanWritable右鍵action中setboolean值

          11.在Window中的Side Bars中點擊Palette

          12.在Palette中找到kitControl中的Logic中的GreaterThan拖入到Wire Sheet中

          13.讓兩個的numericWritable的Out分別指向GreaterThan的A和B(A>B是true)

          14.再讓GreaterThan的Out指向BooleanWritable其中一個值

          15.yang文件夾右鍵點擊Views中的New View

          16.在kitPx中把AnalogMeter拖入New View,再雙擊New View選擇ord

          在ord的彈出框中的下箭頭選擇Component Chooser,,選擇yang文件夾中的一個值(不是標(biāo)準(zhǔn)值)

          17.在KitPxHvac中的boolean中的bulb,拖入New View,再雙擊New View選擇ord

          在ord的彈出框中的下箭頭選擇Component Chooser,,選擇yang文件夾中的boolean對象。

          posted @ 2013-01-25 12:39 楊軍威 閱讀(558) | 評論 (0)編輯 收藏

          jfinal源碼學(xué)習(xí)

          當(dāng)用戶訪問系統(tǒng)時,所有請求先進(jìn)過在web.xml中配置的com.jfinal.core.JFinalFilter這個核心類,
          先執(zhí)行這個類的的init方法,實例化jfinalConfig對象,這個對象是需要開發(fā)者自己定義一個類繼承
          JFinalConfig類,實現(xiàn)幾個抽象方法,其中public void configConstant(Constants me)方法是配置數(shù)據(jù)庫的信息,
          開發(fā)模式,視圖的類型等,public void configRoute(Routes me)方法是配置訪問路徑的路由信息,
          public void configPlugin(Plugins me) 方法是配置數(shù)據(jù)庫連接,其他一些插件,對象模型, public void configInterceptor(Interceptors me)方法是配置全局?jǐn)r截器,public void configHandler(Handlers me)
          是配置處理器,此方法會得到訪問的url,進(jìn)行處理。此類需要在web.xml中配置,如下:
            <filter>
              <filter-name>jfinal</filter-name>
              <filter-class>com.jfinal.core.JFinalFilter</filter-class>
              <init-param>
                <param-name>configClass</param-name>
                <param-value>com.demo.config.DemoConfig</param-value>
              </init-param>
            </filter>
            <filter-mapping>
              <filter-name>jfinal</filter-name>
              <url-pattern>/*</url-pattern>
            </filter-mapping>
          在JFinalFilter類中當(dāng)執(zhí)行完init方法后,會執(zhí)行jfinal類的init方法,此方法是先定義得到工程的路徑,再初始化開發(fā)者剛剛寫的
          繼承JFinalConfig類的類,讀入配置信息。當(dāng)init方法執(zhí)行完后,執(zhí)行doFilter方法,得到用戶訪問的url,分到不同的handler中進(jìn)行處理

          posted @ 2013-01-21 11:05 楊軍威 閱讀(1757) | 評論 (0)編輯 收藏

          java時間程序

               摘要: import java.util.*;002import java.text.*;003import java.util.Calendar;004public class VeDate {005 /**006  * 獲取現(xiàn)在時間007  * 008  * <a href="http://my.oschina.net/u/5...  閱讀全文

          posted @ 2013-01-18 15:29 楊軍威 閱讀(262) | 評論 (0)編輯 收藏

          多個有用的程序

               摘要: 1. 字符串有整型的相互轉(zhuǎn)換 1    2 String a = String.valueOf(2);   //integer to numeric string   3 int i = Integer.parseInt(a); //numeric string to an int ...  閱讀全文

          posted @ 2013-01-18 15:08 楊軍威 閱讀(161) | 評論 (0)編輯 收藏

          svn使用

               摘要: 1.svn環(huán)境搭建在應(yīng)用myEclips 8.5做項目時,svn會成為團(tuán)隊項目的一個非常好的工具,苦苦在網(wǎng)上尋求了一下午,終于整合好了這個環(huán)境,在這里簡單介紹下,希望能為剛開始用svn的朋友一點點幫助。   svn環(huán)境需要(1)服務(wù)器端(2)客戶端(3)應(yīng)用在myeclipse中的svn插件   第一步,安裝svn服務(wù)器端。我用的是VisualSVN-Server-2.1.3這...  閱讀全文

          posted @ 2013-01-18 14:29 楊軍威 閱讀(12310) | 評論 (0)編輯 收藏

          僅列出標(biāo)題
          共43頁: First 上一頁 31 32 33 34 35 36 37 38 39 下一頁 Last 

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 东源县| 黄陵县| 宝兴县| 渭源县| 平塘县| 治县。| 道真| 九台市| 清远市| 丘北县| 正宁县| 轮台县| 洛隆县| 湟源县| 沧源| 堆龙德庆县| 旅游| 林芝县| 鄱阳县| 永定县| 敖汉旗| 昌都县| 宣城市| 镇安县| 兰西县| 双流县| 庄河市| 喜德县| 陈巴尔虎旗| 昔阳县| 高青县| 湟中县| 漾濞| 平度市| 邛崃市| 峨眉山市| 永平县| 迭部县| 彝良县| 元谋县| 东平县|