【问题标题】:Stopwatch is not reporting correct time秒表没有报告正确的时间
【发布时间】:2017-11-30 12:00:03
【问题描述】:

为什么我的代码报告说这个过程最多需要 5 秒才能完成,即使它甚至没有实时花费四分之一秒?

我将尝试将与秒表相关的代码加粗,以使您不必仔细查看所有内容。请善待,因为这是我的第一篇文章,所以如果它很笨拙,我很抱歉。看起来如果代码不加粗,有问题的部分周围会有**。

*背景:这是一篇数学论文。它应该是一个找到主要因素并告诉找到它们需要多长时间的程序。它正在寻找主要因素,但秒表以秒为单位报告了一个荒谬的数字。此外,此代码受

影响最大

http://www.geeksforgeeks.org/print-all-prime-factors-of-a-given-number/

秒表、用户输入功能和重复是由我自己的想法或在其他人的帮助下添加的*

    // Program to print all prime factors
import java.io.*;
import java.lang.Math;
import java.util.Scanner;
import java.text.DecimalFormat;

class primeFactorer4
{
    **static long startTime = System.nanoTime();**
    // A function to print all prime factors
    // of a given number n
    public static void primeFactors(long n)
    {
        // Print the number of 2s that divide n
        while (n%2==0)
        {
            System.out.print(2 + " ");
            n /= 2;
        }

        // n must be odd at this point.  So we can
        // skip one element (Note i = i +2)
        for (int i = 3; i <= Math.sqrt(n); i+= 2)
        {
            // While i divides n, print i and divide n
            while (n%i == 0)
            {
                System.out.print(i + " ");
                n /= i;
            }
        }

        // This condition is to handle the case whien
        // n is a prime number greater than 2
        if (n > 2)
            System.out.print(n);
    }

    public static void main (String[] args)
    {
        Console console = System.console();
        String input = console.readLine("Enter input:");
        long n = Long.valueOf(input);
        for (int k=1; k<=10; k++)
    {
        primeFactors(n);
        System.out.println(" Try " + k);
    }
        **double endTime = System.nanoTime();
        double totalTime = endTime - startTime;
        DecimalFormat totalTimeFormat = new DecimalFormat("##.###");
        System.out.println("    Time taken in seconds:" + totalTimeFormat.format(totalTime/10/1000000000));**
        primeFactorer4.main(args);
    //reason for the weird division is for clarity. "totalTime" is the time surpassed 
    //to repeat all the methods, the "10" in the middle is to get the mean total time
    //of all the primeFactors cycles, and the "1000000000" at the end is to convert nanoseconds into seconds
    }
}

我向 primeFactors 打了 10 次电话的原因是因为我希望我的计算机为我计算结果的平均值,因为任何学校都会告诉你,在进行实验时,你需要重复 IV 级 3(或更多)获得更准确结果的时间

【问题讨论】:

  • 您可以随时将方法粘贴在for 循环中,让代码更简洁。
  • 计算机数学不是科学实验。叫它十次是没有意义的。
  • 这不是整个实验。我将这个结果放在一个图表中,该图表与数字的大小和找到主要因素的时间有关,然后找到回归线,我的老师告诉我这是使其与数学相关的最低要求,哈哈。我的主题是质因数,而我知道让质因数变得有趣的唯一方法是在讨论密码学时。
  • 好的,所以我按照你说的那样做了for 循环,现在它更干净了,但我仍然觉得很荒谬。
  • 如果将endTime 的声明从double 更改为long,会发生什么?我想知道您是否受到当前纳米时间范围内double 值稀疏的影响。

标签: java prime-factoring


【解决方案1】:

好吧,没关系,我解决了我的问题。我在 startTime 和 endTime 变量下放置了一个 println 命令,我发现 startTime 变量是在程序启动时开始的,而不是在用户输入他们想要分解的数字时。它现在给了我适当的结果,感觉与我个人输入数字的速度无关。

对于那些对程序感兴趣的人,这个问题的解决方案适用于你,或者你只是想看看解决方案和问题之间的对比,这里是新代码。

    // Program to print all prime factors
import java.io.*;
import java.lang.Math;
import java.util.Scanner;
import java.text.DecimalFormat;

class primeFactorer4
{

    // A function to print all prime factors
    // of a given number n
    public static void primeFactors(long n)
        {
            // Print the number of 2s that divide n
            while (n%2==0)
            {
                System.out.print(2 + " ");
                n /= 2;
            }

            // n must be odd at this point.  So we can
            // skip one element (Note i = i +2)
            for (int i = 3; i <= Math.sqrt(n); i+= 2)
            {
                // While i divides n, print i and divide n
                while (n%i == 0)
                {
                    System.out.print(i + " ");
                    n /= i;
                }
            }

            // This condition is to handle the case whien
            // n is a prime number greater than 2
            if (n > 2)
                System.out.print(n);
        }



    public static void main (String[] args)
    {
        Console console = System.console();
        String input = console.readLine("Enter input:");
        long n = Long.valueOf(input);
        long startTime = System.nanoTime();
        System.out.println(startTime);
        for (int k=1; k<=10; k++)
        {
            primeFactors(n);
            System.out.println(" Try " + k);
        }
        double endTime = System.nanoTime();
            System.out.println(endTime);
            double totalTime = endTime - startTime;
            DecimalFormat totalTimeFormat = new DecimalFormat("##.##########");
            System.out.println("    Time taken in seconds:" + totalTimeFormat.format(totalTime/10/1000000000));
        primeFactorer4.main(args);
    //reason for the weird division is for clarity. "totalTime" is the time surpassed 
    //to repeat all the methods, the "10" in the middle is to get the mean total time
    //of all the primeFactors cycles, and the "1e9" at the end is to convert nanoseconds into seconds
    }
}

【讨论】:

    【解决方案2】:

    我为你写了一些东西,我认为这可能会更快,因此更准确,但请记住,执行任何语句都需要时间,因此它不可能完全准确。

    long start= System.currentTimeMillis(); //start time
    //Insert code here
    long difference = System.currentTimeMillis(); //finish time
    difference -= start;
    System.out.println("Time took to run code was " + difference);; //Print the amount of time that it took
    

    【讨论】:

    • 谢谢 brenan,你的看起来没有我的那么复杂,也更整洁,但我找到了我的解决方案,而且效果很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-20
    相关资源
    最近更新 更多