473,738 Members | 10,643 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Structures ans speed

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
Jul 19 '05 #1
11 2736
In article <ea************ *************@p osting.google.c om>, Fred Bennett wrote:
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
--
echo "qr*********@rk pvgr.pbz" | rot13
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 19 '05 #2
WW
Fred Bennett wrote:
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.

--
WW aka Attila
Jul 19 '05 #3
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
"Fred Bennett" <fm*******@att. net> wrote in message
news:ea******** *************** **@posting.goog le.com...
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

Jul 19 '05 #4
Viatcheslav L. Gorelenkov wrote:
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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #5

"Kevin Goodsell" <us************ *********@never box.com> wrote in message
news:Q9******** *********@newsr ead3.news.pas.e arthlink.net...
Viatcheslav L. Gorelenkov wrote:
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?


1. Because it affects speed (SUBJ).

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).
2. Yes, it "almost certainly" will. But sometimes you have to deal with
naturaly un-aligned structures ( like BITMAPINFORHEAD ER in windows bitmap
image file) loaded from things like files (or sockets).
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.


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

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.

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.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #6
Viatcheslav L. Gorelenkov wrote:
"Kevin Goodsell" <us************ *********@never box.com> wrote in message
news:Q9******** *********@newsr ead3.news.pas.e arthlink.net...

Why would alignment be an issue?

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.
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 BITMAPINFORHEAD ER 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.


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.


Here we discuss the former, not the latter.

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.


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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #7

"Kevin Goodsell" <us************ *********@never box.com> wrote in message
news:YH******** *********@newsr ead4.news.pas.e arthlink.net...
Viatcheslav L. Gorelenkov wrote:
"Kevin Goodsell" <us************ *********@never box.com> wrote in message
news:Q9******** *********@newsr ead3.news.pas.e arthlink.net...

Why would alignment be an issue?

1. Because it affects speed (SUBJ).


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.
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 BITMAPINFORHEAD ER 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.


2. I'm not so lucky.

Viatcheslav.


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.


Here we discuss the former, not the latter.

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.


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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #8
"WW" <wo***@freemail .hu> wrote in message news:<bl******* ***@phys-news1.kolumbus. fi>...
Fred Bennett wrote:
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.


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

Fred
Jul 19 '05 #9
In article <ea************ **************@ posting.google. com>, Fred Bennett wrote:
"WW" <wo***@freemail .hu> wrote in message news:<bl******* ***@phys-news1.kolumbus. fi>...
Fred Bennett wrote:
> 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?

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

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


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.

--
Chris Johnson

echo "qr*********@rk pvgr.pbz" | rot13
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 19 '05 #10

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

Similar topics

7
1994
by: bearophileHUGS | last post by:
(This is a repost from another python newsgroup). While using some nested data structures, I've seen that I'd like to have a function that tells me if a given data structure contains one or more cyclic references (a way to recognise a cycle in a graph is to do a depth-first search, marking vertices along the way. An already marked vertex means a cycle.) Do you know where I can find a function like this? To be more explicit about this...
8
2150
by: bearophileHUGS | last post by:
I'm frequently using Py2.4 sets, I find them quite useful, and I like them, even if they seem a little slower than dicts. Sets also need the same memory of dicts (can they be made to use less memory, not storing values? Maybe this requires too much code rewriting). I presume such sets are like this because they are kind of dicts. If this is true, then converting a dict to a set (that means converting just the keys; this is often useful for...
14
3548
by: Peter Olcott | last post by:
I want to be able to efficiently build data structures at run-time. These data structures need to be accessed with minimal time. The only a few ways that come immediately to mind would be some sort of dynamically allocated array of : (1) void pointers, that must be cast into the desired types. (2) union of the desired pointers. (3) An Inheritance hierarchy (4) Possibly a union of the desired data types.
5
2268
by: el_roachmeister | last post by:
For being a good web programmer, is a course on data structures important? It seems php already has built-in functions for what they teach in a data structures course. On the other hand all universities seem to teach this class. I tried taking one but just found it too boring and irrelevant for what I was doing. What are your thoughts?
8
1907
by: Vijay Kumar R Zanvar | last post by:
Hi c.l.c, The following table show the general structure of a header: Byte # Field Description ------ ----------------- 0 Extended message flag (01H) 1 Extended message length (n)
26
4831
by: Olaf Baeyens | last post by:
I am trying to port some C++ structures to C# but I have troubles with this one. Note: this is a file record, so I must keep this format. #pragma pack( push, 1 ) typedef struct { char title; DWORD dwCount; } STLHEADER, *LPSTLHEADER;
6
4487
by: Ken Allen | last post by:
OK, I admit that I have been programming since before C++ was invented, and I have developed more than my share of assembly language systems, and even contributed to operating system and compiler systems over the years. I have developed code in more than 30 distinct programming languages for a wide cariety of industries. But this issue of structures in C# is beginning to annoy me. I should also make it clear that I am not a big supporter...
4
2217
by: Dave | last post by:
Hi. I am learning PyOpenGL and I am working with a largish fixed scene composed of several thousand GLtriangles. I plan to store the coords and normals in a NumPy array. Is this the fastest solution in python? would i be significantly better off (timewise or otherwise) using some other data structure to hold this information? no other data structure appears as convenient, but am i loosing valuable time? I am new to 3d programming so...
32
1524
by: Paulo J. Matos | last post by:
Hi all, I am a Python beginner, reading through 2.6 tutorial. I am wondering where are structures? On the other hand, I think I might have the answer. Since Python focus on having one way to do it and structures are something like classes with only public methods, if I want structures that's what I should use. Is that right?
0
8788
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
9476
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
9335
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
9263
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
9208
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
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4570
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
3279
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
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.