- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
给定一个 List
实例,将 List
的大小增加 f
的最有效方法是什么,这样新元素是原始元素的副本,与原始数组交错?
例如
f = 2
Original = [a,b,c,...,x,y,z]
New = [a,a,b,b,c,c,...,x,x,y,y,z,z]
我目前的实现是这样的:
List< Foo > interleave( List< Foo > original, int f ) {
int newSize = original.size() * f;
List< Foo > interleaved = new ArrayList< Foo >( newSize );
for( Foo foo : original ) {
for( int j = 0; j < factor; j++ ) {
interleaved.add( new Foo( foo ) );
}
}
}
问题是我的原始列表可能非常大,因此性能不是很好。我有一种预感,有一种更有效的方法可以做到这一点;有人有什么建议吗?
最佳答案
您提供的代码优化得很好,但还有一些可以改进的地方;重要的取决于您的具体需求。
首先,如果您的克隆元素将保持与原始元素相同的值,或者只有少数元素(与总数相比)将更改它们的值,您可能需要考虑引用 -基于克隆而不是当前的“真正克隆所有”代码,如果不是完全不同的方法甚至不创建新列表的话。
/**
* PROS:
* -Very low memory-footprint, as no new objects are created in memory, just references to a single (original) object.
* -Can be done with generalization; A single method will function for most classes and data-types, as is below.
*
* CONS:
* -If you need each clone element to be changed independently from eachother and/or the orininal, this will not work directly,
* because any change to an reference-element will apply to all other reference-elements that point to that same Object.
*
* @param <E> Sub-class generalizator. Used so that the returned list has the same sub-class as the source.
* @param list Source list. The list containing the elements to be interleaved.
* @param f The factor to interleave for. In effect, the number of resulting elements for each original.
* @return A list containing the interleaved elements, with each element being a REFERENCE to the original object.
*/
public static <E> List<E> interleaveByReference(List<E> list, int f) {
List<E> interleaved = new ArrayList<E>(list.size() * f);
for (E obj : list) {
for (int i = 0; i < f; i++) {
interleaved.add(obj);
}
}
return interleaved;
}
如果您只需要几个克隆来更改值,您的交错列表最好基于引用,并且需要更改的元素稍后单独替换。
但是请注意,这种方法的有效性在很大程度上取决于需要更改原始列表元素的数量;如果需要更改太多,这种方法虽然在内存占用方面仍然更好,但在速度性能方面会更差(这似乎是您主要关心的问题)。
“稍后的个体克隆”可以通过类似的方式实现:
public static void replaceWithTrueClone(List<String> list, int objIndex) {
list.add(objIndex, new String(list.get(objIndex)));
list.remove(objIndex + 1);
}
//OR
public static void replaceWithNewObject (List<String> list, int objIndex, String newObject) {
list.add(objIndex, newObject);
list.remove(objIndex + 1);
}
如果每个元素中的大部分在程序执行过程中都将具有独立的值,那么您当前的方法已经非常准确了。
可以进行两项改进。直接在代码中显示会更容易,所以这就是我要做的:
/**
* PROS:
* -Each element is an independent object, and can be set to independent values without much of an effort.
*
* CONS:
* -Each element has it's own allocated memory for it's values, thus having a much heavier memory footprint.
* -Is constructor-dependent, and thus cannot be generalized as easily;
* Each different expected class will probably need it's own method.
*
* @param list Source list. The list containing the elements to be interleaved.
* @param f The factor to interleave for. In effect, the number of resulting elements for each original.
* @return A list containing the interleaved elements.
* For each of the original elements, the first is a REFERENCE, and the other are CLONES.
*/
public static List<String> interleaveByClone(List<String> list, int f) {
List<String> interleaved = new ArrayList<String>(list.size() * f);
for (String obj : list) {
interleaved.add(obj); //The first element doesn't have to be cloned, I assume.
//If it has to be cloned, delete the line above, and change 'i=1' to 'i=0' on the line below.
for (int i = 1; i < f; i++) {
interleaved.add(new String(obj));
}
}
return interleaved;
}
/*
* What was changed from the original is commented below.
*/
public static List<String> original(List<String> original, int factor) {
/*
* It is unnessessary to have this 'newSize' variable. It gets needlessly maintained until the end of the method.
* Although the impact is unworthy of measurement (negligible), it still exists.
*/
int newSize = original.size() * factor;
List<String> interleaved = new ArrayList<String>(newSize); //Just do the '*factor' operarion directly, instead of 'newSize'.
for (String foo : original) {
/*
* If you can use the original here, that's one less cloning operation (memory-allocation, etc...) per original element.
* A low-impact optimization, but still a good one.
*/
for (int j = 0; j < factor; j++) {
interleaved.add(new String(foo));
}
}
return interleaved;
}
使用包含 200 万个元素的原始列表和 2 的因子,我在 10 次运行中得到以下平均速度:
interleaveByReference()
方法。interleaveByClone()
方法。原始()
方法。关于java - 将列表与自身交错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12163291/
我有两个列表,保证第一个比第二个多一个项目。我想知道创建一个新列表的最 Pythonic 方法,该列表的偶数索引值来自第一个列表,奇数索引值来自第二个列表。 # example inputs list
我正在尝试构建一个导航,它在显示菜单/导航时错开包含的列表项的外观。 我有一个汉堡符号,单击它时会呈现导航(全屏)。我现在想要一个动画,其中不同的列表项(实际链接)出现时彼此有一些延迟,顶部的是第一个
我想在 jquery 1.3 中为一系列项目制作动画,每个下一个项目在第一个动画的中途开始。换句话说,我想要一个半队列的效果。我尝试使用下面的代码,但它不起作用。有人有什么想法吗? $("h3
我按照这篇文章创建了交错布局。 No good example about RecyclerView and StaggeredGridLayoutManager in Android Docs 当我
我有一个包含数据和类的表,比如 ---------------- | DATA | Class | ---------------- | 1 | A | | 2 | A |
这个问题在这里已经有了答案: Interweaving two numpy arrays (13 个答案) 关闭 4 年前。 我正在尝试如下交错数组。 import numpy as np x =
我想创建两个输出交错的线程,如下所示 Thread1:1=>Ping! Thread2:2=>Pong! Thread1:3=>Ping! Thread1:4=>Ping! Thread2:
这个问题在这里已经有了答案: How to elegantly interleave two lists of uneven length? (9 个回答) How to interleave tw
我试图在 GridView 中显示字符串的动态列表。每个单词都是可点击的,可以选择或取消选择。我附上了 Flipboard 的屏幕截图,因为我想要完全相同的功能。 请帮助我找出要在我的应用中实现的相同
这个问题在这里已经有了答案: Merge two lists (6 个回答) 4年前关闭。 有没有办法可以合并 2 个列表 let a = ["a"; "b"; "c"] let b = ["d";
我正在尝试使用 tf.data.Dataset 来交错两个数据集,但这样做时遇到问题。给出这个简单的例子: ds0 = tf.data.Dataset() ds0 = ds0.range(0, 10,
我有一个元素数组 1 5 9(例如 a1 a2 a3) 第二个元素数组 2 4 8 (e.g. b1 b2 b3) 我希望输出为 1,2 5,4 9,8(即 a1,b1 a2,b2 a3,b3)...
我这里有解决方案代码: // Pre-condition: The fronts of two linked lists are provided. // Post-condition: A link
我有一个矩阵,我想根据下面描述的以下方案按行进行洗牌: 我们有矩阵a: import numpy as np np.random.seed(0) low = -5 high = 5 num_frame
我继承了一些在 Linux 嵌入式平台上运行的 ALSA 代码。现有的实现使用 snd_pcm_readi() 和 snd_pcm_writei() 来阻塞读取和写入。 我的任务是让它在 ARM 处理
我如何将 2 个矩阵 A、B 合并为一个,以便新矩阵 C = A 的第 1 行,然后是 B 的第 1 行,然后是 A 的第 2 行,B 的第 2 行,A 的第 3 行,行B 的 3 等等?最好没有 f
如果我像这样迭代 std::map: typedef std::map clist; clist m_connections; for (const auto itt : m_connections)
我在弄清楚 boost 图像库时遇到了一些问题。 我找不到任何关于如何使用 boost::gil 库中包含的 interleaved_view 函数的确切文档。更具体地说,我不知道原始数据应该以什么二
而不是通过编写创建对象: obj: object [ name: "Fork" id: 1020 ] 我想写一些类似... obj: something-or-another [nam
import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:lan
我是一名优秀的程序员,十分优秀!