Author Topic: speed/optimization in c++  (Read 1628 times)

0 Members and 1 Guest are viewing this topic.

Offline Muhammad

  • /dev/null
  • *
  • Posts: 10
  • Cookies: 4
    • View Profile
speed/optimization in c++
« on: May 02, 2013, 12:40:36 am »
Hi guys,

I've been writing c++ for more than two years, and It's easy for me right now to write any code, yet the speed of my codes is slow (comparing to what it should be). I've not studied programming/algorithms (my study area is very far from this field) maybe that's the reason why I'm not familiar with basic stuff to get a code that is fast enough.

So, I'd like you to advice me on how to learn this basic speed tips like what to do and what to avoid (books, websites or any are welcomed).     

one last thing :)
I've recently joined EZ, actually just yesterday and by chance too  :D , and I've really enjoyed what you've got here. I looked at the different sections on the forum and definitely I'll be spending some time here from now on :)

cheers
« Last Edit: May 02, 2013, 12:57:17 am by Muhammad »

Offline rasenove

  • Baron
  • ****
  • Posts: 950
  • Cookies: 53
  • ಠ_ಠ
    • View Profile
Re: speed/optimization in c++
« Reply #1 on: May 02, 2013, 08:55:45 am »
First pleas introduce yourself in the introduction board.

And here is a link. www.en.wikibooks.org/wiki/Optimizing_C%2B%2B/Writing_efficient_code/Performance_improving_features


also, do tell where you learned c++, i can link you to a book list for almost all levels of c++ studyes if you want.
Cheers.
« Last Edit: May 02, 2013, 08:56:40 am by rasenove »
My secrets have secrets...

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: speed/optimization in c++
« Reply #2 on: May 02, 2013, 09:16:06 am »
Can you give us a snippet of a slow program you wrote? That would make it somewhat easier to point out the problem in your coding style.
~Factionwars

Offline Muhammad

  • /dev/null
  • *
  • Posts: 10
  • Cookies: 4
    • View Profile
Re: speed/optimization in c++
« Reply #3 on: May 02, 2013, 02:21:32 pm »
@rasenove
oh sorry I should have introduced my self there, anyways, I'll do it again there =D

Thanks for the link you provided, but actually I didn't mention that I searched for optimization many times before, and I did came across this website and I've recently downloaded a book called "Optimizing software in C++" by Agner Fogb that helped me.

To make my question clear: for example there is a tip saying (I've seen this one during my search):
instead of doing this: x/2.0 do x*0.5 (multiplication is faster) ..

Classes are very nice to organize your code but it slows down you code.
I need this kind of tips. increasing the speed of my code with a fraction of a second will be something :)

I've learned c++ from cplusplus.com, youtube, and most of the time I just googled what I needed to know, I've got most of the answers/ideas from stackoverflow.

I'm really looking for tips that may look very basic (even silly) to any of you to say, but actually I don't have these tips.

@Factionwars


here is a snippet of a code I did some optimization for:


the loop is to update an array (line segments) with new segments after trimming the line.
From timing results I've found that making the three variables c1,c2,c3 increased the speed a bit since I only now caculate each one one time
 
Code: [Select]

        bool c1,c2,c3;


for(iseg = 0 ; iseg < num ; iseg++)
{
min = _tmp_line[iter];iter++;

max = _tmp_line[iter];iter++;

if(!( pmin < min && pmax > max ))
{
c1 = pmin > min && pmin < max && pmax > max;


if(c1)
{
_line[iter2] = min;iter2++; //1st seg
_line[iter2] = pmin;iter2++;
}
c2 =  pmax < max && pmax > min && pmin < min;

if(c2)
{
_line[iter2] = pmax;iter2++; //1st seg
_line[iter2] = max;iter2++;
}
c3 = pmin > min && pmax < max; 
if( c3 && !c1 && !c2 ) //split
{
_line[iter2] = min;iter2++; //1st seg
_line[iter2] = pmin;iter2++;

_line[iter2] = pmax;iter2++; //2nd seg
_line[iter2] = max;iter2++;


}


if(!c1 && !c2 && !c3)
{
_line[iter2] = min;iter2++; //1st seg
_line[iter2] = max;iter2++; //1st seg
}
}


thanks for you replys guys !

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: speed/optimization in c++
« Reply #4 on: May 02, 2013, 06:08:28 pm »
Read about bitwise operations and data structures.  Also note that pre-increment is guaranteed to be no slower than post-increment but could, potentially, be faster.  So use ++i rather than i++ wherever possible.

Code: (cpp) [Select]
if (!(c1 | c2 | c3))  // bitwise operations can simplify the task & allow the compiler to better optimize
-Xires

Offline Muhammad

  • /dev/null
  • *
  • Posts: 10
  • Cookies: 4
    • View Profile
Re: speed/optimization in c++
« Reply #5 on: May 03, 2013, 04:32:21 am »
@Xires
well, I know about bitwise operator, but I don't use them very often (maybe I should try them whenever possible), and I will make a piece of code just to get a feeling of the speed of pre-increment tip.

Now I'll take a look at what I've got from the replys. Thanks all :)
« Last Edit: May 03, 2013, 04:39:37 am by Muhammad »