java学习

java学习

 

java程序性能优化2

六、避免不需要的instanceof操作 如果左边的对象的静态类型等于右边的,instanceof表达式返回永远为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()
用一个字符作为参数调用startswith()也会工作的很好,但从性能角度上来看,调用用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; // 不能转换成位移操作 } }
十、使用移位操作代替'a * b'
同上。 [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; // 不能转换 } }
十一、在字符串相加的时候,使用 ' ' 代替 " ",如果该字符串只有一个字符的话 例子: 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块移出循环 把try/catch块放入循环体内,会极大的影响性能,如果编译jit被关闭或者你所使用的是一个不带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块移出循环 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)代码执行的更快 (生成的字节码少了5个字节); 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' 常量字符串并不需要动态改变长度。 例子: public class usc { string method () { stringbuffer s = new stringbuffer ("hello"); string t = s + "world!"; return t; } } 更正: 把stringbuffer换成string,如果确定这个string不会再变的话,这将会减少运行开销提高性能。
十五、使用条件操作符替代"if (cond) return; else return;" 结构 条件操作符更加的简捷 例子: 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); } }
十六、不要在循环体中实例化变量 在循环体中实例化临时变量将会增加内存消耗 例子: 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); } } } 更正: 在循环体外定义变量,并反复使用 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 杨军威 阅读(196) | 评论 (0)编辑 收藏

java程序性能优化

一、避免在循环条件中使用复杂表达式 在不做编译优化的情况下,在循环中,循环条件会被反复计算,如果不使用复杂表达式,而使循环条件值不变的话,程序将会运行的更快。 例子: 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扩充大小的时候需要重新创建一个更大的数组,将原原先数组中的内容复制过来,最后,原先的数组再被回收。可见vector容量的扩大是一个颇费时间的事。 通常,默认的10个元素大小是不够的。你最好能准确的估计你所需要的最佳大小。 例子: 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. } 更正: 自己设定初始大小。 public vector v = new vector(20); public hashtable hash = new hashtable(10);

三、在finally块中关闭stream 程序中使用到的资源应当被释放,以避免资源泄漏。这最好在finally块中去做。不管程序执行的结果如何,finally块总是会执行的,以确保资源的正确关闭。 例子: 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 ()'代替通过来循环复制数组 'system.arraycopy ()' 要比通过循环来复制数组快的多。 例子: 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); } }

五、让访问实例内变量的getter/setter方法变成”final” 简单的getter/setter方法应该被置成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 杨军威 阅读(209) | 评论 (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获取单选框、复选框的值及操作</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 杨军威 阅读(393) | 评论 (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,
        //加载本地数据框
        mode : 'local',
        displayField : 'name',
        valueField : 'id',
        hiddenName :'id',
        //hiddenName :'id',
        width : 125
    });

posted @ 2013-01-30 13:24 杨军威 阅读(278) | 评论 (0)编辑 收藏

Niagara采集温湿度和控制灯的亮灭

1.在采集温湿度数据时,现在config下建立温湿度的文件夹(view),

2.在此文件夹下新建立NumericWritablewen节点,在view中建立kitPx中的Bargraph柱状图,编辑对应wen节点中out中的value,

3.在控制灯的开关时在kitPx中选择ActionButton,为每个灯选择2个ActionButton,一个开,一个关,编辑开的开关选择此灯节点中的emergencyActive,编辑关的开关选择此灯节点中的emergencyInactive

4.Palette中找iopt,找host,,在config下建立iopt文件夹,Local Port设置6800,Dest Port设置1025

posted @ 2013-01-25 12:40 杨军威 阅读(431) | 评论 (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数值

8.重复6.7再次建立一个标准值的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文件夹中的一个值(不是标准值)

17.在KitPxHvac中的boolean中的bulb,拖入New View,再双击New View选择ord

在ord的弹出框中的下箭头选择Component Chooser,,选择yang文件夹中的boolean对象。

posted @ 2013-01-25 12:39 杨军威 阅读(541) | 评论 (0)编辑 收藏

jfinal源码学习

当用户访问系统时,所有请求先进过在web.xml中配置的com.jfinal.core.JFinalFilter这个核心类,
先执行这个类的的init方法,实例化jfinalConfig对象,这个对象是需要开发者自己定义一个类继承
JFinalConfig类,实现几个抽象方法,其中public void configConstant(Constants me)方法是配置数据库的信息,
开发模式,视图的类型等,public void configRoute(Routes me)方法是配置访问路径的路由信息,
public void configPlugin(Plugins me) 方法是配置数据库连接,其他一些插件,对象模型, public void configInterceptor(Interceptors me)方法是配置全局拦截器,public void configHandler(Handlers me)
是配置处理器,此方法会得到访问的url,进行处理。此类需要在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类中当执行完init方法后,会执行jfinal类的init方法,此方法是先定义得到工程的路径,再初始化开发者刚刚写的
继承JFinalConfig类的类,读入配置信息。当init方法执行完后,执行doFilter方法,得到用户访问的url,分到不同的handler中进行处理

posted @ 2013-01-21 11:05 杨军威 阅读(1753) | 评论 (0)编辑 收藏

java时间程序

     摘要: import java.util.*;002import java.text.*;003import java.util.Calendar;004public class VeDate {005 /**006  * 获取现在时间007  * 008  * <a href="http://my.oschina.net/u/5...  阅读全文

posted @ 2013-01-18 15:29 杨军威 阅读(255) | 评论 (0)编辑 收藏

多个有用的程序

     摘要: 1. 字符串有整型的相互转换 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 杨军威 阅读(137) | 评论 (0)编辑 收藏

svn使用

     摘要: 1.svn环境搭建在应用myEclips 8.5做项目时,svn会成为团队项目的一个非常好的工具,苦苦在网上寻求了一下午,终于整合好了这个环境,在这里简单介绍下,希望能为刚开始用svn的朋友一点点帮助。   svn环境需要(1)服务器端(2)客户端(3)应用在myeclipse中的svn插件   第一步,安装svn服务器端。我用的是VisualSVN-Server-2.1.3这...  阅读全文

posted @ 2013-01-18 14:29 杨军威 阅读(12292) | 评论 (0)编辑 收藏

仅列出标题
共43页: First 上一页 31 32 33 34 35 36 37 38 39 下一页 Last 

导航

统计

常用链接

留言簿

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜