473,746 Members | 2,262 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<ve c.size();++i)" which is better to use?

also I noticed the author references a class variable by using
class-identifier.vari able-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 1549
* 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<ve c.size();++i)" which is better to use?

also I noticed the author references a class variable by using
class-identifier.vari able-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<ve c.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.vari able-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<ve c.size();++i)" which is better to use?

also I noticed the author references a class variable by using
class-identifier.vari able-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.loca ldomain>,
Gary Wessle <ph****@yahoo.c omwrote:
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<ve c.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.vari able-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.co m>,
"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******@earth link.netskrev i meddelandet
news:da******** *************** *****@news.west .earthlink.net. ..
In article <87************ @localhost.loca ldomain>,
Gary Wessle <ph****@yahoo.c omwrote:

>also I noticed the author references a class variable by using
class-identifier.vari able-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.g et_x() + 12);

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

also I noticed the author references a class variable by using
class-identifier.vari able-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_informatio n()' 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.g et_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
8898
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 be invoked is from inside m1. Is either style preferred? Here are two short examples: Helper function as method:
15
2097
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: http://www.acmqueue.com/modules.php?name=Content&pa=showpage&pid=271&page=1 The section specifically on white space: http://www.acmqueue.com/modules.php?name=Content&pa=showpage&pid=271&page=3 Cheers,
7
1654
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
2133
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 peers were writing "better" C++ code. By "better" I mean that it more regularly looked like C++ (use of objects, streams, exceptions, and ANSI-compliance) whereas some of my recent Windows-based C++ peers rely on un-portable, operating-system...
4
1361
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 static so it has not to be reallocated all the time? Or is overall performance better if that memory is freed when the function is done. Or is this no question of performance but rather of style, then, what is considered nicer?
43
2658
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 serious motivations for many issues... I hope you can help me :) I begin with the two major for now, others will come for sure! - function variables: they're used to java, so no pointers. Personally I'd use always pointers, but why could be better...
6
1703
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. Some developers feel any function that returns an object should copy it. This gets pretty expensive performance wise and development wise and hard to enforce. It also defeats a lot of what C# intentionally did. Other developers feel you should...
40
2176
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 blind', and can't get the non-ISO itoa to work as I'd like. The snow blindness makes me think it's me, and not the function! The idea of the program was to be able to change #define I_TYPE char to any scalar type, re-compile, and then play. ...
1
10976
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 select * from a join b on a.id = b.id
0
8801
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
9516
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
9351
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
9286
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
8229
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...
1
6774
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
4587
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
3294
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
3
2200
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.