Thread: Memory Use and Optimization

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    1

    Memory Use and Optimization

    I have a very large program that I have inherited that I need to optimize and improve. (It has a number of componants that include C, Pro*C and Stored procedures in an Oracle database all running on HP Unix).

    There are a large amount of calls to memcmp and memset.

    The questions are

    1. Would it be quicker to use strcmp than memcmp (since I am comparing strings)?

    2. Rather than clearing allocated memory by memseting it to null would it be better to free then malloc or even realloc?

    I'm fairly new to C and have not yet mastered the art of optimization!

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    1) memcmp() is almost certainly used at the heart of strcmp() so I would expect memcmp() to be slightly faster but probably not so much that you would notice unless its being called thousands of times.
    2)memset() is much faster than malloc() and they do a different job. In general I dont think you could replace calls to memset() with calls to malloc() or realloc()
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Unless you're doing a vast amount of memset etc, this is unlikely to help much.

    My initial guess would be the oracle database, but if it only calls it once, and the rest of the processing is in the C code, maybe it's not that at all.

    The only real answer is to see if your compiler has a profile option, which automatically adds code to count how much time is being spent in various parts of the code. Only when you know this can you effectively target your efforts.

    > that I have inherited that I need to optimize and improve
    By how much?
    5%
    50%
    more? less?

    Bear in mind the following (assuming a PC platform here, but you'll get the idea)
    If it's only running on one machine, it will be cheaper to buy a new machine which is twice as fast, than to spend 1 week of your companies time on trying to improve the code.

    On the other hand, if you have 1000 machines costing $10000, then maybe it's worth a few weeks effort on your part

  4. #4
    Sayeh
    Guest
    Is there a reason they are memsetting? Let me rephrase-- why are they initializing the buffers? I mean, sometimes it is in fact necessary to do that. If this is _not_ one of those times, remove the memset() calls.

    When you say "large" program, are you speaking about how much sourcecode it has, or how much memory it uses when it runs?

    We need to know a little bit more to understand in what ways it needs optimized. Are we optimizing for performance, or space?

    Please post when you can.

    enjoy.

Popular pages Recent additions subscribe to a feed