473,396 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

style and performance

Hi

while I am reading this C++ book, I noticed the iterator is being
used to loop through a container, say a vector. I am used to use
"for(int=0;i<vec.size();++i)" which is better to use?

also I noticed the author references a class variable by using
class-identifier.variable-identifier instead of get_variable(); again
which is better?

I am just after improving the performance of my code.
thanks
Aug 5 '06 #1
8 1524
* Gary Wessle:
>
while I am reading this C++ book, I noticed the iterator is being
used to loop through a container, say a vector. I am used to use
"for(int=0;i<vec.size();++i)" which is better to use?

also I noticed the author references a class variable by using
class-identifier.variable-identifier instead of get_variable(); again
which is better?

I am just after improving the performance of my code.
Regarded as style issues your questions are meaningful, but the answers
depend on the context. E.g., using indexing of a vector can be more
clear but can restrict the code to indexable data representations.
Likewise, direct access of object data members can be more clear and
reduce the code (nearly always good), especially for simple cases such
as a 2D point, but can also restrict -- e.g. representation, naming,
the possibility of working on a pure interface, and more.

Regarded as questions about performance the questions are not very
meaningful.

For performance: use efficient algorithms, use common sense, then if
that isn't enough measure, measure, measure.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Aug 5 '06 #2
Gary Wessle wrote:
while I am reading this C++ book, I noticed the iterator is being
used to loop through a container, say a vector. I am used to use
"for(int=0;i<vec.size();++i)" which is better to use?
Use an iterator.

Old-fashioned code used an index because the iterator concept was not
invented yet. An iterator more closely matches the code's concept.
also I noticed the author references a class variable by using
class-identifier.variable-identifier instead of get_variable(); again
which is better?
Generally, always make primitive things private. That includes a classes
data members. Ideally, you should tell a class object What to do, but not
How to do it. The getVariable() idiom is sometimes required, but it
indicates you are asking an object for its data so you can do its actions
for it. You should instead tell the object what to do, and not consider how
it uses its variables to do it.
I am just after improving the performance of my code.
Look up "premature optimization is the root of all evil".

You are asking how to prematurely optimize. The most important resource to
optimize is programming time. You should write the clearest most obvious
code you can. It should use iterators and member functions, not indices or
raw data. Sometimes one is faster, sometimes the other is faster, but you
never know.

To optimize, finish your program, then see if it's slow. With modern CPUs,
you will need to program for a very long time before your code gets slow.
Only at that time should you optimize it. Otherwise you will most likely
waste time optimizing the wrong thing. And such optimizations invariably
make code harder to read. So first work to make the code easy to read and
change.

It's easier to make beautiful code fast than to make fast code beautiful.
And only beautiful code can keep development fast.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Aug 5 '06 #3
Gary Wessle wrote:
Hi

while I am reading this C++ book, I noticed the iterator is being
used to loop through a container, say a vector. I am used to use
"for(int=0;i<vec.size();++i)" which is better to use?

also I noticed the author references a class variable by using
class-identifier.variable-identifier instead of get_variable(); again
which is better?

I am just after improving the performance of my code.
thanks
Another option is to avoid looping over containers. Instead use an
algorithm such as std::for_each to apply some operation to a range of
iterators.

--
Alan Johnson
Aug 5 '06 #4
In article <87************@localhost.localdomain>,
Gary Wessle <ph****@yahoo.comwrote:
while I am reading this C++ book, I noticed the iterator is being
used to loop through a container, say a vector. I am used to use
"for(int=0;i<vec.size();++i)" which is better to use?
Performance wise, I doubt it makes a difference. Style wise, using
iterators is more versatile because they can be used for lists, and I
expect using iterators for deques would have better performance.
also I noticed the author references a class variable by using
class-identifier.variable-identifier instead of get_variable(); again
which is better?
Again, performance wise probably none. Style wise... IMHO,
'get_variable()' is marginally better. The reason I think it is better
is because it can easily be replaced by code that calculates the value
on the fly, or loads the value from disk, or requests the value from
some other object, or any of a number of different things, whereas using
a public member-variable limits you to holding the value in ram in that
object.
Aug 5 '06 #5
In article <BC****************@newssvr21.news.prodigy.com>,
"Phlip" <ph******@yahoo.comwrote:
Ideally, you should tell a class object What to do, but not How to do
it.
I can't agree with the above. Ideally, you should tell a class object
what its environment is like, you shouldn't be telling it what to do.
It's the object's job to decide what to do based on the information it
is given.

Let's start a war Phlip. :-)
Aug 5 '06 #6
Daniel T. wrote:
Let's start a war Phlip. :-)
Totally. Violent agreement is all the rage, these days, apparently...

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Aug 5 '06 #7

"Daniel T." <da******@earthlink.netskrev i meddelandet
news:da****************************@news.west.eart hlink.net...
In article <87************@localhost.localdomain>,
Gary Wessle <ph****@yahoo.comwrote:

>also I noticed the author references a class variable by using
class-identifier.variable-identifier instead of get_variable();
again
which is better?

Again, performance wise probably none. Style wise... IMHO,
'get_variable()' is marginally better. The reason I think it is
better
is because it can easily be replaced by code that calculates the
value
on the fly, or loads the value from disk, or requests the value from
some other object, or any of a number of different things, whereas
using
a public member-variable limits you to holding the value in ram in
that
object.
In which case get_variable() is a lie, if it isn't getting a variable.
:-)

Naming is important, and get_* and set_* almost always indicates a
problem with the abstraction. For example, a set_position() should
most likely be called something like move(). Having both set and get
promotes horrible code like

obj.set_x(obj.get_x() + 12);

:-(
Bo Persson
Aug 5 '06 #8
In article <4j************@individual.net>, "Bo Persson" <bo*@gmb.dk>
wrote:
"Daniel T." <da******@earthlink.netskrev i meddelandet
news:da****************************@news.west.eart hlink.net...
In article <87************@localhost.localdomain>,
Gary Wessle <ph****@yahoo.comwrote:

also I noticed the author references a class variable by using
class-identifier.variable-identifier instead of get_variable();
again
which is better?
Again, performance wise probably none. Style wise... IMHO,
'get_variable()' is marginally better. The reason I think it is
better
is because it can easily be replaced by code that calculates the
value
on the fly, or loads the value from disk, or requests the value from
some other object, or any of a number of different things, whereas
using
a public member-variable limits you to holding the value in ram in
that
object.

In which case get_variable() is a lie, if it isn't getting a variable.
:-)
'get_information()' would be better.
Naming is important, and get_* and set_* almost always indicates a
problem with the abstraction. For example, a set_position() should
most likely be called something like move(). Having both set and get
promotes horrible code like

obj.set_x(obj.get_x() + 12);
All (non-static) member-functions either return state information about
the object or change state information. In that sense, they are all
either getters or setters, and saying that they are "a problem with
abstraction" becomes a huge problem...
Aug 5 '06 #9

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

Similar topics

4
by: Robert Ferrell | last post by:
I have a style question. I have a class with a method, m1, which needs a helper function, hf. I can put hf inside m1, or I can make it another method of the class. The only place hf should ever...
15
by: Nick Coghlan | last post by:
Thought some folks here might find this one interesting. No great revelations, just a fairly sensible piece on writing readable code :) The whole article:...
7
by: Cameron Hatfield | last post by:
I was wondering how good this page of coding standards (http://www.doc.ic.ac.uk/lab/cplus/c++.rules/) is. Is it too outdated?
3
by: Scott Brady Drummonds | last post by:
Hello, all, My most recent assignment has me working on a medium- to large-sized Windows-based C++ software project. My background is entirely on UNIX systems, where it appears that most of my...
4
by: Steffen | last post by:
Hi, in program I have a frequently called function (~few times per second) that needs a comparatively big amount of local memory (~10MB) for doing its job. Is it faster to make that memory...
43
by: Sensei | last post by:
Hi! I'm thinking about a good programming style, pros and cons of some topics. Of course, this has nothing to do with indentation... Students are now java-dependent (too bad) and I need some...
6
by: mswlogo | last post by:
There are many threads on the lack of a true unmanaged C++ const like behavior in C# (.Net) and that's not what this topic is about. The topic is, what is the best practical way to live with it. ...
40
by: rayw | last post by:
I've been trying to write a program that converts an entered number into its binary representation using three different techniques - bit shifting, mod 2, and itoa.. However, I'm getting 'snow...
1
by: ehchn1 | last post by:
Hi, Just curious. Would you use ANSI style table joining or the 'old fashion' table joining; especially if performance is the main concern? What I meant is illustrated below: ANSI Style...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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,...

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.