I need to compute power (10, n)
Is it OK to use Math.Pow (10, n)
?
Or should I use a loop?
for (int i = 0; i < n; i++){
x*=10;
}
Which one is better? and why?
Working of pow() function with integers The pow() function takes 'double' as the arguments and returns a 'double' value. This function does not always work for integers. One such example is pow(5, 2).
The expression math:pow(2, 3) returns 8.0e0 .
You can consider Math. pow to be O(1).
pow treats 0**0 as 1 . This is the oldest defined version. If the power is an exact integer the result is the same as for pown , otherwise the result is as for powr (except for some exceptional cases).
Math.Pow
is better.
Here's a rule of thumb - in 99% of the scenarios, favor built-in functions over custom implementations. This makes your code clearer, saves you lots of work, and reduce chances for errors.
Only when you think of using built-in functions in ways they weren't meant to be used, or when they have severe latency problems (never encountered these scenarios myself, to be honest), should you consider building your own implementation.
If both base and exponent are integers you might consider not using Pow. But even in that case Pow is usually better because its more readable. If at least one is a floatingpoint value, use Pow.
If the exponent is 0.5 you should use Sqrt, and if the exponent is a small integer (2,3,4) expressing the formula with multiplications is faster, but less readable.
If you want to implement fast exponentiation with an integer exponent the Square-and-Multiply algorithm and not a simple loop might be what you want. But in most scenarios Pow is still faster.
For integers, maybe a for loop is faster than Math.Pow
which probably deals with floating-point numbers. But I seriously doubt that the difference is significant in your case (even though I do not know it).
But if you work with 32-bit signed integers, then you can only store the values of 10^n for n <= 9. But then you would gain speed (and maybe readability) by storing these nine (ten) powers of ten in an array. This is not hard: they are (1), 10, 100, 1000, ... .
If you need to compute 10^n for larger n, you do need to use floating-point numbers. And then there is no reason whatsoever not to use Math.Pow
. This is as fast as it gets, and easily readable.
Depends on which one communicates "10 to the power of n" more clearly.
In my case, Math.Pow(10, n)
(although it could mean math, collectively, punching 10 and n in their faces as well, I dunno).
It's similar to how I'd rather algebraically express "10 to the power of n" as 10^n
(or 10n
) than 10 * 10 * 10 * ... * 10 n times
, especially given that n is variable.
The answer would normally be yes, use Math.Pow().
However: if this code is really time-critical and you know you're dealing with small powers 1-9 so the result can be expressed in an Int32 then it can be worth optimizing. I just made a quick test-app and profiled the two versions (making sure the compiler hadn't optimized any code away) and the result on my laptop for the worst-case of 10^9 was that the loop was 20 times faster than Math.Pow(10,9).
But please remember, maybe this calculation is not really a bottleneck after all. If you know for a fact that it is, e.g. if you've profiled your app and found it to be a real problem, then go ahead and replace it with a loop-based method (or even better, an array-lookup). If you're merely guessing that it may a problem then I would suggest you stick to Math.Pow. In general: only optimize code that you know is a performance bottleneck.
Math.Pow is provided for you, and well documented.
Any gotchas are in the documentation.
Why would you not want to use a provided function?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With