This question already has answers here:
Optimizations for pow() with const non-integer exponent?
(10 answers)
Closed 9 years ago.
I have to raise a number to the power of 1/2.2 which is 0.45454545... many times. Actually I have to do this in a loop. Simple pow/powf is very slow (when I comment out this pow from my loop code it's a loot faster). Is there any way to optimize such operation?
You might give a look at: Optimized Approximative pow() in C / C++
It also includes a benchmark.
Related
This question already has answers here:
Checking if a double (or float) is NaN in C++
(21 answers)
Closed 4 years ago.
of course I know I should write better code which just not create NaN values.
But is there any casual method to avoid it. I mean something like:
if (!(floatNumber == NaN))
// do some stupid function
else
return;
But it doesn't work for me. I also tried floatNumber==null, but also no result.
Could you please help me?
To test whether a number is NaN, you can use the standard library function std::isnan.
This question already has answers here:
How to Calculate Execution Time of a Code Snippet in C++
(18 answers)
Closed 8 years ago.
I'd like to ask, which method of measuring the time which is required for function to execute is considered the best?
Just curious.
Thanks.
You can try to use rdtsc(); it's accurate, perhaps fastest and cross-platform (but only for x86 architecture). For example, in Windows:
#include <intrin.h>
uint64_t rdtsc()
{
return __rdtsc();
}
This question already has answers here:
How to measure the inner kernel time in NVIDIA CUDA?
(2 answers)
Closed 8 years ago.
I searched a bit but all things I found could only be annotated in CPU code, how could I measure partial time inside kernel between 2 _syncthread() of 1 threadblock? Is it possible?
One approach is to use the clock() or clock64 function as described in the programming guide.
Search the cuda tag on clock64 for additional examples of its usage.
This question already has answers here:
Why is reading lines from stdin much slower in C++ than Python?
(10 answers)
Accessing individual characters in a file inefficient? (C++)
(3 answers)
Closed 9 years ago.
I've made some programs and saw that scanf and printf are considerably faster than using cin and cout? Most of my programs clear the execution time limit, mostly 3 seconds or 5 seconds, on online compilers when using scanf/printf which exceeded the limit while using cin/cout.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Which is the fastest algorithm to find prime numbers?
What is the fastest way to check if a number is prime( large numbers). I have tried the standard method i.e running a loop till root(n) or (n/2) and checking if anything divides it. Also i have tried the sieve method. Is there anything better to implement in c++?
http://en.wikipedia.org/wiki/Primality_test has all you need.
One tip is that you can ignore any even numbers (so add 2 at a time when either looking for factors or checking values).