How to optimise the power computation
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2017-09-26 2:58 AM
Hi there,
I'm working on STM32F103 MCU and i need to compute the powerof a value.
I use the math.h library but I'm looking for a more optimised library, if it's exists.
Is there a better way to compute the power?
Best Regards,
F.
#math.h Note: this post was migrated and contained many threaded conversations, some content may be missing.- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2017-09-26 6:11 AM
Am I right in thinking that you are trying to compute x^y but you find it takes too long on stm32f103?
If it is taking too long then that often means you want to do it many times in quick succession, perhaps with either x or y not changing between calculations.
In C, pow(x, y) does things in double-precision. If x and y are not double-precision (e.g. float - ) then it first has to convert them to double-precision, and if you only need the result to be float then it has to convert back to float. All 3 steps waste time. powf(x, y) is faster if you only need float.
And if x or y are integers (e.g you only want to calculate x^2) then it is much faster to explicitly do x*x
How else might things be speeded up?
pow calculates exp(y*log(x))
So if x doesn't change from call to call, then you can pre-calculate the log.
I'm sure there are other things to do to improve the speed of calculation, but only because you know something about the calculation or series-of-calculations that the compiler didn't guess.
I'd expect the math library to be pretty-well optimised and fast for double-precision numbers in general.
Hope this helps,
Danish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2017-09-26 8:25 AM
In C, pow(x, y) does things in double-precision.
As usual, there is a single-precision variant in C lib too, here called
powf()
.Or, you are searching for some integer based algorithm. This function is not architecture-dependent, so you can try to port any algorithm.
Or, depending on your requirements, even a table-based approach might do, possibly with interpolation.
A lot depends on your ideas of performance, precision, and parameter range.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2017-09-26 8:25 AM
Hi Danish,
thanks for the reply.
Yes, you are right, I want to compute x^y.
Furthermore, my variables x and y are float. So I think that I can obtain the result in a short amount of time.
So, Can I use math library without problem?
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2017-09-26 8:27 AM
Basically I want use the powf to read the distance from this distance sensor: GP2Y0A41SK0F
I found a libray (for arduino ) that uses the power
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2017-09-26 8:41 AM
powf() is faster than pow() - sticking with the math library.
One point: that sensor claims its output is inversely proportional to distance.
So does this mean y is always -1.0f?
In which case you really want to calculate <scale_factor>/x ?
Another point: We're dealing with mechanical distances here. How many measurements per second is useful here?
And how many milliseconds does the pow() calculation take?
There's little point optimising something that will make very little difference to the overall system performance.
- Danish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2017-09-26 8:45 AM
The sensor generate a new measure every 25 milliseconds. Also I need to compute an average of these values. So I think that I'll compute the distance every 200 - 500 ms.
this is the first time that I use this distance sensor ( with exponential output ) and I don't now the best way to compute the distance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2017-09-26 8:59 AM
For a (supposedly) hobby project, using powf() or even pow() would be fine, there seems plenty of time for the calculation.
But I sense the author of the Arduino lib did not spend much thoughts about resources or performance - which does not come as a surprise...
So does this mean y is always -1.0f?
In which case you really want to calculate <scale_factor>/x ?
I would consider Danish's comment.
A division is cheaper than a (usually) Taylor series based function, in terms of runtime.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2017-09-26 9:02 AM
So It's better to use a division instead of pow function?
I'm not working on a hobby project.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2017-09-26 9:11 AM
Another point worth considering:
Is it better to calculate
average(complex-function(instantaneous-reading))
or
complex-function(average(instantaneous-reading))
Because the latter only needs to call complex-function once!
Danish
