Thread: Help me for selective reading.

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    1

    Thumbs up Help me for selective reading.

    Hey all friends out there .. I'm a noob at C programming (I enjoy programming a lot though) .
    I need your help in selecting the topics on C which help me accomplish the following 2 objectives:
    (A) Minimize the CPU execution time for a program.
    (B) Minimize the memory utilization for a program.
    Plz guide me as to which topics under C I should exactly read???
    I have 7 more days ..

    A request plz:
    I'm a Mechanical Engineering grduate who hasn't used anything more than while for etc loops in C ..(I used Matlab for all my programs yet ) .. so plz be a bit elaborate .. and ya even though this may not be possible in 7 days I'm not giving up until the 7th day so do reply even if there's 1% chance .
    Waiting for ur help .
    Good day .. bye

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Minimizing memory usage is pretty much a matter of cleaning up your variables and eliminating redundent code...
    Minimizing CPU usage is a matter for testing, testing and more testing.

    I shouldn't think either is a primary criterion in most general programming work, these days. Especially so, given the inherrent inefficiency of some modern programming languages.

    It may help if you can provide specific examples of your code that "needs" optimization...

    As for the reading request... Google is your friend. There are a ton of books out there about writing efficient code. However; you should be aware that for someone at the "if -while - for" level of programming, these will almost surely be beyond your skillset.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    My recommendation is to "just" learn C and then some basic understanding of A and B will come automatically. And then if you want to learn specific knowledge you will know what to search for.

    But the obligatory vague answer to A and B is
    Code:
    int main(void) { return 0; }

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    At the beginners level; choosing the best algorithm is the most important thing to do.

    Tim S.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by stahta01 View Post
    At the beginners level; choosing the best algorithm is the most important thing to do.

    Tim S.
    Hi Tim... while I do agree... I might suggest that at some level of noobness getting stuff to work at all is the most important thing...

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    stahta01: At the beginners level; choosing the best algorithm is the most important thing to do.
    Some things to look at here would be:
    1. Instead of sequential sort go with binary search or hash lookup.
    2. Instead of insertion/bubble sort go with quick sort, merge sort, radix sort.

    Other things to look at:

    1. Take a look at function inlining.

    2. Depending on what you are doing: loop unrolling/jamming/inversion. Mind you these may or may not always optimize your code depending on the compiler and what the intent is.

    3. Straight line code is faster to execute than branching code, aka ifs, &&s, switches.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Good cache utilization also becomes a factor. For example, a standard matrix multiply of order O(n^3) may be substantially improved if the result array is assigned/summed into one element at-a-time instead of writing to it in sequence thousands of times. Perhaps with the use of an intermediate subtotal vector. That is, you cater the nested loops such that memory accesses favour sequential writes at the expense of somewhat increased random reads. This assumes you have an in-depth knowledge of the characteristics of the cache in your machine.

    A thorough understanding of your machine's pipelining behaviour could help you hand-code high performance programs.

    But basically, yes, at the higher language level at which most people program, algorithm choice is the most important.

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    First off, this isn't a topic for C programming per se. It's a computer science question, which is only tangentially related to programming in any specific language. Look for courses or books in data structures and algorithm analysis.

    Anyway, in general, most algorithms trade size for speed or vice versa so it doesn't make sense to try and minimize both. For example, you can save and reuse the results of a complex calculation in a table - this gives you better speed at the cost of more memory usage. Or you can compress data when you store it - you take up less memory but it requires more calculation to compress and decompress.

    There's another class of optimizations which have some fixed setup cost which makes the rest of your per-data calculations faster. If you have enough data where the savings per-item overcomes the fixed setup/tear down cost, you win. If the data set is smaller than this break-even point, the more complex algorithm is actually slower.

    So with all of these tradeoffs, how do you write fast programs? You make the thing work first. With experience you can sometimes guess what needs to be optimized as you design the code, but as a beginner focus more on working code instead of making it fast. Your first instincts on where to spend your time will probably be wrong, so don't bother until the code works.

    Then build with compiler optimizations turned on and measure where it is slow. No point in wasting time, effort or complexity to gain a .05% speedup in code which is never used. Tools to look at here are profilers - gprof and oprofile are examples from the Unix world.

    When you do identify areas that are slow you fix them. As others have said, biggest gain here is choosing the right algorithms. Playing with compiler options would be next in the list. Way down the list would be tweaking the code to match the processor you're running on.

    Do you have a specific problem we can help with?

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Sri87 View Post
    (A) Minimize the CPU execution time for a program.
    The standard IT answer is to buy faster hardware.
    This is only partly a joke; I was told this a lot 10 years ago.
    Now, the answer is to buy more cores. This requires the program to work using multiple cores.
    Look for books on Multi-threading programming; note this is at least a few months long task (for advanced Programmers) do not start this for your project due in 7 days.

    Tim S.
    Last edited by stahta01; 06-23-2011 at 02:11 PM.

  10. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by stahta01 View Post
    The standard IT answer is to buy faster hardware.
    Tim S.
    Haha, interesting enough that is also Microsoft's answer for optimization.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading
    By p595285902 in forum C Programming
    Replies: 1
    Last Post: 03-24-2011, 06:28 PM
  2. Selective #include's
    By Malek in forum C++ Programming
    Replies: 4
    Last Post: 04-02-2003, 03:49 PM
  3. Reading selective numbers from a file
    By supaben34 in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2002, 01:56 PM