I configured yourkit on my pc and connect it to the IDE.
After that I ran the program using profiler and I hit heap size limit. So the program was killed. Therefore, I changed the for loop in temp function a litter bit.
here is the heap size limit execption I get:
here is the new temp function:
public static void temp() {
ArrayList a = new ArrayList();
for (int i = 0; i < 3000; i++)
{
for (int j = 0; j < 20000; j++) {
a.add(i + j);
}
}
}
After this modification the program executed flawlessly.
Here is the cpu profile in yourkit.
Here is the memory profile in yourkit.
As you can see the temp function is the main memory and cpu bottleneck in the program.
The sum function is a simple function that calculates the sum of numbers from 0 to a given number n
. The implementation of the sum function in this example is provided in two different methods: sum_bad
and sum_good
.
The sum_bad
method implements the sum function using a simple loop that iterates throw all the numbers from 0 to n and adds eac them up one by one. This implementation has a time complexity of O(n) (one degree) and is considered to be inefficient for large values of n.
The sum_good
method, on the other hand, implements the sum function using the arithmetic progression formula. This implementation has a time complexity of O(1) (0 degrees) and is much more efficient than the sum_bad implementation for large values of n.
The complex_operation_1
and complex_operation_2
methods use the sum_bad and sum_good methods, respectively, to perform some complex computations. These methods use a loop that iterates over a large number of times (1000000) and performs some mathematical operations using the result of the sum function.
Inside the loop in complex_operations, the sum method is called with an increasing value of n + i, where i is the loop variable. The result of the sum is then used in a mathematical operation to update the result variable. Specifically, if the loop index i is even, the result is divided by the result of the sum, otherwise, it is multiplied by the result of the sum.
This code is used to demonstrate the performance difference between the sum_bad and sum_good implementations of the sum function. Profiling the code with YourKit Java Profiler can help identify the performance bottlenecks in the code and optimize the implementation for improved performance.
some snapshots from yourkit using complex_operation_1
:
But after I used sum_good instead of sum_bad we have the following result which is insignifcant memory or cpu usage.