What would be the fastest way preferably native to java to check if all elements of an array are equal to a value? (denoted here by n)
什么是最好的方式,最好是java本机检查数组的所有元素是否等于一个值? (在此表示为n)
So far I have:
到目前为止我有:
boolean check = true;
int len = times.length;
for(int a = 0; check && a < len; a++) {
check = times[a]==n && check;
}
So if each element is equal to a value, check is set to true, otherwise it is set to false.
因此,如果每个元素等于一个值,则check设置为true,否则设置为false。
EDIT: Would this be faster?
编辑:这会更快吗?
boolean check = true;
int len = times.length
int a = 0;
while(a < len && times[a]==n) {
a++;
}
check=(a==len);
Ok, after looking at the answers here I understand the code is as small as its gonna get, so I'll have to look into threads and parallel processing, thanks everyone for the help and the links
好的,看了这里的答案后,我理解代码的规模和它一样小,所以我将不得不研究线程和并行处理,感谢大家的帮助和链接
5 个解决方案
#1
4
This algorithm is O(n)
which is the fastest way to check all elements of a list because you only have to check each element only one time.
该算法是O(n),这是检查列表中所有元素的最快方法,因为您只需要检查每个元素一次。
Now just because this is the fastest algorithm
in finding if all elements equal a value doesn't mean you have optimized it to its fullest potential.
现在只是因为这是查找所有元素是否等于值的最快算法,并不意味着您已将其优化到最大潜力。
This then leaves room for a multi-threaded/multiprocessor
implementation.
这为多线程/多处理器实现留下了空间。
A solution using more cores or threads is splitting the array into the amount of thread/cores you want to process simultaneously i.e. if you have an array of 100 elements and want 10 threads running at the same time-- split the array into 10 pieces and run each section on the array.
使用更多内核或线程的解决方案是将数组拆分为您想要同时处理的线程/内核数量,即如果您有一个包含100个元素的数组并且希望同时运行10个线程 - 将数组拆分为10个并且运行数组上的每个部分。
Some psudo code:
一些psudo代码:
int from,to, threadCount = 10;
boolean check[threadCount];
int factor = array.length()/threadCount;
for(int i = 0; i < threadCount; i++){
from = i*factor;
to = i*factor+factor;
newThreadProcess(from, to, array, check[i]);
}
barrier(); //Wait till all the threads are done processing
for(int i = 0; i < threadCount; i++){
if(!check[i]) return false;
}
return true;
This is best when the array is very large
当阵列非常大时,这是最好的
#2
15
In Java 8, you could use the Stream API:
在Java 8中,您可以使用Stream API:
boolean check = Arrays.asList(times).stream().allMatch(t -> t == n);
This alone won't be faster than iterating over the array directly. But if you then switch to a parallel stream, it could be significantly faster on large arrays. And if performance is a concern, it's presumably large arrays that you care about.
仅这一点并不比直接迭代数组更快。但是,如果您随后切换到并行流,则在大型阵列上可能会明显更快。如果性能是一个问题,它可能是您关心的大型阵列。
boolean check = Arrays.asList(times).parallelStream().allMatch(t -> t == n);
A parallel stream allows the array scanning to be parcelled out to multiple threads, scanning the array using parallel CPUs or cores.
并行流允许将阵列扫描分配到多个线程,使用并行CPU或内核扫描阵列。
#3
2
check = times[a]==n && check;
can be
可
check = times[a]==n;
since you never would've gotten to that line unless check
were true.
因为除非检查是真的,否则你永远不会达到那条线。
#4
2
A simple and clean way:
简单干净的方式:
public static boolean allEqual(E[] array, E element) {
for(E e : array) {
if(e != element) {
return false;
}
}
return true;
}
#5
1
You can write it in a shorter way :
你可以用更短的方式写它:
for(int a = 0; a < len && (check = times[a]==n); a++);
#1
4
This algorithm is O(n)
which is the fastest way to check all elements of a list because you only have to check each element only one time.
该算法是O(n),这是检查列表中所有元素的最快方法,因为您只需要检查每个元素一次。
Now just because this is the fastest algorithm
in finding if all elements equal a value doesn't mean you have optimized it to its fullest potential.
现在只是因为这是查找所有元素是否等于值的最快算法,并不意味着您已将其优化到最大潜力。
This then leaves room for a multi-threaded/multiprocessor
implementation.
这为多线程/多处理器实现留下了空间。
A solution using more cores or threads is splitting the array into the amount of thread/cores you want to process simultaneously i.e. if you have an array of 100 elements and want 10 threads running at the same time-- split the array into 10 pieces and run each section on the array.
使用更多内核或线程的解决方案是将数组拆分为您想要同时处理的线程/内核数量,即如果您有一个包含100个元素的数组并且希望同时运行10个线程 - 将数组拆分为10个并且运行数组上的每个部分。
Some psudo code:
一些psudo代码:
int from,to, threadCount = 10;
boolean check[threadCount];
int factor = array.length()/threadCount;
for(int i = 0; i < threadCount; i++){
from = i*factor;
to = i*factor+factor;
newThreadProcess(from, to, array, check[i]);
}
barrier(); //Wait till all the threads are done processing
for(int i = 0; i < threadCount; i++){
if(!check[i]) return false;
}
return true;
This is best when the array is very large
当阵列非常大时,这是最好的
#2
15
In Java 8, you could use the Stream API:
在Java 8中,您可以使用Stream API:
boolean check = Arrays.asList(times).stream().allMatch(t -> t == n);
This alone won't be faster than iterating over the array directly. But if you then switch to a parallel stream, it could be significantly faster on large arrays. And if performance is a concern, it's presumably large arrays that you care about.
仅这一点并不比直接迭代数组更快。但是,如果您随后切换到并行流,则在大型阵列上可能会明显更快。如果性能是一个问题,它可能是您关心的大型阵列。
boolean check = Arrays.asList(times).parallelStream().allMatch(t -> t == n);
A parallel stream allows the array scanning to be parcelled out to multiple threads, scanning the array using parallel CPUs or cores.
并行流允许将阵列扫描分配到多个线程,使用并行CPU或内核扫描阵列。
#3
2
check = times[a]==n && check;
can be
可
check = times[a]==n;
since you never would've gotten to that line unless check
were true.
因为除非检查是真的,否则你永远不会达到那条线。
#4
2
A simple and clean way:
简单干净的方式:
public static boolean allEqual(E[] array, E element) {
for(E e : array) {
if(e != element) {
return false;
}
}
return true;
}
#5
1
You can write it in a shorter way :
你可以用更短的方式写它:
for(int a = 0; a < len && (check = times[a]==n); a++);