A good part of this review will not be about optimization but about style. The speed of your program should not be worse after you apply the changes though. First of all, you should order your headers by alphabetic order: this will allow you to check whether a header is inluded or not without having to search for it longer than needed:
using namespace std; is bad practice especially in header files: it will pollute the namespace and may cause name clashes. Moreover, you don't even need it except for
std:: is no longer and it's also good practice to put it before any standard function, including C legacy functions (
In C++, you don't need to write
return 0; at the end of your
main function. If the compiler reaches the end of
main and don't find any
return statement, it will automagically add
Your function names are not really good:
* As noted in the comments, you shouldn't mix languages in code. C++ is an English language, therefore, you should rename
for_odd do not tell what the functions actually do but merely say that you should give even or odd numbers to them. You should give more meaningful names.
You should really rename
func2. The names don't give any clue about what these functions do, and the name of the parameters do not help at all. Also, you should be consistent when naming your parameters:
int x while
int n, which is not consistent.
You do not use
func1 at all. You should remove it since it does not seem to add anything (and it's faster to write
x-1 whatsoever, which I assume is what you actually did in
In your main, you use
std::cin for input for
std::printf for output. You should be consistent and use
std::cout which is the idiomatic C++ way to output values to the console.
You can refactor this loop:
int k = 0;
for (int i = pow(n, 2) / 4; i >= n - 1;)
sum2 += 2 * func2(i);
i -= (k + 2);
k = k + 2;
First of all, you can put the
int k = 0 in the first part of the
for loop since it won't be needed after the loop. Then, you increment
k before decrementing
i, that will save you from repeating
k + 2. Also, you can replace
k = k + 2 by
k += 2 (use compound assignement operators when you can). Finally, you can put the
k incrementation and
i decrementation in the last part of the
for (int i = pow(n, 2) / 4, k = 0;
i >= n - 1;
k += 2, i -= k)
sum2 += 2 * func2(i);
That say, I don't think it looks really nice either.
Some more tidbits:
std::pow(n, 2) is good, it shouldn't be different from
n*n on any decent compiler, and is somehow more expressive.
Luwistvis, you compute
pow(n, 2) / 4 at every iteration of your
for loop. While some compilers may perform loop optimizations, you should define a constant instead.
- It does not make any difference for integers, but for other types, pre-increment (
++i) may be faster than post-increment (
i++). Therefore, you should always use pre-increment, unless you explicitly need post-increment.
Overall, someone else will probably be able to give you a way to optimize the algorithm instead of optimizing the code. Choosing the good algorithm is the best way to achieve the best performance.