gpt4 book ai didi

java - 加速/提高 Java 数组类的性能

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

对于类,我必须使用数组编写一个程序,以便有人可以在特定时间范围内查找流行的婴儿名字。 (例如:1890-1910 年间最受欢迎的男孩和女孩名字是......)

我认为该程序还不错,但运行速度非常慢。我不知道这是否只是因为我的代码,或者有一个简单的程序查看一个大文件;但有没有办法加快这个过程呢?

import java.io.*;
import java.util.*;
import java.net.URL;

public class BabyNamesMainDriver {

// ===========================================================================================

private static void process(Scanner sc, String[] args) throws Exception {
// The two arrays
ArrayList<BabyNameClass> boys = new ArrayList<BabyNameClass>();
ArrayList<BabyNameClass> girls = new ArrayList<BabyNameClass>();

System.out.println("Enter a start year between 1880 and 2016:");// start year
int startYear = sc.nextInt();
if (startYear < 1880 || startYear > 2016)// Error check
System.out.println("ERROR: Invalid start year.\n");
else
System.out.println("Enter a end year. Must be less then 2016 but greater than the start year :");// End year
int endYear = sc.nextInt();
if (endYear < 1880 || endYear > 2016)// Error check
System.out.println("ERROR: Invalid end year. \n");
else
System.out.println("Enter the number of baby names to list. ");// Number of baby names
int numOfNames = sc.nextInt();
// if (topBabies <= 0);//Error Check
// System.out.println("ERROR: Invalid number of names. \n");

ReadBabyNames(startYear, endYear, boys, girls);// Reads file info

// Header for top girl names
System.out.print("\nTop " + numOfNames + " Girl names:\n");
for (int i = 0; i < numOfNames; i++) {
System.out.println(girls.get(i));
}
// Header for top boy names
System.out.print("\nTop " + numOfNames + " Boy names:\n");
for (int i = 0; i < numOfNames; i++) {
System.out.println(boys.get(i));
}
sc.nextLine();
}

// ===========================================================================================

private static void ReadBabyNames(int startYear, int endYear, ArrayList<BabyNameClass> boys,
ArrayList<BabyNameClass> girls) throws IOException, Exception {
System.out.println("Please stand by...");

for (int year = startYear; year <= endYear; year++) {
@SuppressWarnings("resource")
Scanner sc = new Scanner(
new URL("https://cs.stcc.edu/~silvestri/babynames/yob" + year + ".txt").openStream());// file from
// URL

sc.useDelimiter("\\s*,\\s*|\\s+");

while (sc.hasNextLine()) {
String name = sc.next();
String sex = sc.next();
int number = sc.nextInt();
sc.nextLine();
BabyNameClass babies = new BabyNameClass(name, number);
Collections.sort(boys);
Collections.sort(girls);
// Getting number of lil' babies
if (sex.equalsIgnoreCase("F")) {
int index = 1;
index = girls.indexOf(babies);
if (index == -1)
girls.add(babies);
else
girls.get(index).addToAmount(number);
} else {
int index = 1;
index = boys.indexOf(babies);
if (index == -1)
boys.add(babies);
else
boys.get(index).addToAmount(number);
}
}
}
}
// ===========================================================================================

public static void main(String args[]) throws Exception {
final String TITLE = "Baby Name Ranking";
final String CONTINUE_PROMPT = "\nDo this again? [y/N] ";

System.out.println("Welcome to " + TITLE);
Scanner sc = new Scanner(System.in);
do {
process(sc, args);
} while (doThisAgain(sc, CONTINUE_PROMPT));
sc.close();
System.out.println("Thank you for using " + TITLE);
}
// ===========================================================================================

private static boolean doThisAgain(Scanner sc, String prompt) {
System.out.print(prompt);
String doOver = sc.nextLine();
return doOver.equalsIgnoreCase("Y");
}

}

二等

public class BabyNameClass implements Comparable<BabyNameClass> {

// ==========================
private String name;
private int num;

// ===========================================================================================
public BabyNameClass(String name, int num) {
this.name = name;
this.num = num;
}

// ===========================================================================================
public String getName() {
return this.name;
}

// ===========================================================================================
public int getNum() {
return this.num;
}

// ===========================================================================================
public void addToAmount(int num) {
this.num += num;
}

// ===========================================================================================
public void setAmount(int num) {
this.num = num;
}

// ===========================================================================================

public boolean equals(Object lak) {
if (lak == null)
return false;
if (this == lak)
return true;
if (getClass() != lak.getClass())
return false;
BabyNameClass Gu = (BabyNameClass) lak;
if (name == null) {
if (Gu.name != null)
return false;
} else if (!name.equals(Gu.name))
return false;
return true;
}

// ===========================================================================================
public int compareTo(BabyNameClass arg0) {
if (this.num < arg0.num)
return 1;
if (this.num > arg0.num)
return -1;
if (this.name.compareTo(arg0.name) > 0)
return 1;
if (this.name.compareTo(arg0.name) < 0)
return -1;

else
return 0;
}
// ===========================================================================================

public String toString() {
return " " + this.num + " babies were named " + this.name;
}

}

最佳答案

每次从 txt 文件中读取新行时,都会对两个数组列表进行排序。为什么?排序非常消耗CPU。特别是当您使用的 txt 文件很大时。

尝试将排序移出 while 循环。

关于java - 加速/提高 Java 数组类的性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47148936/

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