开发者

Which one is faster post increment or pre-increment? [duplicate]

This question already has answers here: 开发者_开发技巧 Closed 11 years ago.

Possible Duplicate:

Is there a performance difference between i++ and ++i in C++?

Hi, I was asked someday ago that which one is faster variable++ or ++variable? I was little confuse. Can someone tell me which one is faster and why??


I think pre-increment would be faster, since it just increments it then and there and the deed is done, whilst post-incrementing requires keeping a copy of said variable a little longer.

This probably depends on the compiler, but I generally use pre-increment unless needed otherwise.


In C++ the answer I've personally been given is something along the lines of:

Post increment must create a copy of the objects current state, increment the object, and return the copy by value. For integer types this is irrelevant as by-value returns are the same size and the copy means little.

Pre-increments may simply increment and return itself by reference.


There are few answers out there:

http://www.geekinterview.com/question_details/14424
http://www.digitalpeer.com/id/where

but this question is a duplicate

Preincrement faster than postincrement in C++ - true? If yes, why is it?

---andrew

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜