Can I increase int i by more than one using the i++ syntax?
int fkt(int &i) { return i++; }
int main()
{
int i = 5;
printf("%d ", fkt(i));
printf("%d ", fkt(i));
printf("%d ", fkt(i));
}
prints '5 6 7 '. Say I want to print '5 7 9 ' like this, is it possible to do it in a similar way without a temporary variable in fkt()? (A temporary variable would marginally decrease efficiency, right?) I.e., something like
return i+=2
or
return i, i+=2;
which both first increases i and then return it, what is not what I need.
Thanks
EDIT: The main reason开发者_C百科, I do it in a function and not outside is because fkt will be a function pointer. The original function will do something else with i. I just feel that using {int temp = i; i+=2; return temp;} does not seem as nice as {return i++;}.
I don't care about printf, this is just for illustration of the use of the result.
EDIT2: Wow, that appears to be more of a chat here than a traditional board :) Thanks for all the answers. My fkt is actually this. Depending on some condition, I will define get_it as either get_it_1, get_it_2, or get_it_4:
unsigned int (*get_it)(char*&);
unsigned int get_it_1(char* &s)
{return *((unsigned char*) s++);}
unsigned int get_it_2(char* &s)
{unsigned int tmp = *((unsigned short int*) s); s += 2; return tmp;}
unsigned int get_it_4(char* &s)
{unsigned int tmp = *((unsigned int*) s); s += 4; return tmp;}
For get_it_1, it is so simple... I'll try to give more background in the future...
"A temporary variable would marginally decrease efficiency, right?"
Wrong.
Have you measured it? Please be aware that ++
only has magical efficiency powers on a PDP-11. On most other processors it's just the same as +=1
. Please measure the two to see what the actual differences actually are.
(A temporary variable would marginally decrease efficiency, right?)
If that's the main reason you're asking, then you're worrying far too early. Don't second-guess your compiler until you have an actual performance problem.
If you want fkt to be able to add different amounts to i, then you need to pass a parameter. There is no material reason to prefer ++
over +=
.
You could increment twice, then subtract, something like:
return (i += 2) - 2
Just answering the question, though, I don't think you should be scared of using a temporary variable.
If you really need that speed, then check the assembly code the compiler outputs. If you do:
int t = i;
i+=2;
return t;
then the compiler may optimize it while inlining the function into something like:
printf("%d ", i);
i+=2;
Which is as good as it's gonna get.
EDIT: Now you say you're jumping to this function via a function pointer? Calling a function by a pointer is pretty slow compared to using a temporary variable. I'm not certain, but I think I remember the Intel docs saying it's somewhere around 20-30 cpu cycles on Core 2s and i7s, that is if your function code is all in cache. Since the compiler cannot inline your function when it is called by a pointer, the ++ will also create a temporary variable anyways.
A temporary variable would marginally decrease efficiency, right?
Always take in account "premature optimization is the root of all evil". Spend time on other parts then try to optimize this away. += and ++ will probably result in the same thing. Your PC will manage ;)
Well, you could do
return i+=2, i-2;
That said, just follow this simple maxim:
Write code that is easy to understand.
Code that is easy for a human to understand is usually easy for the compiler to understand and hence the compiler can optimise it.
Writing crazy stuff in an effort to optimise your code often just confuses the compiler and makes it harder for the compiler to optimise your code.
I would recommend
int fkt(int &i)
{
int orig = i;
i += 2;
return orig;
}
A "temporary variable" will very unlikely decrease performance, as i++ has to hold the old state internally to (i.e. it implicitly uses what you call a temprorary variable). However, the instruction that is needed to increase by two may be slower than the one used for ++.
You can try:
int fkt(int &i)
{
++ii;
return i++;
}
You can compare this to:
int fkt(int &i)
{
int t = i;
i += 2;
return t;
}
That said, I don't think you should be doing any such performance considerations prematurely.
I think what you really want is a functor:
class Fkt
{
int num;
public:
Fkt(int i) :num(i-2) {}
operator()() { num+=2; return num; }
}
int main()
{
Fkt fkt(5);
printf("%d ", fkt());
printf("%d ", fkt());
printf("%d ", fkt());
}
What about
int a = 0;
++++a;
Increments +2 without temporary variables.
EDIT: Oops, sorry: didn't notice you want to achieve 5,7,9. This requires temporary variables so prefix notation can't be used.
精彩评论