相关疑难解决方法(0)

等效静态和非静态方法的速度差异很大

在此代码中,当我在main方法中创建一个Object 然后调用该对象方法:( ff.twentyDivCount(i)运行在16010毫秒)时,它运行速度比使用此注释调用它快得多:( twentyDivCount(i)运行在59516毫秒).当然,当我在不创建对象的情况下运行它时,我将方法设为静态,因此可以在main中调用它.

public class ProblemFive {

    // Counts the number of numbers that the entry is evenly divisible by, as max is 20
    int twentyDivCount(int a) {    // Change to static int.... when using it directly
        int count = 0;
        for (int i = 1; i<21; i++) {

            if (a % i == 0) {
                count++;
            }
        }
        return count;
    }

    public static void main(String[] args) {
        long startT = System.currentTimeMillis();;
        int start = …
Run Code Online (Sandbox Code Playgroud)

java methods performance static object

86
推荐指数
2
解决办法
8254
查看次数

为什么静态方法不可测试?

为什么静态方法不可测试?请举例说明(如果可能,请使用PHP).

php static-methods unit-testing

13
推荐指数
1
解决办法
1768
查看次数