Scala profiling for loops using hprof

The word on the street is that for cycles in scala is slower than in a cycle.

Slow:

for (i <- 0 until 10000) { f(i) } 

Quickly:

 var i = 0 while (i < 10000) { f(i) i += 1 } 

How to use hprof to determine if a for loop is a bottleneck in my code? I profile my code with -agentlib:hprof=cpu=samples so that this method is in the "CPU SAMPLES" section?

I would like to know where to focus on my optimization efforts. Are these nodes a bottleneck?

+6
source share
2 answers

I think you're more fortunate with profiling tools like yourkit or visualvm .

They usually have an interface to capture a processor sample, and then turn around to find out which calls consume most processor cycles.

Bottlenecks will appear (for example, take 95% of the processor’s time), and then you can turn around until you see which of your methods (or library) are in the call stack for these hotspots. You can then see if they are involved for loops.

+3
source

Put each loop in your own method, and then compare the time spent on the methods. And use enough iterations to actually spend some time (or wrap them in another loop). 10,000 iterations should take microseconds; which is difficult for the profiler to measure. Try a billion (or 100 thousand Iterators out of 10 thousand Iterations).

In addition, if f(i) expensive, it will take much longer than a cycle. In addition, if f(i) actually does nothing, it can be fully optimized. Therefore, make sure that it (for example, updates the counter somewhere, calculates the amount or something else).

+2
source

Source: https://habr.com/ru/post/899422/


All Articles