开发者

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

开发者 https://www.devze.com 2023-02-24 05:35 出处:网络
This question already has answers here: 开发者_开发技巧 Closed 11 years ago. Possible Duplicate: Is there a performance difference between i++ and ++i in C++?
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

精彩评论

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