Structures ans speed

F

Fred Bennett

I have a simulation project in which data can naturally be held in
structures for processing. There are calls to multiple functions
involved. Execution speed is an issue. Do I take a big hit for usuing
structures? Is there a preferred way of passing the structures (or
pointers?) that would speed up the program?

I'm relatively new to C++. so, if you have time, detail (or
references) would be appreciated. In any case any help is much
appreciated.

Fred
 
C

Chris Johnson

I have a simulation project in which data can naturally be held in
structures for processing. There are calls to multiple functions
involved. Execution speed is an issue. Do I take a big hit for usuing
structures? Is there a preferred way of passing the structures (or
pointers?) that would speed up the program?
Pointers and references generally will give you a speed increase when
used in the context of passing as parameters to functions. Better to just
pass the address than to mail the whole house (so to speak)

Static processing will usually beat out dynamic processing. Creation and
destruction of memory always eat cpu cycles so the more of that you can
minimize the more cpu cycles you have for "processing".
I'm relatively new to C++. so, if you have time, detail (or
references) would be appreciated. In any case any help is much
appreciated.
A general rule that I picked up through experience and listening to others
that I have looked up to for mentoring is to always code for correctness first
then optimize last. It works for me but your YMMV. Emperical knowledge comes
from learning and using what you learn so time and practice will improve your
understanding. That is about the best help I can offer.

C Johnson
 
W

WW

Fred said:
I have a simulation project in which data can naturally be held in
structures for processing. There are calls to multiple functions
involved. Execution speed is an issue. Do I take a big hit for usuing
structures? Is there a preferred way of passing the structures (or
pointers?) that would speed up the program?

I'm relatively new to C++. so, if you have time, detail (or
references) would be appreciated. In any case any help is much
appreciated.

If you work with C++ why do you use structures with data, why not real
classes with member functions?

And no, usually there is no penalty. Passing many pointers or variables
around will usually end up taking the same amount or more time than passing
around pointers or references to structures. But usually you do not want to
pass them around as copies.

You also have to remember that it will be the algorithms and the more
complex data structures (like lookup tables) you choose, which will have the
most noticeable effect on your runtime speed.
 
V

Viatcheslav L. Gorelenkov

Data members alignment in memory may be an issue.

For Intel-32 processors data members for best perfomance must starts on
4-bytes boundaries and compiler must add padding bytes for data be
aligned - (hopefully - for MS compilers this is default memory alignment).
If your program is using things like SIMD (single instruction multiple data)
commands - data for SIMD must be aligned on 16-bytes boundaries for better
performance - otherwise it is no benefit from SIMD.

Check natural data alignment for your processor/compiler and use appropriate
compiler code generation option.

Viatcheslav
 
K

Kevin Goodsell

Viatcheslav said:
Data members alignment in memory may be an issue.

Please don't top-post. Read section 5 of the FAQ for posting guidelines.

http://www.parashift.com/c++-faq-lite/

Why would alignment be an issue?
For Intel-32 processors data members for best perfomance must starts on
4-bytes boundaries and compiler must add padding bytes for data be
aligned - (hopefully - for MS compilers this is default memory alignment).

The compiler will almost certainly align the structs for best
performance. The programmer doesn't need to worry about that (and
couldn't do anything about it anyway, in portable code).
If your program is using things like SIMD (single instruction multiple data)
commands - data for SIMD must be aligned on 16-bytes boundaries for better
performance - otherwise it is no benefit from SIMD.

C++ doesn't have support for that. If your compiler uses it to optimize,
presumably it will do the Right Thing.
Check natural data alignment for your processor/compiler and use appropriate
compiler code generation option.

I still don't know why you think this would be necessary. This is the
kind of thing the compiler worries about, not the programmer. Optimizing
for a particular platform is rather foolish - better to have portable
code you can move to a faster platform if you need better performance.

But the issue here is passing a struct to a function. If the struct's
alignment is wrong, poor performance is the least of your worries.
Either the struct has correct alignment or your program is broken. If
you've done something to break the alignment of your struct, you have
bigger problems than efficiently passing it to a function.

-Kevin
 
V

Viatcheslav L. Gorelenkov

Kevin Goodsell said:
Please don't top-post. Read section 5 of the FAQ for posting guidelines.

http://www.parashift.com/c++-faq-lite/

Why would alignment be an issue?

1. Because it affects speed (SUBJ).
alignment).

The compiler will almost certainly align the structs for best
performance. The programmer doesn't need to worry about that (and
couldn't do anything about it anyway, in portable code).

2. Yes, it "almost certainly" will. But sometimes you have to deal with
naturaly un-aligned structures ( like BITMAPINFORHEADER in windows bitmap
image file) loaded from things like files (or sockets).
C++ doesn't have support for that. If your compiler uses it to optimize,
presumably it will do the Right Thing.

3. C++ does not, but C++ compilers does.
I still don't know why you think this would be necessary. This is the
kind of thing the compiler worries about, not the programmer. Optimizing
for a particular platform is rather foolish - better to have portable
code you can move to a faster platform if you need better performance.

But the issue here is passing a struct to a function. If the struct's
alignment is wrong, poor performance is the least of your worries.
Either the struct has correct alignment or your program is broken. If
you've done something to break the alignment of your struct, you have
bigger problems than efficiently passing it to a function.
4. Sometimes perfomance is a programmer problem, not compiler.
Seems to be you mixed two things: Portablity of the source code and compiler
code generation options. They are different.


Viatcheslav.
 
K

Kevin Goodsell

Viatcheslav said:
1. Because it affects speed (SUBJ).

Speed of passing a struct to a function was the subject.

From a language perspective, it affects correctness. An incorrect
program may be slow. It may never finish at all. It may be faster. It's
not defined.
2. Yes, it "almost certainly" will. But sometimes you have to deal with
naturaly un-aligned structures ( like BITMAPINFORHEADER in windows bitmap
image file) loaded from things like files (or sockets).

I don't know what "un-aligned structures" are, but I don't believe that
a correct C++ program can create one.

3. C++ does not, but C++ compilers does.

Here we discuss the former, not the latter.
4. Sometimes perfomance is a programmer problem, not compiler.

I didn't claim otherwise. But *alignment* is handled (as it must be) by
the compiler. The language provides no way for the programmer to
determine object alignment requirements or change the way objects are
aligned.
Seems to be you mixed two things: Portablity of the source code and compiler
code generation options. They are different.

I'm quite sure I didn't mix those up. But since this is a C++ language
group, the latter is off-topic anyway. I assume if the OP wanted system-
or compiler-specific answers, he would have posted to a group that
discusses his particular system or compiler.

-Kevin
 
V

Viatcheslav L. Gorelenkov

Kevin Goodsell said:
Speed of passing a struct to a function was the subject.

1. I beleave it is "hit for usuing structures" in C++ programs.
From a language perspective, it affects correctness. An incorrect
program may be slow. It may never finish at all. It may be faster. It's
not defined.


I don't know what "un-aligned structures" are, but I don't believe that
a correct C++ program can create one.

2. I'm not so lucky.

Viatcheslav.
 
F

Fred Bennett

WW said:
If you work with C++ why do you use structures with data, why not real
classes with member functions?

And no, usually there is no penalty. Passing many pointers or variables
around will usually end up taking the same amount or more time than passing
around pointers or references to structures. But usually you do not want to
pass them around as copies.

You also have to remember that it will be the algorithms and the more
complex data structures (like lookup tables) you choose, which will have the
most noticeable effect on your runtime speed.

This shows my ignorance. How would you do this?
Sorry for newbie question

Fred
 
C

Chris Johnson

This shows my ignorance. How would you do this?
Sorry for newbie question

This is really a complex subject on which there is no "silver bullet" and you
must decide which is the best style to use.

I suggest you get a copy of "The C++ Programming Language" by Bjarne Stroustrup
as it will explain my point in great detail. My point also coming from the
advice presented in that book.

classes and structs can be used in different ways and in some degree the same
way. There are *preferred* ways based on a lot of different variables, or
should I really say *viewpoints*(?). The only reason I bothered to post
anything at this point is "There is no one-size fits all" solution. Keep that
in mind when you get responses to this question.

"How would you do this?" -- I would use whatever techniques I know how to use at
the time and force myself to learn new techniques to improve upon them being
mindful of not using a feature just for the sake of using it.
 
C

Chris Johnson

Since misunderstandings seem to run deep in this group I want to clarify my
original post:
I suggest you get a copy of "The C++ Programming Language" by Bjarne Stroustrup
as it will explain *my point* in great detail.
as it will *explain this concept* in great detail.

I do not want to infer or otherwise be misunderstood taking any kind of
credit for the wealth of information Mr. Stroustrup presents in his book.
 
G

glen stark

This shows my ignorance. How would you do this?
Sorry for newbie question

Fred

Hey dude. I do numeric programming for a living, although I feel like
I'm just now getting the hang of it.

Stroustrup is a good *technical* reference, but it's pretty hard to
read, especially if you are new to programming. I personally have two
books that are almost always open on my desk: The C++ primer by S.
Lippman, and the Stroustrup book. The Stroustup book is great, but not
always decipherable when you are just starting out. This is especially
true when you really just want to figure out something new.

Depending on your time constraints, take a couple days to just read the
lippman and/or stroustrup books (or something else). Familiarize
yourself with classes and member functions. It's really handy and will
save you a lot of time in the long run.

Then sit down and break down your program: What do you need to have
running right away (for testing purposes). What do you want in an ideal
world where you have lots of time and programming expertise? What does
your simulation do? Input? Output?

Then: Are there any libraries out there you can use to simplify your
work (google, www.boost.org, www.oonumerics.org)? If so, use them.
Then sketch out your program a little. Make a kind of outline how you
want things to work. What classes will store what data, and what
function will they do? A class is like a structure, but also contains
functions. It's handy. Really.

As for efficiecy, that comes a bit with practice. Try not to worry too
much. Premature optimization is the root of all evil. On the other
hand we can't ignore efficiency. Try to avoid doing anything obviously
bad. When it's done, if it runs too slow, try to figure out why, and
how to improve it (profiling tools).

If you have a numeric programmer around, talk to them as your working.

Finally, I have a collection of simulations I've programmed in C++ on my
website www.glenstark.org (go to Programs). you can look it over and
see if anything is helpful, but be warned: I'm a mediocre programmer,
and my early stuff is especially mediocre.

Glen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top