SW练习中估算时间、内存限制辅助方法

1.内存限制

无论是SW.adv 还是SW.pro 题目中都有 “存储限制 256MB\512MB”
解题同时,我们需要使用一些估算方法和措施,进行调试

1.1 大概估算

//二维int数组 10005*10005
//估算,1_0000_0000 int  * 4 bit /1024*1024 约等于 381.5MB
private static int[][] pool = new int[size][size];

//如果题目限制为256MB 运行后则会出现下面的Exception
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
	at sw.test.CheckMemory.main(CheckMemory.java:24)

1.2 在IDE中设置VM Option

如下图所示 在IDEA中 Run configuration 设置VM options
在这里插入图片描述
在Eclipse 中Run configuration 设置VM options
在这里插入图片描述
测试代码

package sw.test;

public class CheckMemory {
    
    
    private static int size = 1_0005;
    //private static int[][] pool = new int[size][size];


    /* 如果题目的内存限制在256M,可在Run configuration的 VM Option
        添加参数中添加 -Xmx256m
        本例中 二维int数组 10005*10005
        估算,1_0000_0000 int  * 4 bit /1024*1024 约等于 381.5MB
        在修改为 -Xmx512m后,不在OutOfMemoryError
     */
    public static void main(String[] args) {
    
    
        //TO-Do
        // 测试的代码段
        int[][] pool = new int[size][size];
    }
}

2。时间限制

无论是SW.adv 还是SW.pro 题目中都有 “时间限制 1.0s\2.0s\3.0s”

2.1 大概估算

代码中 计算\运行 1_0000_0000次, 大概需时间1秒

2.2 使用代码计算

package sw.test;

import java.util.Random;

public class CheckMemory {
    
    
    private static int size = 1_0005;
    //private static int[][] pool = new int[size][size];


    /* 如果题目的内存限制在256M,可在Run configuration的 VM Option
        添加参数中添加 -Xmx256m
        本例中 二维int数组 10005*10005
        估算,1_0000_0000 int  * 4 bit /1024*1024 约等于 381.5MB
        在修改为 -Xmx512m后,不在OutOfMemoryError
     */
    public static void main(String[] args) {
    
    


        //估算法:代码中 计算\运行 1_0000_0000次, 大概需时间1秒

        //简单测试法:
        long startTime = System.currentTimeMillis();   //获取开始时间
        //TO-Do
        // 测试的代码段
        Random random = new Random(startTime);
        int[][] pool = new int[size][size];
        for (int i = 0; i <pool.length; i++) {
    
    
            for (int j = 0; j < pool.length; j++) {
    
    
                pool[i][j] = random.nextInt();
            }
        }
        long endTime = System.currentTimeMillis(); //获取结束时间
        System.out.println("程序运行时间: " + (endTime - startTime) + "ms");


    }
}


    程序运行时间: 932ms

3. SW source 模板

以下模板可以直接复制使用
也可以在IDEA\Eclipse 设置代码模板,直接生成

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;

public class source {
    
    

    public static void main(String[] args) throws Exception {
    
    


        //简单测试法:
        long startTime = System.currentTimeMillis();   //获取开始时间


        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        int N = Integer.parseInt(br.readLine());


        int[][] pool = new int[N][N];
        for (int i = 0; i < pool.length; i++) {
    
    
            StringTokenizer st = new StringTokenizer(br.readLine());
            for (int j = 0; j < pool.length; j++) {
    
    
                pool[i][j] = Integer.parseInt(st.nextToken());
            }
        }

        //TO-Do

        //Solution


        long endTime = System.currentTimeMillis(); //获取结束时间
        System.out.println("程序运行时间: " + (endTime - startTime) + "ms");


    }
}

猜你喜欢

转载自blog.csdn.net/awp0011/article/details/83713249