Is multiplication faster than array access? - java

To my surprise I get a longer time (10 milliseconds) when "optimizing" multiplications by pregenerating the results in an array compared to the original 8 milliseconds. Is that just a Java quirk or is that general of the PC architecture? I have a Core i5 760 with Java 7, Windows 8 64 Bit.
public class Test {
public static void main(String[] args) {
long start = System.currentTimeMillis();
long sum=0;
int[] sqr = new int[1000];
for(int a=1;a<1000;a++) {sqr[a]=a*a;}
for(int b=1;b<1000;b++)
// for(int a=1;a<1000;a++) {sum+=a*a+b*b;}
for(int a=1;a<1000;a++) {sum+=sqr[a]+sqr[b];}
System.out.println(System.currentTimeMillis()-start+"ms");
System.out.println(sum);
}
}

Konrad Rudolph commented on the issues with the benchmarking. So I am ignoring the benchmark and focus on the question:
Is multiplication faster than array access?
Yes, it is very likely. It used to be the other way around 20 or 30 years ago.
Roughly speaking, you can do an integer multiplication in 3 cycles (pessimistic, if you don't get vector instructions), and a memory access costs you 4 cycles if you get it straight from the L1 cache but it is straight downhill from there. For reference, see
Latencies and throughput in Appendix C of the Intel 64 and IA-32 Architectures Optimization Reference Manual
Approximate cost to access various caches and main memory?
Herb Sutter's presentation on this very subject: Machine Architecture: Things Your Programming Language Never Told You
One thing specific to Java was pointed out by Ingo in a comment below: You also get bounds checking in Java, which makes the already slower array access even slower...

A more reasonable benchmark would be:
public abstract class Benchmark {
final String name;
public Benchmark(String name) {
this.name = name;
}
abstract int run(int iterations) throws Throwable;
private BigDecimal time() {
try {
int nextI = 1;
int i;
long duration;
do {
i = nextI;
long start = System.nanoTime();
run(i);
duration = System.nanoTime() - start;
nextI = (i << 1) | 1;
} while (duration < 1000000000 && nextI > 0);
return new BigDecimal((duration) * 1000 / i).movePointLeft(3);
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
#Override
public String toString() {
return name + "\t" + time() + " ns";
}
private static void shuffle(int[] a) {
Random chaos = new Random();
for (int i = a.length; i > 0; i--) {
int r = chaos.nextInt(i);
int t = a[r];
a[r] = a[i - 1];
a[i - 1] = t;
}
}
public static void main(String[] args) throws Exception {
final int[] table = new int[1000];
final int[] permutation = new int[1000];
for (int i = 0; i < table.length; i++) {
table[i] = i * i;
permutation[i] = i;
}
shuffle(permutation);
Benchmark[] marks = {
new Benchmark("sequential multiply") {
#Override
int run(int iterations) throws Throwable {
int sum = 0;
for (int j = 0; j < iterations; j++) {
for (int i = 0; i < table.length; i++) {
sum += i * i;
}
}
return sum;
}
},
new Benchmark("sequential lookup") {
#Override
int run(int iterations) throws Throwable {
int sum = 0;
for (int j = 0; j < iterations; j++) {
for (int i = 0; i < table.length; i++) {
sum += table[i];
}
}
return sum;
}
},
new Benchmark("random order multiply") {
#Override
int run(int iterations) throws Throwable {
int sum = 0;
for (int j = 0; j < iterations; j++) {
for (int i = 0; i < table.length; i++) {
sum += permutation[i] * permutation[i];
}
}
return sum;
}
},
new Benchmark("random order lookup") {
#Override
int run(int iterations) throws Throwable {
int sum = 0;
for (int j = 0; j < iterations; j++) {
for (int i = 0; i < table.length; i++) {
sum += table[permutation[i]];
}
}
return sum;
}
}
};
for (Benchmark mark : marks) {
System.out.println(mark);
}
}
}
which prints on my intel core duo (yes, it's old):
sequential multiply 2218.666 ns
sequential lookup 1081.220 ns
random order multiply 2416.923 ns
random order lookup 2351.293 ns
So, if I access the lookup array sequentially, which minimizes the number of cache misses, and permits the hotspot JVM to optimize bounds checking on array access, there is a slight improvement on an array of 1000 elements. If we do random access into the array, that advantage disappears. Also, if the table is larger, the lookup gets slower. For instance, for 10000 elements, I get:
sequential multiply 23192.236 ns
sequential lookup 12701.695 ns
random order multiply 24459.697 ns
random order lookup 31595.523 ns
So, array lookup is not faster than multiplication, unless the access pattern is (nearly) sequential and the lookup array small.
In any case, my measurements indicate that a multiplication (and addition) takes merely 4 processor cycles (2.3 ns per loop iteration on a 2GHz CPU). You're unlikely to get much faster than that. Also, unless you do half a billion multiplications per second, the multiplications are not your bottleneck, and optimizing other parts of the code will be more fruitful.

Related

Why is running time of the same algorithm different in doubling ratio test?

I am analyzing brute force Three Sum algorithm. Let's say the running time of this algorithm is T(N)=aN^3. What I am doing is that I am running this ThreeSum.java program with 8Kints.txt and using that running time to calculate constant a. After calculating a I am guessing what the running time of 16Kints.txt is. Here is my ThreeSum.java file:
public class ThreeSum {
public static int count(int[] a) {
// Count triples that sum to 0.
int N = a.length;
int cnt = 0;
for (int i = 0; i < N; i++)
for (int j = i + 1; j < N; j++)
for (int k = j + 1; k < N; k++)
if (a[i] + a[j] + a[k] == 0)
cnt++;
return cnt;
}
public static void main(String[] args) {
In in = new In(args[0]);
int[] a = in.readAllInts();
Stopwatch timer = new Stopwatch();
int count = count(a);
StdOut.println("elapsed time = " + timer.elapsedTime());
StdOut.println(count);
}
}
When I run like this:
$ java ThreeSum 8Kints.txt
I get this:
elapsed time = 165.335
And now in doubling ratio experiment where I use the same method inside another client and run this client with multiple files as arguments and wanna try to compare the running time of 8Kints.txt with above method but I get different result actually faster result. Here is my DoublingRatio.java client:
public class DoublingRatio {
public static double timeTrial(int[] a) {
Stopwatch timer = new Stopwatch();
int cnt = ThreeSum.count(a);
return timer.elapsedTime();
}
public static void main(String[] args) {
In in;
int[][] inputs = new int[args.length][];
for (int i = 0; i < args.length; i++) {
in = new In(args[i]);
inputs[i] = in.readAllInts();
}
double prev = timeTrial(inputs[0]);
for (int i = 1; i < args.length; i++) {
double time = timeTrial(inputs[i]);
StdOut.printf("%6d %7.3f ", inputs[i].length, time);
StdOut.printf("%5.1f\n", time / prev);
prev = time;
}
}
}
When I run this like:
$ java DoublingRatio 1Kints.txt 2Kints.txt 4Kints.txt 8Kints.txt 16Kints.txt 32Kints.txt
I get faster reuslt and I wonder why:
N sec ratio
2000 2.631 7.8
4000 4.467 1.7
8000 34.626 7.8
I know it is something that has to do with Java not the algorithm? Does java optimizes some things under the hood.

Quicksort Multithreading

I'd like to implement a quicksort algorthmus for a 2D array with multithreading.
Its working very fast in Single thread, but now i Tried to speed it up. This is my code to sort every part of the 2d array correctly (the speed of the sorting algorithm himself should be very fast). Its directly working on the "c".
public static void sort(int[][] c) {
int[][] a = new int[][] { { 0, -4, 1, 2 }, { 1, 0, 3 }, { 2, 3, 0 } };
for (int i = 0; i < c.length; i++) {
sort(c[i],0,c[i].length-1);
}
}
I tried up to know:
split the for loop in small "loopers" which perform a task of "x" loops, but this is slowing the algorithm.
Can someone help me to speed it up?
A couple of possibilities:
public static void sort(int[][] c) {
for (int i = 0; i < c.length; i++) {
//sort(c[i],0,c[i].length-1);
Arrays.sort(c[i]);
}
}
public static void parallelSort(int[][] c) {
Arrays.asList(c).parallelStream().forEach(d -> Arrays.sort(d));
}
public static void threadedSort(int[][] c) throws InterruptedException {
int count = 4;
Thread[] threads = new Thread[count];
for (int i = 0; i < count; i++) {
final int finalI = i;
threads[i] = new Thread(
() -> sortOnThread(c, (c.length / count) * finalI, c.length / count),
"Thread " + i
);
threads[i].start();
}
for (Thread thread : threads) {
thread.join();
}
}
private static void sortOnThread(int[][] c, int first, int length) {
for (int i = first; i < first + length; i++) {
Arrays.sort(c[i]);
}
}
public static void main(String[] args) throws InterruptedException {
int[][] c = new int[10_000_000][75];
shuffle(c);
System.out.println("Starting sort()");
long before = System.currentTimeMillis();
sort(c);
System.out.println("Took " + (System.currentTimeMillis() - before) + "ms");
shuffle(c);
System.out.println("Starting parallelSort()");
before = System.currentTimeMillis();
parallelSort(c);
System.out.println("Took " + (System.currentTimeMillis() - before) + "ms");
shuffle(c);
System.out.println("Starting threadedSort()");
before = System.currentTimeMillis();
threadedSort(c);
System.out.println("Took " + (System.currentTimeMillis() - before) + "ms");
}
private static void shuffle(int[][] c) {
for (int i = 0; i < c.length; i++) {
for (int j = 0; j < c[i].length; j++)
c[i][j] = j;
Collections.shuffle(Arrays.asList(c[i]));
}
}
Which produced these timings on a quad core (i5-2430M):
Starting sort()
Took 2486ms
Starting parallelSort()
Took 984ms
Starting threadedSort()
Took 875ms
The parallelStream() approach was the least code, but clearly comes with a bit more overhead (sending each sort through the ForkJoinPool) than direct threading. This was more noticable when the array was the smaller [100_000] [75]:
Starting sort()
Took 48ms
Starting parallelSort()
Took 101ms
Starting threadedSort()
Took 21ms
Just in case it's useful ... initially while coding this, I found the timings for the three approaches were much more similar:
Starting sort()
Took 2403ms
Starting parallelSort()
Took 2435ms
Starting threadedSort()
Took 2284ms
This turned out to be because I was naively allocating new sub-arrays each time in my shuffle() method. Clearly this was generating a lot of extra GC work - even a short sleep before the calling the sort methods made all the difference.

how to find max value of the two-dimensional array in java using multithreading

I want to find the maximum value of the two-dimensional array. I found this value without using multithreading. How do I find the maximum value of the two-dimensional array using multithreading? I want to compare the speed of finding the maximum value of the array in different ways.
public class Search {
public int[][] fillMatrix(int matrix[][]) {
for (int i = 0; i < matrix.length; i++){
for (int j = 0; j < matrix[i].length; j++){
matrix[i][j] = (int)(Math.random() * 1000);
}
}
return matrix;
}
public int searchMaxValue(int[][] matrix, int row, int column) {
int max = matrix[0][0];
for (int a = 0; a < row; a++) {
for (int b = 0; b < column; b++) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (matrix[a][b] > max) {
max = matrix[a][b];
}
}
}
return max;
}
public static void main(String[] args) {
Search search = new Search();
int[][] matrix = new int[4][100];
search.fillMatrix(matrix);
long start = System.currentTimeMillis();
int max = search.searchMaxValue(matrix, 4, 100);
long end = System.currentTimeMillis();
System.out.println("Max value is " + max);
System.out.println("Time for execution: " + (end - start));
}
}
Here is the outline how you would go about implementing this. I am not providing code intentionally, so that you can have the joy of implementing it yourself.
create a method to findmax out of an array lets call it findMax(int[]
input)
for each sub array in 2D array (can be accessed using matrix[i])
start a thread to findMax(matrix[i]) (hint: use ExecutorService)
in the thread, once max is found, fill it in to ith position
of a one dimensional array called results in the thread, indicate
its completion(hint: use CountDownLatch)
In the main thread, wait till all threads finish ( hint: use
CountDownLatch) Now call findMax(results) and you have the
maxiumum from matrix.
Considerations: Do we need to fork as many threads as the rows in matrix? So do we use a FixedThreadPool with number of rows ?
The simplest way would be to start a thread to calculate the maximum in each of the four rows in the matrix, have the main thread join all of these row threads and calculate the maximum of the four row maxima. You will likely need a much larger array to be able to see the time difference, though. Don't neglect the necessary synchronization.
If as I suspect you are looking for code, you should make an attempt at the solution and run it, then repost or elaborate this post with the problems you run into.
Here is how to do it in java 8:
int[][] values = fillMatrix(new int[1000][1000]);
OptionalInt max = Arrays.stream(values)
.parallel()
.flatMapToInt(Arrays::stream)
.parallel()
.max();
But frankly speaking, I'm not sure that for such simple computation it makes sense to use several threads, indeed creating and orchestrating threads has a price which seems to be too high in this case.
Response Update
As it is your homework, I provide an answer without any comments on purpose in order to ensure that you will at least think about how it works, but the main idea is to provide chunk of data to each thread to avoid collisions as next:
int[][] matrix = fillMatrix(new int[100][100]);
int totalThreads = 10;
int[] results = new int[totalThreads];
int chunkSize = matrix.length / totalThreads;
CountDownLatch end = new CountDownLatch(totalThreads);
for (int i = 0; i < totalThreads; i++) {
int threadIndex = i;
new Thread(
() -> {
int max = -1;
int startIndex = threadIndex * chunkSize;
for (int j = startIndex; j < startIndex + chunkSize && j < matrix.length; j++) {
for (int k = 0; k < matrix[j].length; k++) {
if (max == -1 || max < matrix[j][k]) {
max = matrix[j][k];
}
}
}
results[threadIndex] = max;
end.countDown();
}
).start();
}
end.await();
int max = results[0];
for (int k = 1; k < results.length; k++) {
if (max < results[k]) {
max = results[k];
}
}
System.out.printf("Max found %d%n", max);

Largest 5 in array of 10 numbers without sorting

Here's my code to find the max number in an array of numbers, but i can't seem to understand how to get the top 5 numbers and store them in an array and later retrieve them
Here's the code:
public class Max {
public static void main (String[] args)
{
int i;
int large[]=new int[5];
int array[] = {33,55,13,46,87,42,10,34,43,56};
int max = array[0]; // Assume array[0] to be the max for time-being
//Looping n-1 times, O(n)
for( i = 1; i < array.length; i++) // Iterate through the First Index and compare with max
{
// O(1)
if( max < array[i])
{
// O(1)
max = array[i];// Change max if condition is True
large[i] = max;
}
}
for (int j = 0; j<5; j++)
{
System.out.println("Largest 5 : "+large[j]);
}
System.out.println("Largest is: "+ max);
// Time complexity being: O(n) * [O(1) + O(1)] = O(n)
}
}
I'm using an array to store 5 numbers, but when i run it, it is not what i want.
Can anyone help me with the program?
The optimum data structure to retrieve top n items from a larger collection is the min/max heap and the related abstract data structure is called the priority queue. Java has an unbounded PriorityQueue which is based on the heap structure, but there is no version specialized for primitive types. It can used as a bounded queue by adding external logic, see this comment for details..
Apache Lucene has an implementation of the bounded priority queue:
http://grepcode.com/file/repo1.maven.org/maven2/org.apache.lucene/lucene-core/5.2.0/org/apache/lucene/util/PriorityQueue.java#PriorityQueue
Here is a simple modification that specializes it for ints:
/*
* Original work Copyright 2014 The Apache Software Foundation
* Modified work Copyright 2015 Marko Topolnik
*
* Licensed under the Apache License, Version 2.0 (the "License");
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/** A PriorityQueue maintains a partial ordering of its elements such that the
* worst element can always be found in constant time. Put()'s and pop()'s
* require log(size) time.
*/
class IntPriorityQueue {
private static int NO_ELEMENT = Integer.MIN_VALUE;
private int size;
private final int maxSize;
private final int[] heap;
IntPriorityQueue(int maxSize) {
this.heap = new int[maxSize == 0 ? 2 : maxSize + 1];
this.maxSize = maxSize;
}
private static boolean betterThan(int left, int right) {
return left > right;
}
/**
* Adds an int to a PriorityQueue in log(size) time.
* It returns the object (if any) that was
* dropped off the heap because it was full. This can be
* the given parameter (in case it isn't better than the
* full heap's minimum, and couldn't be added), or another
* object that was previously the worst value in the
* heap and now has been replaced by a better one, or null
* if the queue wasn't yet full with maxSize elements.
*/
public void consider(int element) {
if (size < maxSize) {
size++;
heap[size] = element;
upHeap();
} else if (size > 0 && betterThan(element, heap[1])) {
heap[1] = element;
downHeap();
}
}
public int head() {
return size > 0 ? heap[1] : NO_ELEMENT;
}
/** Removes and returns the least element of the PriorityQueue in log(size)
time. */
public int pop() {
if (size > 0) {
int result = heap[1];
heap[1] = heap[size];
size--;
downHeap();
return result;
} else {
return NO_ELEMENT;
}
}
public int size() {
return size;
}
public void clear() {
size = 0;
}
public boolean isEmpty() {
return size == 0;
}
private void upHeap() {
int i = size;
// save bottom node
int node = heap[i];
int j = i >>> 1;
while (j > 0 && betterThan(heap[j], node)) {
// shift parents down
heap[i] = heap[j];
i = j;
j >>>= 1;
}
// install saved node
heap[i] = node;
}
private void downHeap() {
int i = 1;
// save top node
int node = heap[i];
// find worse child
int j = i << 1;
int k = j + 1;
if (k <= size && betterThan(heap[j], heap[k])) {
j = k;
}
while (j <= size && betterThan(node, heap[j])) {
// shift up child
heap[i] = heap[j];
i = j;
j = i << 1;
k = j + 1;
if (k <= size && betterThan(heap[j], heap[k])) {
j = k;
}
}
// install saved node
heap[i] = node;
}
}
The way you implement betterThan decides whether it will behave as a min or max heap. This is how it's used:
public int[] maxN(int[] input, int n) {
final int[] output = new int[n];
final IntPriorityQueue q = new IntPriorityQueue(output.length);
for (int i : input) {
q.consider(i);
}
// Extract items from heap in sort order
for (int i = output.length - 1; i >= 0; i--) {
output[i] = q.pop();
}
return output;
}
Some interest was expressed in the performance of this approach vs. the simple linear scan from user rakeb.void. These are the results, size pertaining to the input size, always looking for 16 top elements:
Benchmark (size) Mode Cnt Score Error Units
MeasureMinMax.heap 32 avgt 5 270.056 ± 37.948 ns/op
MeasureMinMax.heap 64 avgt 5 379.832 ± 44.703 ns/op
MeasureMinMax.heap 128 avgt 5 543.522 ± 52.970 ns/op
MeasureMinMax.heap 4096 avgt 5 4548.352 ± 208.768 ns/op
MeasureMinMax.linear 32 avgt 5 188.711 ± 27.085 ns/op
MeasureMinMax.linear 64 avgt 5 333.586 ± 18.955 ns/op
MeasureMinMax.linear 128 avgt 5 677.692 ± 163.470 ns/op
MeasureMinMax.linear 4096 avgt 5 18290.981 ± 5783.255 ns/op
Conclusion: constant factors working against the heap approach are quite low. The breakeven point is around 70-80 input elements and from then on the simple approach loses steeply. Note that the constant factor stems from the final operation of extracting items in sort order. If this is not needed (i.e., just a set of the best items is enough), the we can simply retrieve the internal heap array directly and ignore the heap[0] element which is not used by the algorithm. In that case this solution beats one like rakib.void's even for the smallest input size (I tested with 4 top elements out of 32).
Look at the following code:
public static void main(String args[]) {
int i;
int large[] = new int[5];
int array[] = { 33, 55, 13, 46, 87, 42, 10, 34, 43, 56 };
int max = 0, index;
for (int j = 0; j < 5; j++) {
max = array[0];
index = 0;
for (i = 1; i < array.length; i++) {
if (max < array[i]) {
max = array[i];
index = i;
}
}
large[j] = max;
array[index] = Integer.MIN_VALUE;
System.out.println("Largest " + j + " : " + large[j]);
}
}
Note: If you don't want to change the inputted array, then make a copy of it and do the same operation on the copied array.
Take a look at Integer.MIN_VALUE.
I get the following output:
Largest 0 : 87
Largest 1 : 56
Largest 2 : 55
Largest 3 : 46
Largest 4 : 43
Here is a simple solution i quickly knocked up
public class Main {
public static void main(String args[]) {
int i;
int large[] = new int[5];
int array[] = { 33, 55, 13, 46, 87, 42, 10, 34, 43, 56 };
for (int j = 0; j < array.length; j++) {
for (i = 4; i >= 0; i--) {
if (array[j] > large[i]) {
if (i == 4) {
large[i] = array[j];
}
else{
int temp = large[i];
large[i] = array[j];
large[i+1] = temp;
}
}
}
}
for (int j = 0; j<5; j++)
{
System.out.println("Largest "+ j + ":"+ large[j]);
}
}
}
Sorting, regular expressions, complex data structures are fine and make programming easy. However, I constantly see them misused nowadays and no one has to wonder:
Even if computers have become thousands of times faster over the past decades, the perceived performance still continues to not only not grow, but actually slows down. Once in your terminal application, you had instant feedback, even in Windows 3.11 or Windows 98 or Gnome 1, you often had instant feedback from your machine.
But it seems that it becomes increasingly popular to not only crack nuts with a sledgehammer, but even corns of wheat with steam hammers.
You don't need no friggin' sorting or complex datastructures for such a small problem. Don't let me invoke Z̴̲̝̻̹̣̥͎̀A̞̭̞̩̠̝̲͢L̛̤̥̲͟͜G͘҉̯̯̼̺O̦͈͙̗͎͇̳̞̕͡. I cannot take it, and even if I don't have a Java compiler at hands, here's my take in C++ (but will work in Java, too).
Basically, it initializes your 5 maxima to the lowest possible integer values. Then, it goes through your list of numbers, and for each number, it looks up into your maxima to see if it has a place there.
#include <vector>
#include <limits> // for integer minimum
#include <iostream> // for cout
using namespace std; // not my style, I just do so to increase readability
int main () {
// basically, an array of length 5, initialized to the minimum integer
vector<int> maxima(5, numeric_limits<int>::lowest());
// your numbers
vector<int> numbers = {33, 55, 13, 46, 87, 42, 10, 34, 43, 56};
// go through all numbers.
for(auto n : numbers) {
// find smallest in maxima.
auto smallestIndex = 0;
for (auto m=0; m!=maxima.size(); ++m) {
if (maxima[m] < maxima[smallestIndex]) {
smallestIndex = m;
}
}
// check if smallest is smaller than current number
if (maxima[smallestIndex] < n)
maxima[smallestIndex] = n;
}
cout << "maximum values:\n";
for(auto m : maxima) {
cout << " - " << m << '\n';
}
}
It is a similar solution to rakeb.voids' answer, but flips the loops inside out and does not have to modify the input array.
Use steam hammers when appropriate only. Learn algorithms and datastructures. And know when NOT TO USE YOUR KUNG-FU. Otherwise, you are guilty of increasing the society's waste unecessarily and contribute to overall crapness.
(Java translation by Marko, signature adapted to zero allocation)
static int[] phresnel(int[] input, int[] output) {
Arrays.fill(output, Integer.MIN_VALUE);
for (int in : input) {
int indexWithMin = 0;
for (int i = 0; i < output.length; i++) {
if (output[i] < output[indexWithMin]) {
indexWithMin = i;
}
}
if (output[indexWithMin] < in) {
output[indexWithMin] = in;
}
}
Arrays.sort(output);
return output;
}
As an alternative to sorting, here is the logic. You figure out the code.
Keep a list (or array) of the top X values found so far. Will of course start out empty.
For each new value (iteration), check against top X list.
If top X list is shorter than X, add value.
If top X list is full, check if new value is greater than any value. If it is, remove smallest value from top X list and add new value.
Hint: Code will be better if top X list is sorted.
If you don't want to sort you can check lower number and it's position and replace. WORKING DEMO HERE.
public static void main(String[] args) {
int array[] = {33,55,13,46,87,42,10,34,43,56};
int mArray[] = new int[5];
int j = 0;
for(int i = 0; i < array.length; i++) {
if (array[i] > lower(mArray)) {
mArray[lowerPos(mArray)] = array[i];
}
}
System.out.println(Arrays.toString(mArray));
}
public static int lower(int[] array) {
int lower = Integer.MAX_VALUE;
for (int n : array) {
if (n < lower)
lower = n;
}
return lower;
}
public static int lowerPos(int[] array) {
int lower = Integer.MAX_VALUE;
int lowerPos = 0;
for (int n = 0; n < array.length; n++) {
if (array[n] < lower) {
lowerPos = n;
lower = array[n];
}
}
return lowerPos;
}
OUTPUT:
[43, 55, 56, 46, 87]
try :
public static int getMax(int max,int[] arr ){
int pos=0;
//Looping n-1 times, O(n)
for( int i = 0; i < arr.length; i++) // Iterate through the First Index and compare with max
{
// O(1)
if( max < arr[i])
{
// O(1)
max = arr[i];// Change max if condition is True
pos=i;
}
}
arr[pos]=0;
return max;
}
public static void main(String[] args) {
int large[]=new int[10];
int array[] = {33,55,13,46,87,42,10,34,43,56};
int k=0;
for(int i=0;i<array.length;i++){
large[k++]=getMax(0,array);
}
System.out.println("Largest 5 is: "+ Arrays.toString(Arrays.copyOf(large,5)));
}
output:
Largest 5 is: [87, 56, 55, 46, 43]
Here is another approach:
public static void main(String args[]){
int i;
int largestSize = 4;
int array[] = {33,55,13,46,87,42,10,34};
// copy first 4 elemets, they can just be the highest
int large[]= Arrays.copyOf(array, largestSize);
// get the smallest value of the large array before the first start
int smallest = large[0];
int smallestIndex = 0;
for (int j = 1;j<large.length;++j) {
if (smallest > large[j]) {
smallest = large[j];
smallestIndex = j;
}
}
// First Loop start one elemnt after the copy
for(i = large.length; i < array.length; i++)
{
// get the smallest value and index of the large array
if(smallest < array[i])
{
large[smallestIndex] = array[i];
// check the next smallest value
smallest = large[0];
smallestIndex = 0;
for (int j = 1;j<large.length;++j) {
if (smallest > large[j]) {
smallest = large[j];
smallestIndex = j;
}
}
}
}
for (int j = 0; j<large.length; j++)
{
System.out.println("Largest 5 : "+large[j]);
}
System.out.println();
System.out.println("Largest is: "+ getHighest(large));
}
private static int getHighest(int[] array) {
int highest = array[0];
for (int i = 1;i<array.length;++i) {
if (highest < array[i]) {
highest = array[i];
}
}
return highest;
}
First of all, you can't use the i constant with large array. i goes up to 10, while large length is 5.
Use a separate variable for that and increment when you add a new value.
Second, this logic is not retrieving the max values, you need to go over your array fully, retrieve the max value and add it to your array. Then you have to it again. You can write a first loop which use large.length as a condition and the inner loop which will use array.length. Or, you can use recursion.
You could do this properly in an OOp way. This maintains a list of the n largest values of a list of offered values.
class Largest<T extends Comparable<T>> {
// Largest so far - null if we haven't yet seen that many.
List<T> largest;
public Largest(int n) {
// Build my list.
largest = new ArrayList(n);
// Clear it.
for (int i = 0; i < n; i++) {
largest.add(i, null);
}
}
public void offer(T next) {
// Where to put it - or -1 if nowhere.
int place = -1;
// Must replace only the smallest replaceable one.
T smallest = null;
for (int i = 0; i < largest.size(); i++) {
// What's there?
T l = largest.get(i);
if (l == null) {
// Always replace null.
place = i;
break;
}
if (l.compareTo(next) < 0) {
// Only replace the smallest.
if (smallest == null || l.compareTo(smallest) < 0) {
// Remember here but keep looking in case there is a null or a smaller.
smallest = l;
place = i;
}
}
}
if (place != -1) {
// Replace it.
largest.set(place, next);
}
}
public List<T> get() {
return largest;
}
}
public void test() {
Integer array[] = {33, 55, 13, 46, 87, 42, 10, 34, 43, 56};
Largest<Integer> l = new Largest<>(5);
for (int i : array) {
l.offer(i);
}
List<Integer> largest = l.get();
Collections.sort(largest);
System.out.println(largest);
// Check it.
List<Integer> asList = Arrays.asList(array);
Collections.sort(asList);
asList = asList.subList(asList.size() - largest.size(), asList.size());
System.out.println(asList);
}
For larger numbers you can improve the algorithm using binarySearch to find the best place to put the new item instead of blindly walking the whole list. This has the added benefit of returning a sorted list.
class Largest<T extends Comparable<T>> {
// Largest so far - null if we haven't yet seen that many.
List<T> largest;
// Limit.
final int n;
public Largest(int n) {
// Build my list.
largest = new ArrayList(n + 1);
this.n = n;
}
public void offer(T next) {
// Try to find it in the list.
int where = Collections.binarySearch(largest, next, Collections.reverseOrder());
// Positive means found.
if (where < 0) {
// -1 means at start.
int place = -where - 1;
// Discard anything beyond n.
if (place < n) {
// Insert here.
largest.add(place, next);
// Trim if necessary.
if (largest.size() > n) {
largest.remove(n);
}
}
}
}
public List<T> get() {
return largest;
}
}
Simple working solution for this for all condition is as given below. Please refer code below and let me know in case of any issue in comments.
public static void main(String[] args) {
int arr[] = {75, 4, 2, 43, 56, 1,66};
int k = 5;
int result = find5thMaxValueApproach3(arr, k);
System.out.println("\n 5th largest element is : " + result);
}
static int find5thMaxValueApproach3(int arr[], int k){
int newMax = 0;
int len = arr.length;
int lastMax = Integer.MAX_VALUE;
for(int j = 0; j < k; j++){
int i = 0;
while(arr[i] >= lastMax )
i++;
if(i >= len)
break;
newMax = arr[i];
for( ; i < len; i++){
if( arr[i] < lastMax && arr[i] > newMax){
newMax = arr[i];
}
}
System.out.println("newMax =" + newMax+ " lastMax="+ lastMax);
lastMax = newMax;
}
return lastMax;
}

What is the most efficient implementation of for loop for ArrayList?

I just recently finished an android project and I have done some extensive codes of ArrayList and doing looping stuffs on it. I have two sets example of loops here.
ArrayList<SomeObject> object = new ArrayList<SomeObject>();
for(int c=0;c<object.size();c++){
}
for(SomeObject obj: object){
}
I think the second one is good if you don't need the incrementing value and I think far more readable and neat than the first. But what in the two loops is ideal or efficient to use in terms speed, performance and memory usage?
As per suggestions from the Android Documentation the most efficient ways for making loop are:
public void one() {
int sum = 0;
Foo[] localArray = mArray;
int len = localArray.length;
for (int i = 0; i < len; ++i) {
sum += localArray[i].mSplat;
}
}
public void two() {
int sum = 0;
for (Foo a : mArray) {
sum += a.mSplat;
}
}
Please, note that these ways have same performance for devices with JIT. So, in cases then JIT is off (e.g. while debugging using Eclipse) You might observe different results (two() will be faster than one())
Update
I've checked ArrayLists also by using the following code:
final ArrayList<String> testArrayList = new ArrayList<String>(1000);
for (int i = 0; i < 1000; i++) {
testArrayList.add(String.valueOf(i<<2));
}
final TimingLogger timings = new TimingLogger("Loop", "ArrayLists");
timings.addSplit("start");
for (int loopCount = 0; loopCount < 1000; loopCount++) {
int sum = 0;
for (int i = 0; i < testArrayList.size(); i++) {
sum += Integer.valueOf(testArrayList.get(i));
}
}
timings.addSplit("zero()");
for (int loopCount = 0; loopCount < 1000; loopCount++) {
int sum = 0;
final int len = testArrayList.size();
for (int i = 0; i < len; i++) {
sum += Integer.valueOf(testArrayList.get(i));
}
}
timings.addSplit("one()");
for (int loopCount = 0; loopCount < 1000; loopCount++) {
int sum = 0;
for (String item : testArrayList) {
sum += Integer.valueOf(item);
}
}
timings.addSplit("two()");
timings.dumpToLog();
And obtained the following output (with JIT):
ArrayLists: begin
ArrayLists: 0 ms, start
ArrayLists: 2713 ms, zero()
ArrayLists: 1753 ms, one()
ArrayLists: 1922 ms, two()
ArrayLists: end, 6388 ms
So, we able to see, that variants one() and two() provides similar results, and these results are faster than zero() (so, results look similar to ones described in the documentation for arrays).

Categories