Thread: Compare methods

  1. #1
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154

    Compare methods

    I have these two methods in order to set the bits of a memory block to zero.

    First
    Code:
    char newHeader[BF_BLOCK_SIZE];
    
    //....
          //Set up new header
          memset(newHeader,0,BF_BLOCK_SIZE);
    
    //...
    Second
    Code:
    char newHeader[BF_BLOCK_SIZE];
    
    //...
         //Set up new header
         for(i=0;i<BF_BLOCK_SIZE;i++)
              newHeader[i]=0;
    
    //...
    Trying both i see no difference.
    Is there something that diversify those two methods or it's just the same way ?
    Last edited by ch4; 03-25-2009 at 07:40 AM.

  2. #2
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    depending on the underlying library implementation, one may be more efficient than the other. I would assume that memset might be more efficient in some cases.

    (since it may be utilizing wider buses by writing more than one char at once)

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It would depend a lot on the number of elements, and optimization settings of the compiler, as well as what processor is being used, to name a few factors.

    For nearly all cases except where the size is tiny, the memset call will be faster. If in doubt, measure it. [But unless it's really time-critical, who cares?]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154
    Thank you !

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    The only thing not going for memset() is the overhead of an extern function call.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by itCbitC View Post
    The only thing not going for memset() is the overhead of an extern function call.
    Any decent compiler will have an intrinsic implementation of memset() -- it should not result in a function call, at least if optimization is enabled.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by brewbuck View Post
    Any decent compiler will have an intrinsic implementation of memset() -- it should not result in a function call, at least if optimization is enabled.
    If that's the case then it's a win-win for memset().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generalising methods to handle multiple weapon types
    By Swarvy in forum Game Programming
    Replies: 2
    Last Post: 05-22-2009, 02:52 AM
  2. Turn Based Stradegy System Methods
    By TylerMoyer in forum Game Programming
    Replies: 2
    Last Post: 07-30-2007, 10:45 PM
  3. Lesson #5 - Methods
    By oval in forum C# Programming
    Replies: 1
    Last Post: 05-04-2006, 03:09 PM
  4. compare strings not working
    By gtriarhos in forum C Programming
    Replies: 7
    Last Post: 09-29-2005, 12:51 PM
  5. Replies: 8
    Last Post: 07-27-2003, 01:52 PM