Fastest Initialization of a Struct

This is a discussion on Fastest Initialization of a Struct within the C Programming forums, part of the General Programming Boards category; I suck at searching and finding the things that I want so hopefully this isn't a common question that I ...

  1. #1
    Professional Chef leeor_net's Avatar
    Join Date
    Apr 2004
    Location
    Charles Town, WV
    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;
    - Leeor

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    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
    21,883
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Professional Chef leeor_net's Avatar
    Join Date
    Apr 2004
    Location
    Charles Town, WV
    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!
    - Leeor

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    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
    Professional Chef leeor_net's Avatar
    Join Date
    Apr 2004
    Location
    Charles Town, WV
    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.
    - Leeor

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-20-2009, 11:37 PM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05: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, 04:04 AM
  5. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21