How to make C++ running efficient?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 11 of 11

Thread: How to make C++ running efficient?

  1. #1
    Join Date
    Aug 2005
    Posts
    14

    How to make C++ running efficient?

    HI, all,

    I have a block of following C++ code to run,

    vector<double> st(25000,0);

    for(int n=0;n<st.size();n++)
    for(int i=0;i<p.size();i++)
    if(n-i>0 && n-i<a.size()) st[n]+=p[i]*a[n-i];

    where a is a double vector of the same size as st, p is a double vector of size 2500.

    I found that this block takes considerable running time. Do you have any good idea to improve this block running efficiency?
    Thanks in advance.

  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    a cheap first pass at it.

    const int st_size = st.size(); //lets not call a function every iteration!
    const int p_size = p.size();
    const int a_size = a.size();
    register int i; //try to help the compiler make a smart decision

    for(int n=0;n<st_size;n++)
    for(i=0;i<p_size;i++)
    {
    const int nmi = n-i; //this is used a lot
    if(nmi>0 && nmi<a_size)
    st[n]+= p[i]*a[nmi];
    }

  3. #3
    Join Date
    Dec 2003
    Posts
    3,366
    Additional thoughts -- dont use double if you dont NEED double.
    Re-arranging the code carefully to access memory sequentially instead of random will reduce page faults (can consume considerable effort)

    Re-arranging the code to only have one loop will help free up registers and resources. Nontrivial coding effort.

    If you really, really need a boost drop the vector and use an array and manage the size yourself. There is some overhead in the vector class that can be cleared up at the cost of a nice object.

  4. #4
    Join Date
    Dec 2003
    Posts
    3,366
    Finally, I am almost certain you can lose the conditional(busted pipeline, many bad things) AND code the for loops in a way that reduces the "do nothing" iterations and only loops over valid indexes (that do work). This should cut out a lot of wasted time and should not be a difficult fix! A better algorithm is always the first step toward speed.

  5. #5
    Join Date
    Aug 2005
    Posts
    14
    Thanks a lot. I will see what I can do with these points.
    The vector and double type cannot be saved since this is a block in a class where they already exisit there.

  6. #6
    Join Date
    Nov 2003
    Posts
    4,118
    Another point: you probably can replace the double with int, execute the loop on a vector of int, and then copy the result to a vector of double. I see nothing that warrants the use of double anyway.

    Another, perhaps the most curcial point: turn your compiler's optimization switches on. Modern compilers are very good at optimizing loops, so perhaps you won't need to work very hard...
    Finally, I disagree with jonnin with respect to the register declaration. I doubt that Visual C++ (even in its older versions) heeds this hint since the register keyword, like auto, is completely transparent -- the compiler ignores it because it usually knows best which variables are the best candidates for being stored in the CPU's registers.
    Last edited by Danny; 10-14-2005 at 10:30 AM.
    Danny Kalev

  7. #7
    Join Date
    Dec 2003
    Posts
    3,366
    I havent had much luck with register to be sure. But I stick it in anyway as part of self documentation (hey reader, this is gonna be used a lot and might be a spot to somehow optimize).

    You can't see the incoming data (p and a) -- how can you know that double/floating type isnt necessary? I dont see that -- it looks for all the world like a matrix multiply to me.

  8. #8
    Join Date
    Nov 2003
    Posts
    4,118
    I guess you're right with respect to the double/float. Anyway, since I didn't see any positive evidence warranting the usage of a floating point datatype, I didn't exclude this option...
    As for register: I think I can agree with you. It doesn't matter to the compiler anyway, so at least the keyword documents that this variable is used frequently. I never use register and inline in my code, btw.
    Danny Kalev

  9. #9
    Join Date
    Dec 2003
    Posts
    3,366
    I *have* to use inline -- we have the cheap visual .net and it does not support optimization (the release is set to only inline marked functions and cannot be changed.)

  10. #10
    Join Date
    Nov 2003
    Posts
    4,118
    duh! why can't it optimize the code?
    Danny Kalev

  11. #11
    Join Date
    Dec 2003
    Posts
    3,366
    The cheap version has hardcoded the optimizer settings to "only functions labeled inline" among other dumb settings. I didnt see this when we bought it and now im stuck for a while -- I thought the cheap version lost the profiler and some less useful things.

Similar Threads

  1. Efficient Running SUM
    By Karel Semerák in forum Database
    Replies: 0
    Last Post: 01-29-2002, 05:19 AM
  2. Replies: 8
    Last Post: 11-04-2000, 11:44 AM
  3. Replies: 0
    Last Post: 09-26-2000, 01:00 PM
  4. STILL Can't make buttons WORK!!
    By superwebmonkey in forum Java
    Replies: 2
    Last Post: 05-13-2000, 10:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center