gpt4 book ai didi

java - 秒表未报告正确的时间

转载 作者:行者123 更新时间:2023-12-02 11:56:29 25 4
gpt4 key购买 nike

为什么我的代码报告说此过程最多需要 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(或更高)多次获得更准确的结果

最佳答案

好吧,没关系,我解决了我的问题。我在 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
}
}

关于java - 秒表未报告正确的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47565125/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com