我想用Java创建一个2D String'matrix'对象。我的两个目标是提高效率和简化代码。
我听说使用ArrayList比String [] []效率更高。首先,我想知道这是否属实,如果是,那么效率会更高?
另一件事是我必须能够在'矩阵'中添加列和行。我能够开发一些有效的算法来向String [] []添加行和列。我很想知道以这种方式开发算法来操作2D List是否值得 - 是否会有显着的性能提升?
感谢您的帮助!
答案 0 :(得分:2)
首先简单回答:直接数组操作会更快,因为您可以避免通用类的开销。通过利用Unsafe实例(非公共API)进行直接阵列访问,您甚至可以获得更高的性能。
对于意见部分:如果性能如此重要,我宁愿看看如何同时执行或分配不同系统。在这种情况下,我宁愿避免直接使用数组,因为这很难维护。但当然这一切都取决于您的具体用例。
因此,与所有与性能相关的问题一样,针对您的特定用例进行一些测量,并自行决定哪种方法最适合您。
就像我在评论中所说的那样,我已经摆弄了自己的Matrix实现,它有两个专门的Map实现(CompactArrayMap和DirectArrayMap),它们都是关键映射的int。键自然顺序,非常像数组但具有地图功能。
答案 1 :(得分:1)
List
是一个界面。有了这个界面,您可以使用不同的实现,例如ArrayList
,LinkedList
,CopyOnWriteArrayList
等
所以你的问题也许应该改写为
ArrayList<ArrayList<String>> vs String[][]
ArrayList
是使用数组实现的列表。它有一些方法可以让你处理特定位置的元素:
void add(int index, E element);
E get(int index);
我曾经认为ArrayList
几乎和数组一样快,但我想
实际使用情况将决定实际的性能差异。下面我补充说
一些轶事实验的一些结果显示数组是
比ArrayList
更快,特别是在2-D矩阵的人口中。
这两者的访问时间似乎彼此相同。
ArrayList
为您提供了一些优势,例如您无需了解
大小提前。然而,如果我确定我需要的是一个数组而不是一个通用列表(例如,正如你所说,对于矩阵计算),那么我可以使用一个数组来获得更简洁的语法。
String s1 = array[i][j];
array[i][j] = s1;
对战
String s2 = list.get(j).get(i);
list.get(j).add(i, s2);
有关区分接口与其实现的更多信息,可以参考Oracle / Sun的本教程:
https://docs.oracle.com/javase/tutorial/collections/implementations/list.html
轶事实验
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class ArrayListVsString {
private static final int NUM_TESTS = 5;
public static void main(String[] args) {
List<List<String>> list;
String[][] array;
int height = 500;
int width = 1000;
String __ = " "; // indent
for (int n=0; n<NUM_TESTS; n++) {
System.out.format("Testing 2D matrix of %dx%d: Test %d%n"
, height, width, n);
/*
* Time to populate the matrices
*/
long startTime = System.nanoTime();
// array
String subTestArray = "2-D array";
array = new String[width][height];
for (int i=0; i<width; i++) {
for (int j=0; j<height; j++) {
array[i][j] = getRandomString();
}
}
startTime
= logElapsedTime(startTime
, __ + "creating matrix as "
+ subTestArray);
// array-list
String subTestList = "2-D array-list";
list = new ArrayList<>(height);
for (int j=0; j<height; j++) {
List<String> row = new ArrayList<>(width);
list.add(j, row);
for (int i=0; i<width; i++) {
String element = getRandomString();
list.get(j).add(i, element);
}
}
startTime
= logElapsedTime(startTime
, __ + "creating matrix as "
+ subTestList);
int hash = 0;
/*
* Time to do a full walk through all the elements
*/
// array
for (int i=0; i<width; i++) {
for (int j=0; j<height; j++) {
hash += (array[i][j]).hashCode();
}
}
startTime
= logElapsedTime(startTime
, __ + "full walk of matrix as"
+ subTestArray);
// array-list
for (int j=0; j<height; j++) {
for (int i=0; i<width; i++) {
hash += list.get(j).get(i).hashCode();
}
}
startTime
= logElapsedTime(startTime
, __ + "full walk of matrix as "
+ subTestList);
list = null;
}
}
private static Random random = new Random();
private static String getRandomString() {
return String.valueOf(random.nextInt());
}
private static long logElapsedTime(long startTimeNano
, String action) {
long elapsedTimeNano = System.nanoTime() - startTimeNano;
System.out.format("%s took %,d ms%n"
, action, elapsedTimeNano/1000000);
return System.nanoTime();
}
}
<强>结果
Testing 2D matrix of 500x1000: Test 0
creating matrix as 2-D array took 117 ms
creating matrix as 2-D array-list took 232 ms
full walk of matrix as2-D array took 25 ms
full walk of matrix as 2-D array-list took 31 ms
Testing 2D matrix of 500x1000: Test 1
creating matrix as 2-D array took 65 ms
creating matrix as 2-D array-list took 186 ms
full walk of matrix as2-D array took 20 ms
full walk of matrix as 2-D array-list took 30 ms
Testing 2D matrix of 500x1000: Test 2
creating matrix as 2-D array took 61 ms
creating matrix as 2-D array-list took 60 ms
full walk of matrix as2-D array took 14 ms
full walk of matrix as 2-D array-list took 15 ms
Testing 2D matrix of 500x1000: Test 3
creating matrix as 2-D array took 66 ms
creating matrix as 2-D array-list took 358 ms
full walk of matrix as2-D array took 16 ms
full walk of matrix as 2-D array-list took 15 ms
Testing 2D matrix of 500x1000: Test 4
creating matrix as 2-D array took 45 ms
creating matrix as 2-D array-list took 55 ms
full walk of matrix as2-D array took 14 ms
full walk of matrix as 2-D array-list took 15 ms