hi,
do you know where i can find some ebooks or websites talking about C#
optimisation ?
for exemple, i just learned that ++i is faster than i++. i would like to
know more about the things that can make code faster than fast.
thank you.
Nov 17 '05
55 3735
James Curran wrote: but since the dawn of time C-programmers have preferred the (1 character shorter) "i++" to "i+=1". There have *never* *ever* been *any* performance-reason for this.
No, actually there was. Remember that the original C compiler was written for a very small machine -- one which measured memory in KB, not MB (and probably measure it with only 2 digits or fewer)
Can you point to a C-compiler, even a *really* old one, that does not
optimize "x = x + 1" to some available ASM instruction (if one exists on
that architecture) that increments x?
Nothing about it the language is superflious. If the syntax was different, the code produced would be different. The best way to see this is to consider what you can't do with a particular syntax: Since the compiler was very stupid with no optimizations, ANY statement written in that form would be translated the same way.
I have never seen any compiler do that. Do you have an example?
[...more examples cutted...]
I have implemented compilers, i know how code generation works. If a
compiler have *any* optimizations at all I would hazard a guess that "x
= x + c" is optimized.
--
Helge Jensen
mailto:he****** ****@slog.dk
sip:he********* *@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Helge Jensen wrote: No, actually there was. Remember that the original C compiler was written for a very small machine -- one which measured memory in KB, not MB (and probably measure it with only 2 digits or fewer)
After a bit of googling, i found: http://cm.bell-labs.com/cm/cs/who/dmr/primevalC.html, which lists *the*
first C-compiler created at bell-labs.
Funny to see that old code.
Reading: http://cm.bell-labs.com/cm/cs/who/dm...1120c/efftab.s, it
seems to me that even this compiler have support for:
i += C;
optimizations, although I must admit PDP10 assembler is not really my
strong point :)
--
Helge Jensen
mailto:he****** ****@slog.dk
sip:he********* *@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Ennixo <en****@free.fr > wrote: I can't imagine that there is *any* difference whatsoever for the performance using ++i or i++ (esp. on integral types), and will continue to think that untill I see a test-program proving otherwise.
so try this sample code (paste in Main method) and you will think that ++i is really faster than other ways to increment by 1. (note: run in RELEASE mode)
No I won't, because I can run ildasm and see that the generated code
for both loops is the same.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Ennixo <en****@free.fr > wrote: Where did you read that? There shouldn't be any difference from the generated code performance.
i read it in a source code from csharpfr.com and i saw it in some sources of codeproject.com
Unfortunately that's the kind of myth which often spreads :(
It's always worth looking a bit deeper than an initial benchmark - try
moving the code around and see what happens, try looking at the
generated IL etc.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
"James Curran" <ja*********@mv ps.org> wrote in message
news:eE******** ******@TK2MSFTN GP14.phx.gbl... Everyone seems to be missing the important point about ++i vs i++ optimization...
Namely it comes in play mostly on user defined types with an overload operator ++.
You're just not going to see the difference using an int.
To understand this, imagine we have a class (MyClass), which has a member function (AddOne), and we want to implement op++ and ++op. (For the moment, I'm going to do this in C++)
class MyClass { public: void AddOne() {.....}
// The Pre-fix (++x) operator is rather straight forward. MyClass& operator++() { AddOne(); return *this; }
// the post-fix (x++) is a bit trickier MyClass operator++(int) { MyClass temp(*this); AddOne(); return temp; } }
Note that for the post fix, we've got to make two copies of MyClass (one in temp, and one for the return), which aren't needed in the pre-fix op++. Also, note that I could have written the post-fix as:
MyClass operator++(int) { MyClass temp(*this); this->operator++() ; // or ++(*this); return temp; }
Now, let's write a similar class in C#:
class MyClass { public void AddOne() {.....}
public static MyClass operator++(MyCl ass a) { a.AddOne(); return a; } }
You'll note that this is the pre-fix operator++. So, where is the post-fix? The C# compiler creates it for you automatically. And is it written? Basically:
public static MyClass operator++(MyCl ass a) // post-fix { MyClass b = a.Clone(); ++a; return b; }
So, like in the C++, the post-fix has to create a copy of the unaltered object, so that it can be returned.
When an int is used, the operator method is inlined, so, when it's just "x++;" on a line by itself, the compile can see that the duped value isn't used, and it can remove the whole creation of it. When ++ is used on some other type object, the oper++ is done out-of-line, and so, the compiler cannot remove it.
So, now, let's try your test again, but this using a class: using System; public class LikeAnInt { int n =0; public LikeAnInt(int i) { n = i; } public void AddOne() { ++n; } static public LikeAnInt operator++(Like AnInt a) { a.AddOne(); return a; } public int Val { get { return n; } } }
public class MyClass { public static void Main() { DateTime d; TimeSpan t; int iMax = 1000000000; int j; // Test 1 : i++ j = 0; d = DateTime.Now; for (LikeAnInt i = new LikeAnInt(0); i.Val < iMax; i++) j += i.Val; t = DateTime.Now - d; Console.WriteLi ne("i++ : {0}", t.TotalMillisec onds.ToString() );
// Test 2 : ++i j = 0; d = DateTime.Now; for (LikeAnInt i = new LikeAnInt(0); i.Val < iMax; ++i) j += i.Val; t = DateTime.Now - d; Console.WriteLi ne("++i : {0}", t.TotalMillisec onds.ToString() ); // Test 3 : i.AddOne() j = 0; d = DateTime.Now; for (LikeAnInt i = new LikeAnInt(0); i.Val < iMax; i.AddOne()) j += i.Val; t = DateTime.Now - d; Console.WriteLi ne("i.AddOne : {0}", t.TotalMillisec onds.ToString() ); Console.Read(); } }
The Results: SnippetCompiler : i++ : 44206.6704 ++i : 42848.6265 Approx 3% faster
VC#-Debug: i++ : 34887.6795 ++i : 34731.5825 Approx 0.3% faster
VC#-Release: i++ : 1623.4088 ++i : 1607.7991 i.AddOne : 1607.7991 Approx 0.9% faster
A minmal difference, but it's beyond the "noise" level. -- -- Truth, James Curran [erstwhile VC++ MVP]
Please stop this nonsense about post/prefix increments.
Looak at the IL and more important look at the machine code generated by the
JIT.
Here is the what the JIT produced in optimized builds:
Post-increment loop
02c70086 b978519700 mov ecx,0x975178
02c7008b e8881f78fd call 003f2018
02c70090 895804 mov [eax+0x4],ebx
02c70093 8bf0 mov esi,eax
02c70095 eb0f jmp 02c700a6
02c70097 8b4604 mov eax,[esi+0x4]
02c7009a 8bd0 mov edx,eax
02c7009c c1fa1f sar edx,0x1f
02c7009f 03d8 add ebx,eax
02c700a1 13fa adc edi,edx
02c700a3 ff4604 inc dword ptr [esi+0x4]
02c700a6 817e0400ca9a3b cmp dword ptr [esi+0x4],0x3b9aca00
pre-increment loop
02c70086 b978519700 mov ecx,0x975178
02c7008b e8881f78fd call 003f2018
02c70090 895804 mov [eax+0x4],ebx
02c70093 8bf0 mov esi,eax
02c70095 eb0f jmp 02c700a6
02c70097 8b4604 mov eax,[esi+0x4]
02c7009a 8bd0 mov edx,eax
02c7009c c1fa1f sar edx,0x1f
02c7009f 03d8 add ebx,eax
02c700a1 13fa adc edi,edx
02c700a3 ff4604 inc dword ptr [esi+0x4]
02c700a6 817e0400ca9a3b cmp dword ptr [esi+0x4],0x3b9aca00
See the same routine gets executed for both loops.
Willy.
it's ok, thank you for this enlightment =)
Jon Skeet [C# MVP] a écrit : Ennixo <en****@free.fr > wrote:
Where did you read that? There shouldn't be any difference from the generated code performance.
i read it in a source code from csharpfr.com and i saw it in some sources of codeproject.com
Unfortunately that's the kind of myth which often spreads :(
It's always worth looking a bit deeper than an initial benchmark - try moving the code around and see what happens, try looking at the generated IL etc.
Since the dawn of time, C programmers use i++ only when pre-incremented
value of i is needed. Otherwise they use ++i. This has been my experience
for the past 20 years working in C and C++. There is no point in saving the
pre-incremented value if you don't need it.
In the past the pre-increment and post-increment versions did have a
significant difference in performance even in C, if you want to go back to
the dawn of time. This is well known among older C developers. But they are
semantically different as well, so you may as well use the correct operator
in the correct spot, and lessen confusion.
The pre-increment and post-increment versions were invented for good
reasons.
- Frank
"Helge Jensen" <he**********@s log.dk> wrote in message
news:%2******** ********@TK2MSF TNGP14.phx.gbl. .. but since the dawn of time C-programmers have preferred the (1 character shorter) "i++" to "i+=1". There have *never* *ever* been *any* performance-reason for this.
I C++ you have overloadable operator++, and this means that you *can* construct classes where the left++ is cheaper than the right++, therefore you see C++ programmers do:
for ( IT it = begin; it != end; ++it )
which *may* yield better performance in *rare* cases (the optimizer solves most simple cases).
> The pre-increment and post-increment versions were invented for good
reasons.
Umm... because the PDP-11 happened to have those two modes? :)
Frank Hileman <fr******@no.sp amming.prodiges oftware.com> wrote: Since the dawn of time, C programmers use i++ only when pre-incremented value of i is needed. Otherwise they use ++i. This has been my experience for the past 20 years working in C and C++. There is no point in saving the pre-incremented value if you don't need it.
Whereas in for loops, I've almost always seen it as i++, just because
it shows you what you're dealing with before what you're going to do
with it, even though you don't care about the pre-incremented value.
I've also almost always seen it as i++ on lines on its own - ++i is
very rare in the code I've seen...
In the past the pre-increment and post-increment versions did have a significant difference in performance even in C, if you want to go back to the dawn of time. This is well known among older C developers. But they are semantically different as well, so you may as well use the correct operator in the correct spot, and lessen confusion.
I very rarely use either in the case where it makes any difference - it
takes longer reading the code that way than separating the lines,
usually.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
++i or i++ can go in any expression, not just a for loop, and they mean
something completely different. So there is a good reason for having both
versions. At the end of the for clause, either one works, but it looks
strange to use i++ when you don't need it. It does not help a novice
understand the operator either.
- Frank
"Helge Jensen" <he**********@s log.dk> wrote in message
news:%2******** ********@TK2MSF TNGP14.phx.gbl. .. No, "++i" and "i++" are *both* expressions. "i = i + 1" and "i += 1", are statements. Using "++i" as a statement is equivalent to "i = i + 1", or "i += 1".
Remember that the syntax of "for" is
'for' '(' STMT ';' EXPR ';' STMT ')' BLOCK_OR_STMT
not:
'for' '(' STMT ';' EXPR ';' EXPR ')' BLOCK_OR_STMT
So we should really be writing:
for ( int i = 0; i < limit; i += 1 )
otherwise you are discard the value of an expression.
but since the dawn of time C-programmers have preferred the (1 character shorter) "i++" to "i+=1". There have *never* *ever* been *any* performance-reason for this.
I C++ you have overloadable operator++, and this means that you *can* construct classes where the left++ is cheaper than the right++, therefore you see C++ programmers do:
for ( IT it = begin; it != end; ++it )
which *may* yield better performance in *rare* cases (the optimizer solves most simple cases).
using i++ where you don't need it, you are making code less readable. It is good practice to use ++ correctly.
Which is not really much except in indexing in loops:
public void CopyTo(Array a, int index) { foreach ( object o in this ) a.SetValue(o, index++); }
that i prefer to:
public void CopyTo(Array a, int index) { foreach ( object o in this ) { a.SetValue(o, index); index += 1; } }
-- Helge Jensen mailto:he****** ****@slog.dk sip:he********* *@slog.dk -=> Sebastian cover-music: http://ungdomshus.nu <=- This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: OPQ |
last post by:
Hi all,
I'd happy to have you share some thougts about ultimate optimisations
on those 2 topics:
(1)- adding one caractere at the end of a string (may be long)
(2)- in a dict mapping a key to a list of int, remove every entrie
where the list of int have of length < 2
|
by: Simon Elliott |
last post by:
What optimisation do compilers typically provide when passing STL
containers around?
For example, if I do something like this:
struct Tbloggs
{
std::string s1;
};
typedef std::vector<Tbloggs> TbloggsList;
|
by: simonwittber |
last post by:
Hello People.
I've have a very tight inner loop (in a game app, so every millisecond
counts) which I have optimised below:
def loop(self):
self_pool = self.pool
self_call_exit_funcs = self.call_exit_funcs
self_pool_popleft = self.pool.popleft
self_pool_append = self.pool.append
|
by: EC-AKD |
last post by:
Hi All,
I am new to the concept of optimising codes in C. I was wondering if
C level inlining of code is any way comparable to macros. I believe
that inlining is equivalent to writing macros. However I had this
strange feeling if I should go for macros wherever necessary instead
of inlining the codes.
In my project, I have to use basic operations like add and sub many
times. So I initially inlined them and found a lot of optimisation.
|
by: Jon Maz |
last post by:
Hi,
I'm facing a code-optimisation issue on an asp.net/vb.net/SQL Server 2000
project. A web page containing not much more than 3 DropDownLists is taking
nigh on 6 seconds to load, because each ddl opens up a separate connection
to the DB, pulls out its data, and closes its own connection before the next
ddl repeats the process.
The code to handle each DDL's connection to the DB is packaged in an object
(presentation-layer code...
| |
by: Lee Harr |
last post by:
I have a database where I remove the schema public. When I
try to use the createlang script, it fails like this ...
>createdb foo
CREATE DATABASE
>psql foo -c "select version()"
version
---------------------------------------------------------------------
PostgreSQL 7.4.1 on i386-portbld-freebsd4.9, compiled by GCC 2.95.4
(1 row)
|
by: David Welch |
last post by:
Hi,
I have a bit of code where I am relying on empty base member
optimisation. The bit of code is below:
template<typename Enum>
struct EncodePrefix
{
template<Enum e>
struct Apply
|
by: grid |
last post by:
Hi,
I was exploring the affect of cache on program
performance/optimisation.Is it the compilers responsibility only to
consider this kind of optimisation or the programmer can do his bit in
this case ?
Reading through the "Expert C Programming" text,it mentions how the
below program can be efficient taking the cache details into accont.
The below program can be executed using the two versions of copy
alternatively and running the time...
|
by: special_dragonfly |
last post by:
Hello,
I know this might be a little cheeky, and if it is, please say, but I need a
little hand optimising some code. For the simple reason that this is
'company' code and I have no idea what I'm allowed to release and not as the
case may be I've changed anything that could give an indication of the
company - if that makes any sense...
for the code below:
text_buffer is a single record from an XML stream. I can't read in the
entire XML...
|
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,...
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |