Thread: c++ time saving techniques

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    10

    c++ time saving techniques

    Hi ....
    Could you please refer me some materials for effective c++ time saving techniques, apart from the Dummies Reference Manual ......... It would be really very helpful if someone could provide such sort of reading material.....

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What sort of "timesaving" are you after?
    - Shorter execution time
    - Shorter time to produce the code
    - Less maintenance in the long run

    Or something else?

    --
    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.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    10
    what am currently doing is researching on all the techniques that one would have ignored if not known ....... e.g.

    C++ stream I/O is a very flexible and safe method of doing input and output. It allows you to define specific output formats for your own objects. If an object doesn't support output, the compiler will tell you. Our old friend printf, on the other hand, is not very safe. If you specify the wrong number of parameters, or give the wrong order, you crash. You can't define new output formats, either. But printf does have a few things going for it: it's fast, it's easy to use, and it's often easier to read than long lines of << operators



    At this point I am trying to open up in all the directions, be them compiler based or user based.....
    one more example:
    Try avoiding function call at the for loop condition check

    for ( ninitialize = 0 ; ninitialize < vec_nVal.size() ; i++ ) //Not Preferred

    int nVecSize = vec_nVal.size(); //Preffered
    for ( ninitialize = 0 ; ninitialize < nVecSize ; i++ )

    ..........

    I hope I have made myself clear..... What I want to do is teach some general time saving techniques to students that they might not be aware of.......

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I've always thought that the "Not Preferred" way is actually more idiomatic. Anyway, those couple of microseconds you might (or might not) gain wouldn't make any noticeable difference in a large majority of loops of the program. (In your example it seems to mask the small error that you have chosen an unsuitable type for storing the size - size_t should be more proper . In the "Not Preferred" version you'd be warned about that by the compiler.)

    Not so much with functions that are known to be expensive, such as strlen, though.

    I was going to post a link, but your bolded quote appears to come from that.

    In general, I guess, worry less about small inefficiencies (only if they actually cause performance problems). Does it make you happy if program output rushes by 10&#37; faster, if you are not going to be able to read it one way or another (and if nobody is going to read it, why not omit output altogether and gain a major speed boost)? (Concerning the ease of reading format strings, you might look into boost::format which combines the benefits of readable format string with type checking - but it might be slower than both alternatives.)
    Last edited by anon; 09-15-2008 at 09:25 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by gvkalra View Post
    what am currently doing is researching on all the techniques that one would have ignored if not known ....... e.g.

    C++ stream I/O is a very flexible and safe method of doing input and output. It allows you to define specific output formats for your own objects. If an object doesn't support output, the compiler will tell you. Our old friend printf, on the other hand, is not very safe. If you specify the wrong number of parameters, or give the wrong order, you crash. You can't define new output formats, either. But printf does have a few things going for it: it's fast, it's easy to use, and it's often easier to read than long lines of << operators



    At this point I am trying to open up in all the directions, be them compiler based or user based.....
    one more example:
    Try avoiding function call at the for loop condition check

    for ( ninitialize = 0 ; ninitialize < vec_nVal.size() ; i++ ) //Not Preferred

    int nVecSize = vec_nVal.size(); //Preffered
    for ( ninitialize = 0 ; ninitialize < nVecSize ; i++ )

    ..........

    I hope I have made myself clear..... What I want to do is teach some general time saving techniques to students that they might not be aware of.......
    As anon points out, calling vec_nVal.size() in the loop is probably not too bad. It is much worse to call for example strlen() or the size of a linked list (if the list doesn't keep a count of how many items there are in the list, that is). And it may well be that if size is an inline function, there is very little, if any, difference between the "call size" and "use an extra variable".

    --
    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.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suppose that while shifting that function call has minimal impact, there might be some gain, and it costs very little to implement, so perhaps it can be classified as a "don't pessimise prematurely" optimisation. More obvious examples would include using the prefix form of operator++ and operator-- for iterators, and passing expensive to copy objects by const reference instead of by value (unless a copy will be done anyway).

    The use of generic algorithms can also ease the use of these three "techniques" altogether since you would not manually perform the loop condition test and increment/decrement, and since you pass a range with cheap to copy iterators, you would not be passing a container by value.
    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

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Make sure you learn and use RAII, and idioms such as copy and swap.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Help with assignment!
    By RVDFan85 in forum C++ Programming
    Replies: 12
    Last Post: 12-03-2006, 12:46 AM
  3. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM
  4. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM