Java computing template design patterns takes time

Hello everyone, I'm Ziph!

Compare and view foreground, background, and time-consuming summary inquiry:
the Java code is relatively time-consuming method of execution

A simple calculation of time spent template design mode as follows:

//模板方法设计模式
public class TestTemplate {
    public static void main(String[] args) {
        new SubTemplate().spendTime();
    }
}

abstract class Template {
    abstract void code();

    public void spendTime() {
        long start = System.currentTimeMillis();

        code();

        long end = System.currentTimeMillis();
        System.out.println("花费的时间为:" + (end - start));
    }
}

class SubTemplate extends Template {
    public void code() {
        boolean flag = false;
        for (int i = 2; i < 100000; i++) {
            for (int j = 2; j < Math.sqrt(i); j++) {
                if (i % j == 0) {
                    flag = true;
                    break;
                }
            }
            if (!flag) {
                System.out.println(i);
            }
            flag = false;
        }
    }
}
Published 52 original articles · won praise 96 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_44170221/article/details/104453040