0

Any explanation on different CPU usages from these two scenarios:

  1. when using qPow(x,2), CPU usage 14%;
  2. instead x*x, drops to 3%

I just write a simple Qt UI dealing with some img processing.

Very appreciated to any feedbacks!!

For qPow(x,2), it is Okay to use x*x; but what if I need qCos for instance.

Yangroot
  • 1
  • 2
  • try `ldexp()` for speed. – user1095108 Jan 25 '23 at 12:01
  • ldexp() gives a usage in between, about 7%. – Yangroot Jan 25 '23 at 12:13
  • `pow(x, 2)` is a special case where there's a huge amount to gain from manually optimizing to not call a function at all, especially not one that accepts arbitrary floating-point powers so works by `exp(log(x) * power)` or similar. For `qCos` or standard C `cos`, is you input always 0 or pi/2? If so, just compare and pick between `1.0` or `0.0`. Otherwise, it's not a special case that can be computed much more quickly. Depending how much precision you need, you might use a faster approximation than the math library, e.g. with lower degree polynomials. – Peter Cordes Jan 25 '23 at 14:28
  • Also, for image processing you're presumably doing the same thing to a lot of pixels, so you want to take advantage of SIMD. With the right compiler options, some compilers may be able to vectorize `cos`, computing 4 or 8 return values in parallel for 4 or 8 different inputs. IDK how `qCos` is defined, whether the compiler can see through it to the actual `cos` and potentially vectorize. – Peter Cordes Jan 25 '23 at 14:30

0 Answers0