为什么我的代码报告说这个过程最多需要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是因为我希望我的电脑为我做结果的平均值,因为任何学校会告诉你,在试验时,你需要重复你的IV等级3(或更多)时间来获得更准确的结果
答案 0 :(得分: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
}
}
答案 1 :(得分:0)
我为你写了一些东西,我认为这可能更快,因此更准确但请记住,执行任何陈述需要时间,因此它不能完全准确。
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