473,666 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Educated guesses for efficiency?



Commonly, people may ask a question along the lines of, "Which code
snippet is more efficient?".

If the code is anything other than assembler (e.g. C or C++), then
there's no precise answer because we don't know the instruction set of
the target system, or how the compiler will "map" each executable line of
C code to assembly instructions.

I consider myself to be fairly proficient in C, but I'll admit I know
very little about machine code, instructions set, and the like...

When writing fully-portable code, are there any guidelines as to how to
write your code in order to make an "educated guess" as to what would be
more efficient on *most* target platforms?

For instance, I heard that on the original machine for which C was
intended, that there was a single CPU instruction which could perform the
following:

*p++ = something; /* (p is a pointer variable) */

Therefore, at the time, it would have made sense to make use of "*p++" as
much as possible in the code.

So, my question is: Is there any sort of guide available on the web which
discusses constructs which should be used in fully-portable code because
they're likely to be quite efficient across the board?
--

Frederick Gotham
Jun 17 '06 #1
19 1522
Frederick Gotham a écrit :
Commonly, people may ask a question along the lines of, "Which code
snippet is more efficient?".

If the code is anything other than assembler (e.g. C or C++), then
there's no precise answer because we don't know the instruction set of
the target system, or how the compiler will "map" each executable line of
C code to assembly instructions.

I consider myself to be fairly proficient in C, but I'll admit I know
very little about machine code, instructions set, and the like...

When writing fully-portable code, are there any guidelines as to how to
write your code in order to make an "educated guess" as to what would be
more efficient on *most* target platforms?

For instance, I heard that on the original machine for which C was
intended, that there was a single CPU instruction which could perform the
following:

*p++ = something; /* (p is a pointer variable) */

Therefore, at the time, it would have made sense to make use of "*p++" as
much as possible in the code.

So, my question is: Is there any sort of guide available on the web which
discusses constructs which should be used in fully-portable code because
they're likely to be quite efficient across the board?


There is no one code snippet more efficient than another, if they
are strictly equivalent. Today's optimizing compilers are quite good
at generating very tight and fast assembly code.

In modern workstations, most of the traditional "optimizati ons" like

"a*2 is more expensive than a << 1"

are no longer valid. Much more important than the instructions
themselves is the data layout.

Data layout is today where the efficiency can be improved. The memory is
running at more or less 400 MHZ, the processor is running at more than
2-3GHZ. By improving the locality of data accesses you can gain a lot
in efficiency by avoiding expensive main memory reads that can cost you
like 10 or more cheap instructions of one cycle.

Data layout is a hot research subject, and I would recommend you reading
the manuals of the CPU vendor about this.

For the AMD/Intel world, both have produced optimizations manuals that
are a very instructive read.

jacob
Jun 17 '06 #2
Frederick Gotham (in Xn************* **************@ 194.125.133.14)
said:

| So, my question is: Is there any sort of guide available on the web
| which discusses constructs which should be used in fully-portable
| code because they're likely to be quite efficient across the board?

If there is, I'd be inclined to not trust it. It's really the compiler
writers' job to turn any and all valid source code into efficient
intermediate and/or executable codes. This allows the programmer to
focus on producing _valid_ source code solutions.

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto
Jun 17 '06 #3
Frederick Gotham wrote:

For instance, I heard that on the original machine for which C was
intended, that there was a single CPU instruction which could perform the
following:

*p++ = something; /* (p is a pointer variable) */

Therefore, at the time, it would have made sense to make use of "*p++" as
much as possible in the code.

Modern compilers are very good at optimizing array operations, so you
shouldn't go out of your way to use pointers. If you do use pointers,
any good compiler should be able to turn

*p = something;
++p;
into as efficient code, when "something" doesn't involve conflicting
operations. So the old advice to make your source code as clear as
possible to humans who need to understand it is better than ever. The
most likely gain in the ++ operator is saving in typing and typos.

A certain architecture which has had it fanatics is better suited to

*++p = something;

and the difference in performance could be detected.
Jun 17 '06 #4
Frederick Gotham wrote:
Commonly, people may ask a question along the lines of, "Which code
snippet is more efficient?".

If the code is anything other than assembler (e.g. C or C++), then
there's no precise answer because we don't know the instruction set of
the target system, or how the compiler will "map" each executable line of
C code to assembly instructions.

I consider myself to be fairly proficient in C, but I'll admit I know
very little about machine code, instructions set, and the like...

When writing fully-portable code, are there any guidelines as to how to
write your code in order to make an "educated guess" as to what would be
more efficient on *most* target platforms?

For instance, I heard that on the original machine for which C was
intended, that there was a single CPU instruction which could perform the
following:

*p++ = something; /* (p is a pointer variable) */


Some guy named Ritchie writes, in reference to the development
of C's immediate ancestor B:

Thompson went a step further by inventing the ++ and --
operators, which increment or decrement; their prefix or
postfix position determines whether the alteration occurs
before or after noting the value of the operand. [...]
People often guess that they were created to use the
auto-increment and auto-decrement address modes provided
by the DEC PDP-11 on which C and Unix first became popular.
This is historically impossible, since there was no PDP-11
when B was developed.

As for the PDP-11's addressing modes, there were indeed some
instances where the compiler could translate *p++ or *--p to a
single instruction. But it certainly could not do so in all
circumstances! The hardware's increment or decrement was by
either one or two bytes (for byte- or word-using instructions),
so other operand sizes needed additional instructions to adjust
the pointer value.

As for the larger question, there's a rather bitter thread
raging at this very moment over the question of whether micro-
optimizations of this sort are necessary nowadays, or even whether
they're truly optimizations at all. The doubters seem to be in
the majority, but can't be said to be winning -- nobody "wins" in
silly debates like that one.

Concerning the even larger question, I'd recommend reading

http://www.codeproject.com/tips/optimizationenemy.asp

for some good sense about optimization. The principal message
is simple: "Measure, measure, measure!" The corollary is also
simple: Since you can't know anything useful about performance
until you've measured the running code, micro-optimizations in
the initial development are silly.

--
Eric Sosman
es*****@acm-dot-org.invalid

Jun 17 '06 #5
Frederick Gotham said:


Commonly, people may ask a question along the lines of, "Which code
snippet is more efficient?".
The one that costs the maintainer the least time to fix.

<snip>
When writing fully-portable code, are there any guidelines as to how to
write your code in order to make an "educated guess" as to what would be
more efficient on *most* target platforms?
Sure.

1) Use good algorithms.
2) Write clear code.
3) All code should either do something good or stop something bad happening.
4) If, as you write a section of code, a smile comes over your face and you
think "hey, this is way cool", it probably needs re-writing.
For instance, I heard that on the original machine for which C was
intended, that there was a single CPU instruction which could perform the
following:


Who cares about a single instruction? If you're interested in efficiency, go
for macro-efficiencies. Maybe x = -x is a nanosecond quicker than x *= -1,
and maybe it isn't, but we know for sure that a binary search is vastly
quicker than a linear search in almost all circumstances.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 17 '06 #6
Richard Heathfield a écrit :
4) If, as you write a section of code, a smile comes over your face and you
think "hey, this is way cool", it probably needs re-writing.


I do not agree with this at all.

Sometimes I write a program I am proud of, or at least I like the way
I wrote it.

It is short, powerful, there is nothing too much, nothing missing.

Programming is fun.

Yes, you can see the world as a sea of tears.

Or you can see the world as a place where you can find joy and
satisfaction with what you have done.

jacob
Jun 17 '06 #7
Richard Heathfield schrieb:
Frederick Gotham said:
When writing fully-portable code, are there any guidelines as to how to
write your code in order to make an "educated guess" as to what would be
more efficient on *most* target platforms?
Sure.

1) Use good algorithms.


Yes.
2) Write clear code.
Yes.
3) All code should either do something good or stop something bad happening.
Depends on your definition of good and bad.
4) If, as you write a section of code, a smile comes over your face and you
think "hey, this is way cool", it probably needs re-writing.


No and yes.

I usually smile when the roles identified during specification
and design really work as intended, everything comes down to
exactly the right level of granularity and clarity, and I just
get the feeling that it will be joy to revisit the code
throughout the years.
Or if I found a clever way to break complexity without
endangering clarity and conciseness.
And I find this "way cool".

On the other hand, nifty tricks essentially coming down to
micro-optimization can give this feeling, too -- for them, I
agree with you for most applications. However, the threshold
is very different for different projects, people, departments,
etc.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Jun 17 '06 #8
Michael Mair said:
Richard Heathfield schrieb:
4) If, as you write a section of code, a smile comes over your face and
you think "hey, this is way cool", it probably needs re-writing.
No and yes.

I usually smile when the roles identified during specification
and design really work as intended, everything comes down to
exactly the right level of granularity and clarity, and I just
get the feeling that it will be joy to revisit the code
throughout the years.
Or if I found a clever way to break complexity without
endangering clarity and conciseness.
And I find this "way cool".


So do I, and that's why I included the weasel word "probably". I'm afraid
that you and I are in a minority.

On the other hand, nifty tricks essentially coming down to
micro-optimization can give this feeling, too -- for them, I
agree with you for most applications.
That's the kind of thing I was talking about, yes.

However, the threshold is very different for different projects, people,
departments, etc.


Sure. If your target machine has 256 octets of storage and you find a
micro-optimisation that reduces your object code size from 257 octets to
254, then you have every right to feel pleased.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 17 '06 #9
jacob navia <ja***@jacob.re mcomp.fr> writes:
Richard Heathfield a écrit :
4) If, as you write a section of code, a smile comes over your face
and you think "hey, this is way cool", it probably needs re-writing.


I do not agree with this at all.

Sometimes I write a program I am proud of, or at least I like the way
I wrote it.

It is short, powerful, there is nothing too much, nothing missing.

Programming is fun.

Yes, you can see the world as a sea of tears.

Or you can see the world as a place where you can find joy and
satisfaction with what you have done.


You and Richard are talking about two very different forms of "hey,
this is way cool". If the reaction comes from having written
well-constructed and *clear* code, then it really is way cool. If it
comes from having written code that so incredibly clever that nobody
else will ever understand it, file it away for possible submission to
the IOCCC, or delete it so it will never see the light of day.

On this point, I suspect the three of us are in complete agreement.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 17 '06 #10

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

Similar topics

2
2310
by: Sara | last post by:
Hi - I've been reading the posts for a solution to my query, and realize that I should ask an "approch" question as well. We receive our production data from a third party, so my uers import the data from Excel into the appropriate tables. There are 6 different databases that receive data, though 4 of them only get one table each. I have learned how to automate the data import through
31
2627
by: mark | last post by:
Hello- i am trying to make the function addbitwise more efficient. the code below takes an array of binary numbers (of size 5) and performs bitwise addition. it looks ugly and it is not elegant but it appears to work. using time, i measured it takes .041s to execute, which i admit isnt much. but, the problem is that this procedure will be called many, many times in my project (probably at least a few thousand times, if not more) so...
92
4041
by: Dave Rudolf | last post by:
Hi all, Normally, I would trust that the ANSI libraries are written to be as efficient as possible, but I have an application in which the majority of the run time is calling the acos(...) method. So now I start to wonder how that method, and others in the math.h library, are implemented. Dave
9
4610
by: Peng Jian | last post by:
I have a function that is called very very often. Can I improve its efficiency by declaring its local variables to be static?
1
2275
by: Tomás | last post by:
dynamic_cast can be used to obtain a pointer or to obtain a reference. If the pointer form fails, then you're left with a null pointer. If the reference form fails, then an exception is thrown. Would "Feed1" or "Feed2" be preferable in the following: #include <iostream>
335
11654
by: extrudedaluminiu | last post by:
Hi, Is there any group in the manner of the C++ Boost group that works on the evolution of the C language? Or is there any group that performs an equivalent function? Thanks, -vs
19
2922
by: vamshi | last post by:
Hi all, This is a question about the efficiency of the code. a :- int i; for( i = 0; i < 20; i++ ) printf("%d",i); b:- int i = 10;
9
3308
by: OldBirdman | last post by:
Efficiency I've never stumbled on any discussion of efficiency of various methods of coding, although I have found posts on various forums where individuals were concerned with efficiency. I'm not concerned when dealing with user typing, but I am if a procedure is called by a query. Does the VBA compiler generate "in-line" code for some apparent function calls? For example, y = Abs(x) might be compiled as y = x & mask. The string...
5
2952
by: want.to.be.professer | last post by:
For OO design, I like using virtual member function.But considering efficiency, template is better. Look at this program, class Animal { public: virtual void Walk() = 0; }; class Dog
0
8448
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8783
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
8552
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
8640
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...
0
7387
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4198
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...
1
2773
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
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1776
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.