Structures ans speed

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fred Bennett

    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
  • Chris Johnson

    #2
    Re: Structures ans speed

    In article <ea62206b.03092 80537.88b5e9d@p osting.google.c om>, Fred Bennett wrote:[color=blue]
    > 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?[/color]
    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 ".[color=blue]
    >
    > 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.[/color]
    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 "qripwbuafba@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! =-----

    Comment

    • WW

      #3
      Re: Structures ans speed

      Fred Bennett wrote:[color=blue]
      > 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.[/color]

      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


      Comment

      • Viatcheslav L. Gorelenkov

        #4
        Re: Structures ans speed

        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" <fmbennett@att. net> wrote in message
        news:ea62206b.0 309280537.88b5e 9d@posting.goog le.com...[color=blue]
        > 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[/color]


        Comment

        • Kevin Goodsell

          #5
          Re: Structures ans speed

          Viatcheslav L. Gorelenkov wrote:
          [color=blue]
          > Data members alignment in memory may be an issue.[/color]

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



          Why would alignment be an issue?
          [color=blue]
          >
          > 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).[/color]

          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).
          [color=blue]
          > 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.[/color]

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

          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.

          Comment

          • Viatcheslav L. Gorelenkov

            #6
            Re: Structures ans speed


            "Kevin Goodsell" <usenet1.spamfr ee.fusion@never box.com> wrote in message
            news:Q9Gdb.7741 $NX3.6959@newsr ead3.news.pas.e arthlink.net...[color=blue]
            > Viatcheslav L. Gorelenkov wrote:
            >[color=green]
            > > Data members alignment in memory may be an issue.[/color]
            >
            > 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?[/color]

            1. Because it affects speed (SUBJ).[color=blue]
            >[color=green]
            > >
            > > 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[/color][/color]
            alignment).[color=blue]
            >
            > 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).[/color]

            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).[color=blue]
            >[color=green]
            > > If your program is using things like SIMD (single instruction multiple[/color][/color]
            data)[color=blue][color=green]
            > > commands - data for SIMD must be aligned on 16-bytes boundaries for[/color][/color]
            better[color=blue][color=green]
            > > performance - otherwise it is no benefit from SIMD.[/color]
            >
            > C++ doesn't have support for that. If your compiler uses it to optimize,
            > presumably it will do the Right Thing.[/color]

            3. C++ does not, but C++ compilers does.[color=blue]
            >[color=green]
            > >
            > > Check natural data alignment for your processor/compiler and use[/color][/color]
            appropriate[color=blue][color=green]
            > > compiler code generation option.[/color]
            >
            > 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.
            >[/color]
            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.

            [color=blue]
            > -Kevin
            > --
            > My email address is valid, but changes periodically.
            > To contact me please use the address from a recent posting.
            >[/color]


            Comment

            • Kevin Goodsell

              #7
              Re: Structures ans speed

              Viatcheslav L. Gorelenkov wrote:
              [color=blue]
              > "Kevin Goodsell" <usenet1.spamfr ee.fusion@never box.com> wrote in message
              > news:Q9Gdb.7741 $NX3.6959@newsr ead3.news.pas.e arthlink.net...
              >[color=green]
              >>
              >>Why would alignment be an issue?[/color]
              >
              >
              > 1. Because it affects speed (SUBJ).[/color]

              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.
              [color=blue]
              >[color=green]
              >>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).[/color]
              >
              >
              > 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).[/color]

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

              [color=blue][color=green]
              >>
              >>C++ doesn't have support for that. If your compiler uses it to optimize,
              >>presumably it will do the Right Thing.[/color]
              >
              >
              > 3. C++ does not, but C++ compilers does.[/color]

              Here we discuss the former, not the latter.
              [color=blue][color=green]
              >>
              >>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.
              >>[/color]
              >
              > 4. Sometimes perfomance is a programmer problem, not compiler.[/color]

              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.
              [color=blue]
              > Seems to be you mixed two things: Portablity of the source code and compiler
              > code generation options. They are different.[/color]

              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.

              Comment

              • Viatcheslav L. Gorelenkov

                #8
                Re: Structures ans speed


                "Kevin Goodsell" <usenet1.spamfr ee.fusion@never box.com> wrote in message
                news:YHIdb.7007 $RW4.5622@newsr ead4.news.pas.e arthlink.net...[color=blue]
                > Viatcheslav L. Gorelenkov wrote:
                >[color=green]
                > > "Kevin Goodsell" <usenet1.spamfr ee.fusion@never box.com> wrote in message
                > > news:Q9Gdb.7741 $NX3.6959@newsr ead3.news.pas.e arthlink.net...
                > >[color=darkred]
                > >>
                > >>Why would alignment be an issue?[/color]
                > >
                > >
                > > 1. Because it affects speed (SUBJ).[/color]
                >
                > Speed of passing a struct to a function was the subject.[/color]

                1. I beleave it is "hit for usuing structures" in C++ programs.[color=blue]
                >
                > 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.
                >[color=green]
                > >[color=darkred]
                > >>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).[/color]
                > >
                > >
                > > 2. Yes, it "almost certainly" will. But sometimes you have to deal with
                > > naturaly un-aligned structures ( like BITMAPINFORHEAD ER in windows[/color][/color]
                bitmap[color=blue][color=green]
                > > image file) loaded from things like files (or sockets).[/color]
                >
                > I don't know what "un-aligned structures" are, but I don't believe that
                > a correct C++ program can create one.[/color]

                2. I'm not so lucky.

                Viatcheslav.
                [color=blue]
                >
                >[color=green][color=darkred]
                > >>
                > >>C++ doesn't have support for that. If your compiler uses it to optimize,
                > >>presumably it will do the Right Thing.[/color]
                > >
                > >
                > > 3. C++ does not, but C++ compilers does.[/color]
                >
                > Here we discuss the former, not the latter.
                >[color=green][color=darkred]
                > >>
                > >>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.
                > >>[/color]
                > >
                > > 4. Sometimes perfomance is a programmer problem, not compiler.[/color]
                >
                > 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.
                >[color=green]
                > > Seems to be you mixed two things: Portablity of the source code and[/color][/color]
                compiler[color=blue][color=green]
                > > code generation options. They are different.[/color]
                >
                > 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.
                >[/color]


                Comment

                • Fred Bennett

                  #9
                  Re: Structures ans speed

                  "WW" <wolof@freemail .hu> wrote in message news:<bl6pf4$6t e$1@phys-news1.kolumbus. fi>...[color=blue]
                  > Fred Bennett wrote:[color=green]
                  > > 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.[/color]
                  >
                  > 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.[/color]

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

                  Fred

                  Comment

                  • Chris Johnson

                    #10
                    Re: Structures ans speed

                    In article <ea62206b.03092 81624.517163c8@ posting.google. com>, Fred Bennett wrote:[color=blue]
                    > "WW" <wolof@freemail .hu> wrote in message news:<bl6pf4$6t e$1@phys-news1.kolumbus. fi>...[color=green]
                    >> Fred Bennett wrote:[color=darkred]
                    >> > 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?[/color]
                    >> 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.[/color]
                    >
                    > This shows my ignorance. How would you do this?
                    > Sorry for newbie question[/color]

                    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 "qripwbuafba@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! =-----

                    Comment

                    • Chris Johnson

                      #11
                      Re: Structures ans speed

                      In article <slrnbnf2gl.5lj .cjohnson@gesta lt.localdomain> , Chris Johnson wrote:

                      Since misunderstandin gs seem to run deep in this group I want to clarify my
                      original post:[color=blue]
                      > I suggest you get a copy of "The C++ Programming Language" by Bjarne Stroustrup
                      > as it will explain *my point* in great detail.[/color]
                      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.

                      --
                      Chris Johnson

                      echo "qripwbuafba@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! =-----

                      Comment

                      • glen stark

                        #12
                        Re: Structures ans speed

                        [color=blue]
                        >
                        > This shows my ignorance. How would you do this?
                        > Sorry for newbie question
                        >
                        > Fred[/color]

                        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

                        Comment

                        Working...