之前的java测试代码 性能测试

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Administrator
 */
public class 测试代码2 {

    public static void main(String args[]) {
        int intSize = 0;
        try {
          //for (int i = 191059999; i <= 191160000; i++) {
            //内存开到3个g可以跑4开头的9位数int
            for (int i = 447483646; i <= 2147483647; i++) {
                intSize = i;
                int[] allInt = new int[i];
                System.out.println("allInt:" + allInt.length);
            }
        } catch (Exception e) {
        } finally {
            System.out.println("intSize:" + intSize);
        }
    }
}
import java.util.ArrayList;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Administrator
 */
public class 测试代码1 {

    static ArrayList<Integer> dataList = new ArrayList<Integer>();

    public static void main(String args[]) {
        int judgeListmaxCount = 0;
        try {
            for (int i = 0; i <= 500000000; i++) {
                judgeListmaxCount = i;
                dataList.add(i);
            }
        } catch (Exception e) {
            System.out.println("judgeListmaxCount:" + judgeListmaxCount);
        } finally {
            System.out.println("judgeListmaxCount:" + judgeListmaxCount);

        }
    }
}
  java代码测试集合,数组等最大存储大小 

 分类: java常用知识 
2011-11-21 19:00 305人阅读 评论(0) 收藏 举报 

           其实java里面的集合,数组等他们的存储容量我们是可以预估的。1、存储原始类型,这种存储大小是固定的。2、存储对象,由于内容大小有变化,但是我们可以通过测试获取到大慨存储容量。

           还有就是我们扩容内存,会影响集合或数组存储大小。

1、List<Integer>

 int judgeListmaxCount = 0;
         try {
             for (int i = 0; i <= 50000000; i++) {
                 judgeListmaxCount = i;
                 dataList.add(i);
             }
         } catch (Exception e) {
             System.out.println("judgeListmaxCount:" + judgeListmaxCount);
         } finally {
             System.out.println("judgeListmaxCount:" + judgeListmaxCount);

        }

异常消息:

Exception in thread "main" judgeListmaxCount:38647475
 java.lang.OutOfMemoryError: Java heap space
     at java.util.ArrayList.ensureCapacity(ArrayList.java:169)
     at java.util.ArrayList.add(ArrayList.java:351)
     at com.yxie.test.CreateDichotoSearchData.main(CreateDichotoSearchData.java:21)

xieyun@xieyun-OptiPlex-760:~/work/dichotomy/bin$ java -Xms1024m -Xmx1024m com.yxie.test.CreateDichotoSearchData
 judgeListmaxCount:38647475
 Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
     at java.util.Arrays.copyOf(Arrays.java:2760)
     at java.util.Arrays.copyOf(Arrays.java:2734)
     at java.util.ArrayList.ensureCapacity(ArrayList.java:167)
     at java.util.ArrayList.add(ArrayList.java:351)
     at com.yxie.test.CreateDichotoSearchData.main(CreateDichotoSearchData.java:21)

 2、int[]

int intSize = 0;
         try {
             for (int i = 191059999; i <= 191160000; i++) {
                 intSize = i;
                 int[] allInt = new int[i];
                 System.out.println("allInt:" + allInt.length);
             }
         } catch (Exception e) {
         } finally {
             System.out.println("intSize:" + intSize);
         }

异常消息:

intSize:191060000
 Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
     at com.yxie.test.CreateDichotoSearchData.main(CreateDichotoSearchData.java:54)

改变java虚拟机内存大小,会影响int[]数组大小。

xieyun@xieyun-OptiPlex-760:~/work/dichotomy/bin$ java -Xms1024m -Xmx1024m com.yxie.test.CreateDichotoSearchData
 Integer max:2147483647
 fileSize:4000000000
 spareFileSize:1852516353
intSize:191059999
 Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
     at com.yxie.test.CreateDichotoSearchData.main(CreateDichotoSearchData.java:54)




 xieyun@xieyun-OptiPlex-760:~/work/dichotomy/bin$ 
xieyun@xieyun-OptiPlex-760:~/work/dichotomy/bin$ java -Xms2024m -Xmx2024m com.yxie.test.CreateDichotoSearchData
 Integer max:2147483647
 fileSize:4000000000
 spareFileSize:1852516353
 allInt:191059999
 allInt:191060000
 allInt:191060001


import java.util.HashMap;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Administrator
 */
public class hashmap {

    static HashMap<Integer, Integer> dataList = new HashMap<Integer, Integer>();

    public static void main(String args[]) {
        int judgeListmaxCount = 0;
        try {
            for(int j=0;j<1000;j++){
                            //447483646
                //小于300*225000时效率较高 
            for (int i = 0; i <= 225000; i++) {
                judgeListmaxCount ++;
                dataList.put(i*j+i, i*j);}
                System.out.println(j);
            }
        } catch (Exception e) {
            System.out.println("judgeListmaxCount:" + judgeListmaxCount);
        } finally {
            System.out.println("judgeListmaxCount:" + judgeListmaxCount);

        }
    }
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一条测试空调性能Java 代码: ``` public class AirConditionerTest { public static void main(String[] args) { AirConditioner airConditioner = new AirConditioner(); airConditioner.turnOn(); airConditioner.setTemperature(25); int targetTemperature = 20; int currentTime = 0; int maxTime = 300; while (airConditioner.getTemperature() > targetTemperature && currentTime < maxTime) { airConditioner.decreaseTemperature(); currentTime++; } if (airConditioner.getTemperature() <= targetTemperature) { System.out.println("空调性能测试通过!"); } else { System.out.println("空调性能测试失败,请检查空调是否正常工作。"); } airConditioner.turnOff(); } } class AirConditioner { private int temperature; private boolean isOn; public void turnOn() { isOn = true; } public void turnOff() { isOn = false; } public void setTemperature(int temperature) { this.temperature = temperature; } public void decreaseTemperature() { if (isOn) { temperature--; } } public int getTemperature() { return temperature; } } ``` 这个程序创建了一个名为 `AirConditionerTest` 的类,它包含了 `main` 方法,用于测试空调的性能。这个程序还创建了一个名为 `AirConditioner` 的类,它包含了一些方法,用于控制空调的开关、温度等。在测试过程中,程序会将空调打开,并将温度设为 25 度,然后让空调降温,直到温度降到 20 度或经过了 300 秒钟。如果空调成功降温到了目标温度,则输出“空调性能测试通过!”;否则输出“空调性能测试失败,请检查空调是否正常工作。”。最后程序会关闭空调。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值