473,657 Members | 2,612 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

++u or u++ which is faster?

Hi all,

Recently I was asked the question about whether ++U or U++ is
faster if U is a user defined type. What's the answer?

Thank you
Jul 22 '05 #1
22 1906
The Doctor wrote:
Hi all,

Recently I was asked the question about whether ++U or U++ is
faster if U is a user defined type. What's the answer?

Thank you

http://www.parashift.com/c++-faq-lit...html#faq-13.12

--
Sumit Rajan <sumitrajan AT alexandria DOT cc>
Jul 22 '05 #2
The Doctor posted:
Hi all,

Recently I was asked the question about whether ++U or U++ is
faster if U is a user defined type. What's the answer?

Thank you

++u is faster.
Always use ++u. Never use u++ unless you need to.
-JKop

Jul 22 '05 #3
Usually operator ++ (int) is implemented like this:

T operator ++ (int)
{
T tmp = *this;
++*this;
return tmp;
}

If compiler isn't able to optimize away the initialization of the temporary
then using u++ is slower. That's why it is usually better to use
pre-increment/pre-decrement instead of post-decrement/post-increment.

VH

"The Doctor" <do****@nospam. com> wrote in message
news:Xn******** *************** ***********@61. 9.191.5...
Hi all,

Recently I was asked the question about whether ++U or U++ is
faster if U is a user defined type. What's the answer?

Thank you

Jul 22 '05 #4

"The Doctor" <do****@nospam. com> wrote in message
news:Xn******** *************** ***********@61. 9.191.5...
Hi all,

Recently I was asked the question about whether ++U or U++ is
faster if U is a user defined type. What's the answer?

Thank you


The simple answer is when using U++, the object U needs to get copied twice
whereas with ++U, it need only be copied once.

So use ++U unless you specifically require the value of the post-increment.
Jul 22 '05 #5

"JKop" <NU**@NULL.NULL > schrieb im Newsbeitrag
news:I0******** ***********@new s.indigo.ie...
The Doctor posted:
Hi all,

Recently I was asked the question about whether ++U or U++ is
faster if U is a user defined type. What's the answer?

Thank you

++u is faster.


This general statement is not true. It depends on the datatype and the
optimization of the compiler. Both types might be the same speed, but as you
say it's generally wise to use ++u.


Always use ++u. Never use u++ unless you need to.
-JKop

Jul 22 '05 #6
JKop wrote:
++u is faster.

For built-in types their speed is the same. For user defined types it
applies what you say.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #7
Ioannis Vranos <iv*@guesswh.at .grad.com> wrote:
JKop wrote:
++u is faster.

For built-in types their speed is the same. For user defined types it
applies what you say.


So lets state:
++u is never slower.

Now it's perfect ;P
--
Simon Stienen <http://dangerouscat.ne t> <http://slashlife.de>
»What you do in this world is a matter of no consequence,
The question is, what can you make people believe that you have done.«
-- Sherlock Holmes in "A Study in Scarlet" by Sir Arthur Conan Doyle
Jul 22 '05 #8
Method Man wrote:

"The Doctor" <do****@nospam. com> wrote in message
news:Xn******** *************** ***********@61. 9.191.5...
Hi all,

Recently I was asked the question about whether ++U or U++ is
faster if U is a user defined type. What's the answer?

Thank you


The simple answer is when using U++, the object U needs to get copied
twice whereas with ++U, it need only be copied once.


What would that extra copy be needed for? I can see U++ doing at most 2
copies (but usually one - or if the function gets inlined and the return
value is not used even both - of them getting optimized away) and ++U doing
none at all. Consider the operators for an iterator into a linked list:

class mylistiterator
{
public:
//...

mylistiterator& operator++()
{
node = node->next;
return *this;
} // no copy made

mylistiterator operator++(int)
{
myiterator ret = *this; // first copy
node = node->next;
return ret; // second one, usually optimized away
}
private:
mylist::node* node;
};

So it stands 0 to 2 copies for postfix++ vs. 0 for prefix++.

Jul 22 '05 #9
> > The simple answer is when using U++, the object U needs to get copied
twice whereas with ++U, it need only be copied once.
What would that extra copy be needed for? I can see U++ doing at most 2
copies (but usually one - or if the function gets inlined and the return
value is not used even both - of them getting optimized away)


One time to store the old value, and another time for returning the old
value. Of course any optimizations are compiler-specific, dependent on
things like inlining, register variables, etc, or dependent on the
++operator code if U is a user type.

But I was speaking theoretically.
and ++U doing
none at all.
...


You're probably right here.

I guess the bottom line is U++ is never faster than ++U.
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

36
2622
by: Armin Rigo | last post by:
Hi! This is a rant against the optimization trend of the Python interpreter. Sorting a list of 100000 integers in random order takes: * 0.75 seconds in Python 2.1 * 0.51 seconds in Python 2.2 * 0.46 seconds in Python 2.3
23
4742
by: YinTat | last post by:
Hi, I learned C++ recently and I made a string class. A code example is this: class CString { public: inline CString(const char *rhs) { m_size = strlen(rhs);
98
14399
by: jrefactors | last post by:
I heard people saying prefix increment is faster than postfix incerement, but I don't know what's the difference. They both are i = i+1. i++ ++i Please advise. thanks!!
65
12567
by: Skybuck Flying | last post by:
Hi, I needed a method to determine if a point was on a line segment in 2D. So I googled for some help and so far I have evaluated two methods. The first method was only a formula, the second method was a piece of C code which turned out to be incorrect and incomplete but by modifieing it would still be usuable. The first method was this piece of text:
1
2721
by: James dean | last post by:
I done a test and i really do not know the reason why a jagged array who has the same number of elements as a multidimensional array is faster here is my test. I assign a value and do a small calculation. Even if i initialise the jagged array inside the function it is still much faster. Are these results correct?. If i put the initialisation loop in the constructor its ridiculously faster but even here its 4 times faster...is this correct?...
9
5156
by: VenuGopal | last post by:
Hi, why n++ executes faster than n+1..... or does it realli execute faster? thanks Venugopal.B
11
2982
by: ctman770 | last post by:
Hi Everyone, Is it faster to save the precise location of an html dom node into a variable in js, or to use getElementById everytime you need to access the node? I want to make my application as fast as possible. I have about 10-20 id tags that need to be accessed and modified from time to time. Would the jvm perform slowly if I stored all of the dom node strings "document.node.child...." into a huge js array?
12
9389
by: karthikbalaguru | last post by:
Hi, How is 'Int' Faster than 'Char' ? I think , 'Char' is small and so it should be easily & efficiently . Can someone here provide some info regarding this. Thanks and Regards, Karthik Balaguru
23
2511
by: Python Maniac | last post by:
I am new to Python however I would like some feedback from those who know more about Python than I do at this time. def scrambleLine(line): s = '' for c in line: s += chr(ord(c) | 0x80) return s def descrambleLine(line):
41
2671
by: c | last post by:
Hi every one, Me and my Cousin were talking about C and C#, I love C and he loves C#..and were talking C is ...blah blah...C# is Blah Blah ...etc and then we decided to write a program that will calculate the factorial of 10, 10 millions time and print the reusult in a file with the name log.txt.. I wrote something like this
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8823
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8730
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1607
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.