Thread: Fastest Initialization of a Struct

  1. #1
    Registered User
    Join Date
    Apr 2004
    Location
    Ohio
    Posts
    147

    Fastest Initialization of a Struct

    I suck at searching and finding the things that I want so hopefully this isn't a common question that I just can't find.

    I've been pondering over the question of initializing a struct. Is it faster to initialize a struct using a list, by accessing each individual element or is there no discernable difference at all (e.g., it's just a matter of coding style)?

    Code:
    myStruct tmpStruct = {1, 2, 3};
    
    tmpStruct.a = 1;
    tmpStruct.b = 2;
    tmpStruct.c = 3;

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Code:
    ; 8    :     struct foo bar = {1, 2, 3};
    
    	mov	DWORD PTR _bar$[ebp], 1
    	mov	DWORD PTR _bar$[ebp+4], 2
    	mov	DWORD PTR _bar$[ebp+8], 3
    
    ; 9    :     struct foo baz;
    ; 10   :     baz.a = 1;
    
    	mov	DWORD PTR _baz$[ebp], 1
    
    ; 11   :     baz.b = 2;
    
    	mov	DWORD PTR _baz$[ebp+4], 2
    
    ; 12   :     baz.c = 3;
    
    	mov	DWORD PTR _baz$[ebp+8], 3
    I see no difference in terms of assembler code. This is a style matter, only. I did not even build an exe.

    Edit: woops, but still no difference.
    Last edited by whiteflags; 03-11-2009 at 10:59 PM.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No difference in assembler output for gcc 4.2.4 either, with or without optimisations enabled. If you are talking about which is faster to type, I'd say that using an initialiser usually is faster

    That said, it is not always a matter of style, e.g., you need it for const objects.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Apr 2004
    Location
    Ohio
    Posts
    147
    Great. Thanks for the replies. I don't actually use structs in my own code as I'm sticking with strictly OOP objects in C++ but I'm using several API's that are written in C. I only ask because I know it can be a lot faster to use an initialization list for a class so I wondered if the same benefits applied to C structs.

    For me having fewer lines of code makes sense when setting up these structs so I'll stick with that.

    Thanks again!

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    The compiler is free to do pretty much anything. It could even vary from one initialization to another, by the same compiler. Initialization of a large struct could even be accomplished by a call to memcpy().

    I think it's unlikely that any decision the compiler makes about this sort of thing is going to significantly impact performance.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Apr 2004
    Location
    Ohio
    Posts
    147
    I didn't think so. I assumed that it would all be pretty much the same and any differences would be negligable.

    I'm using the SDL struct SDL_Rect which only has four values. In this case it makes far more sense to have a single line setting all of those values than having five lines to define the struct and set each value. I guess you could call it a case of readability vs. any real improvement in performance.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Struct Initialization
    By carrotcake1029 in forum C Programming
    Replies: 5
    Last Post: 05-06-2008, 02:27 PM
  4. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  5. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM