首页 文章 精选 留言 我的

精选列表

搜索[优化],共10000篇文章
优秀的个人博客,低调大师

Java程序性能优化7

避免不需要的造型操作 所有的类都是直接或者间接继承自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; } }

优秀的个人博客,低调大师

Java程序性能优化9

使用移位操作来代替'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; // 不能转换成位移操作 } }

优秀的个人博客,低调大师

Java程序性能优化2

为'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); 参考资料:Dov Bulka, "Java Performance and Scalability Volume 1: Server-Side Programming Techniques" Addison Wesley, ISBN: 0-201-70429-3 pp.55 – 57

优秀的个人博客,低调大师

Java程序性能优化3

在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 = 0; while (fis.read () != -1) count++; System.out.println (count); fis.close (); } catch (FileNotFoundException e1) { } catch (IOException e2) { } } } 更正:在最后一个catch后添加一个finally块 参考资料:Peter Haggar: "Practical Java - Programming Language Guide".Addison Wesley, 2000, pp.77-79

优秀的个人博客,低调大师

Android性能优化之UI实战

在Android应用开发过程中,屏幕上控件的布局代码和程序的逻辑代码通常是分开的。界面的布局代码是放在一个独立的xml文件中的,这个文件里面是树型组织的,控制着页面的布局。通常,在这个页面中会用到很多控件,控件会用到很多的资源。Android系统本身有很多的资源,包括各种各样的字符串、图片、动画、样式和布局等等,这些都可以在应用程序中直接使用。这样做的好处很多,既可以减少内存的使用,又可以减少部分工作量,也可以缩减程序安装包的大小。 原文博客地址:http://www.apkbus.com/blog-919651-76902.html []()1)利用系统定义的id 在xml文件中引用系统的id,只需要加上“@android:”前缀即可。如果是在Java代码中使用系统资源,和使用自己的资源基本上是一样的。不同的是,需要使用android.R类来使用系统的资源,而不是使用应用程序指定的R类。这里如果要获取ListView可以使用android.R.id.list来获取。 <ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="fill_parent"/> []()2)利用系统的图片资源 Android中没有公开的资源,在xml中直接引用会报错。除了去找到对应资源并拷贝到我们自己的应用目录下使用以外,我们还可以将引用“@android”改成“@*android”解决。比如上面引用的附件图标,可以修改成下面的代码。 android:icon="@*android:drawable/ic_menu_attachment" 修改后,再次Build工程,就不会报错了。 []()3)利用系统的字符串资源 如果使用系统的字符串,默认就已经支持多语言环境了。如上述代码,直接使用了@android:string/yes和@android:string/no,在简体中文环境下会显示“确定”和“取消”,在英文环境下会显示“OK”和“Cancel”。 []()4)利用系统的Style android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" /> 其中android:textAppearance="?android:attr/textAppearanceMedium"就是使用系统的style。需要注意的是,使用系统的style,需要在想要使用的资源前面加“?android:”作为前缀,而不是“@android:”。 []()5)利用系统的颜色定义 []()6)提取共同的组件,通过include引入 <include layout="@layout/navigator_bar" /> 一般情况下,在项目的初期就能够大致确定整体UI的风格。所以早期的时候就可以做一些规划,将通用的模块先写出来。 下面是可能可以抽出的共用的布局: 1)背景。有的应用在不同的界面里会用到统一的背景。后期可能会经常修改默认背景,所以可以将背景做成一个通用模块。 2)头部的标题栏。如果应用有统一的头部标题栏,就可以抽取出来。 3)底部的导航栏。如果应用有导航栏,而且大部分的Activity的底部导航栏是相同的,就可以将导航栏写成一个通用模块。 4)ListView。大部分应用都会用到ListView展示多条数据。项目后期可能会经常调整ListView的风格,所以将ListView作为一个通用的模块比较好。 []()7)延迟加载隐藏的View 有时候,我们的页面中可能会包含一些布局,这些布局默认是隐藏的,当用户触发了一定的操作之后,隐藏的布局才会显示出来。比如,我们有一个Activity用来显示好友的列表,当用户点击Menu中的“导入”以后,在当前的Activity中才会显示出一个导入好友的布局界面。从需求的角度来说,这个导入功能,一般情况下用户是不使用的。即大部分时候,导入好友的布局都不会显示出来。这个时候,就可以使用延迟加载的功能。 ViewStub是一个隐藏的,不占用内存空间的视图对象,它可以在运行时延迟加载布局资源文件。当ViewStub被设置为可见,或者调用inflate()函数时,才会真的去加载这个布局资源文件。该ViewStub在加载视图时会在父容器中替换它本身。因此,ViewStub会一直存在于视图中,直到调用setVisibility(int)或者inflate()为止。ViewStub的布局参数会随着加载的视图数一同被添加到ViewStub父容器。同样,也可以通过使用inflated Id属性来定义或重命名要加载的视图对象的Id值。 <ViewStub android:id="@+id/stub_import" android:inflatedId="@+id/panel_import" android:layout="@layout/progress_overlay" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" /> 通过“stub_import”这个id可以找到被定义的ViewStub对象。加载布局资源文件“progress_overlay”后,ViewStub对象从其父容器中移除。可以通过“panel_import”这个id找到由布局资源“progress_overlay”创建的View。 ((ViewStub) findViewById(R.id.stub_import)).setVisibility(View.VISIBLE); // 或者 View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate();

优秀的个人博客,低调大师

Android布局优化----ViewStub、include、merge

1 StubView 作用:StubView标签中的布局只有在需要的时候才会被渲染加载。 注意:StubView的渲染加载操作只能执行一次;不支持merge标签 使用示例: (1)ViewStub中引用的布局 1 2 3 4 5 6 7 8 9 10 11 12 13 14 <? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout android:id = "@+id/stublayout" xmlns:android = "http://schemas.android.com/apk/res/android" android:orientation = "vertical" android:layout_width = "match_parent" android:layout_height = "match_parent" > < TextView android:id = "@+id/sbtv" android:layout_width = "300dp" android:layout_height = "300dp" android:text = "thisissubtextview" /> </ LinearLayout > (2)使用ViewStub 1 2 3 4 5 < ViewStub android:id = "@+id/viewstub" android:layout_width = "100dp" android:layout_height = "100dp" android:layout = "@layout/sublayout" /> (3)java代码中渲染加载 1 2 3 4 ViewStubstub=(ViewStub)findViewById(R.id.viewstub); stub.inflate(); TextViewstubtv=(TextView)layout.findViewById(R.id.sbtv); stubtv.setText( "hellostub!" ); 2 include标签 作用:将引用的布局替换到当前布局中该标签所处的位置; 注意:引用的布局非merge,设置inlude的id属性后会覆盖掉引用布局顶层layout的id; 示例1 引用布局非merge (1)引用布局 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:id = "@+id/sharedlayout2" android:layout_width = "200dp" android:layout_height = "200dp" android:background = "@android:color/black" > < TextView android:id = "@+id/mytv" android:layout_width = "100dp" android:layout_height = "100dp" android:background = "@android:color/holo_blue_light" android:text = "这时一个公用的布局" /> </ LinearLayout > (2)使用include标签 1 2 3 4 5 <include android:id= "@+id/include1" <!--该id会覆盖布局( 1 )中LinearLayout的id;--> layout= "@layout/my" android:layout_width= "50dp" android:layout_height= "50dp" ></include> (3)java中使用 1 2 3 LinearLayoutsharedlayout=(LinearLayout)findViewById(R.id.include1); TextViewtv=(TextView)sharedlayout.findViewById(R.id.mytv); tv.setText( "helloinclude1" ); 示例2 引用merge布局 (1)引用布局 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 < merge xmlns:android = "http://schemas.android.com/apk/res/android" > < LinearLayout android:id = "@+id/mergetlayout" android:layout_width = "wrap_content" android:layout_height = "wrap_content" > < TextView android:id = "@+id/mergetv1" android:layout_width = "40dp" android:layout_height = "40dp" android:background = "@android:color/holo_blue_bright" android:text = "mergetext1" /> < TextView android:id = "@+id/mergetv2" android:layout_width = "40dp" android:layout_height = "40dp" android:background = "@android:color/darker_gray" android:text = "mergetext2" /> </ LinearLayout > </ merge > (2)include标签:merge布局没有id属性,所以这里的id其实没有意义 1 2 3 < include android:id = "@+id/include2" layout = "@layout/mymerge" ></ include > (3)java中使用 1 2 3 4 5 //LinearLayoutlayout=(LinearLayout)findViewById(R.id.include2);//通过该代码无法获取(1)中的LinearLayout,这里的layout为null TextViewmTV1=(TextView)findViewById(R.id.mergetv1); mTV1.setText( "hellomergetv1" ); TextViewmtv2=(TextView)findViewById(R.id.mergetv2); mtv2.setText( "hellomergetv2" ); 3 merge标签 merge标签相当于控件的简单集合,被include到其他布局中后根据所处容器布局结合各个控件的相关属性进行布局。 注意:merge标签没有id属性 使用示例: (1)merge布局 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 < merge xmlns:android = "http://schemas.android.com/apk/res/android" > < TextView android:id = "@+id/mergetv1" android:layout_width = "40dp" android:layout_height = "40dp" android:background = "@android:color/holo_blue_bright" android:text = "mergetext1" /> < TextView android:id = "@+id/mergetv2" android:layout_width = "40dp" android:layout_height = "40dp" android:background = "@android:color/darker_gray" android:text = "mergetext2" /> </ merge > 此时的布局为两个textview重叠并且mergetv2处于上层; (2)merge标签使用 1 2 3 < include android:id = "@+id/include2" layout = "@layout/mymerge" ></ include > (3)由于该include标签处于一个竖直的linearlayout中,此时merge布局的显示效果如下: merge text 1 merge text 2 (4)java中使用 1 2 3 4 TextViewmTV1=(TextView)findViewById(R.id.mergetv1); mTV1.setText( "hellomergetv1" ); TextViewmtv2=(TextView)findViewById(R.id.mergetv2); mtv2.setText( "hellomergetv2" ); 本文转自wauoen51CTO博客,原文链接:http://blog.51cto.com/7183397/1847175 ,如需转载请自行联系原作者

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册