Profiling scala for loops using hprof
Asked Answered
V

2

6

Word on the street is that for loops in scala are slower than while loops.

Slow:

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

Fast:

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

How do I use hprof to tell whether the for loops are the bottleneck in my code? I'm profiling my code using -agentlib:hprof=cpu=samples, what would the method be in the "CPU SAMPLES" section?

I'd like to know where to focus my optimization efforts. Are for loops the bottleneck?

Venerate answered 16/10, 2011 at 21:54 Comment(0)
K
3

I think you may have more luck with tools specialized with profiling such as yourkit or visualvm.

They usually have interface to capture CPU sample and then drill down to see what calls consumed most CPU cycles.

Bottlenecks of any kind would show up (like taking 95% of the CPU time) and then you could drill down until you see what methods of yours (or the library) is on the call stack for those hot spots. Then you can see if for loops are involved.

Kurland answered 17/10, 2011 at 0:2 Comment(0)
I
2

Put each loop in its own method, then compare the time taken by the methods. And use enough iterations to actually take some time (or wrap those in another loop). 10000 iterations should take microseconds; that's hard for a profiler to measure. Try a billion (or 100k iteratons of 10k iterations).

Also, if f(i) is expensive, that will take far more time than the loop will. Also, if f(i) doesn't actually do anything, it might get optimized away entirely. So make sure it does (e.g. update a counter somewhere, compute a sum, or something).

Improvement answered 17/10, 2011 at 2:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.