Thread: which is faster?

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    22

    which is faster?

    Suppose I have some kind of structure:
    Code:
    typedef struct MyStruct {
       int a;
    }
    will there be any difference in the execution time of this code:
    Code:
    MyStruct* msp;
    msp = new MyStruct;
    and this code:
    Code:
    MyStruct ms;
    and if there is, how significant will it be?
    And a related question: which (if either) of these will be faster - this:
    Code:
    msp->a = 5;
    or this:
    Code:
    MyStruct& msr = &ms;
    ms.a = 5;
    Thanks.

  2. #2
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    It depends on the implementation but the second one should be a tiny bit faster because the memory is allocated at execution rather than runtime. Also your third would be faster than the fourth or the same depending on whether the optimiser picks it up.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    22
    Brian, thanks!

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Which is faster should be irrelevant to you. Which suits what you are tryign to accomplish better? You should be more concerned with how to clearly solve your problem and work on speed later.

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Echo orbitz.

    And the speed difference is going to be imperceptible. Go with whichever is more appropriate to the particular situation.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    And in general, you create variables on the stack unless you have a reason not to. Reasons to use new to allocate on the heap include a need to maintain the object lifetime beyond the current scope, or if the object is large and requires a lot of memory (e.g. a huge array).

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    It's generally more helpful, in my opinion, to think about how to create objects not by the stack or heap (since tehse arn't even C++ terms) but their lifetime. Allocate an object locally when it should die when exiting the current scope. Allocate it on the freestore when it needs to live to an undetermined point.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    orbitz is on the money here, premature optimisation is a killer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Faster bitwise operator
    By Yarin in forum C++ Programming
    Replies: 18
    Last Post: 04-29-2009, 01:56 PM
  2. Faster way of printing to the screen
    By cacophonix in forum C Programming
    Replies: 16
    Last Post: 02-04-2009, 01:18 PM
  3. Computations - which is faster?
    By ulillillia in forum C Programming
    Replies: 9
    Last Post: 12-09-2006, 10:23 PM
  4. does const make functions faster?
    By MathFan in forum C++ Programming
    Replies: 7
    Last Post: 04-25-2005, 09:03 AM
  5. Floating point faster than fixed-point
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-08-2001, 11:34 PM