|
||||||
| C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
What methods do you guys use to make your C++ code faster? I've created a small query statement which I can't share but it executes very slow and uses more resources than anticipated. What can I do to make it faster?
|
| Sponsored Links |
|
|
|
|||||
|
Check optimization settings on your compiler, or try a different compiler, to start with.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum Programming is a branch of mathematics. |
|
|||
|
They'll analyze your program (code) and find out where the bottlenecks are. It's pointless to work on optimizing code just by looking at it, because something you think is difficult and something your computer thinks is difficult can be very different. Lots of things that seem very simple can run in O(n^2) or higher times. My last CS class made a big deal out of this stuff. Your program is only as fast as the slowest part of code, so until you know that, it's pointless to try to optimize the rest.
Hope this helps. |
| Sponsored Links |
|
|
|
|||||
|
just find places in your code to be optimized.
like instead of while (n-- > 0) { *p = 0; p++ } it could be something where it doesent have to do so many things like this while (n-- > 0) *p++ = 0; this isnt the only way to optimize your programs. you can find various books/ebooks on the subject. just googled heres some links C++ Optimization Strategies and Techniques Optimizing C and C++ Code u can also get books like this one Amazon.com: Code Optimization Techniques for Embedded Processors - Methods, Algorithms, and Tools: Books: Rainer, Leupers Last edited by Deathcry; 02-27-2007 at 04:26 PM. |
|
|||||
|
One of the first rules of optimizing is: let the compiler do it for you.
You are more likely to get mileage from making sure you have an efficient algorithm (such as O(ln(n)) vs O(n^2)) than from trying to do minor tweaks. Also, be aware of WHERE you are getting the most slowdown in your code. If your code runs slow because it does a ton of disk access, optimize there, not in the memory management.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum Programming is a branch of mathematics. |
|
|||
|
Is this query an SQL query, or a C/C++ algorithm or ?
A lot of issues with using SQL can be solved by using the correct indices. For C/C++, I usually end up single-stepping through the code through some test cases to see if I forgot something and the code is doing something I didn't expect (or that I see is stupid; eg, one (obvious & piggy) way compute a rolling average: Code:
for(i=0;i<buf_size;i++)
if(i> roll_size)
{
for(j=0;j<roll_size;j++)
sum+=input[i-j];
output[i]=sum/roll_size;
}
else
output[i]=input[i];
-- better way: Code:
sum=0;
for(i=0;i<roll_size;i++)
{
sum+=input[i];
output[i]=input[i];
}
for(i=roll_size;i<buffer_size;i++)
{
sum-=input[i-roll_size];
sum+=input[i];
output[i]=sum/roll_size;
}
Another caveat (vtune & the profilers can help show this): allocating memory is slow -- if you have something like Code:
for(i=;i<1000;i++)
{
z=BatchSize(i);
int *buf = calloc(z * SIZE_OF_ELEMENT);
...
free(buf);
}
|
| Sponsored Links |
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Basic Calculator | AfTriX | VB Tutorials | 3 | 02-29-2008 09:53 AM |
| Making it Standalone from Source Code (Python) | annannienann | Python | 8 | 10-03-2007 12:45 AM |
| Please Help With A C Program!! | siren | C and C++ | 7 | 04-17-2007 09:45 AM |
| Making your windows run much faster | ahsan16 | Tutorials | 7 | 01-17-2007 10:58 AM |
| WingedPanther | ........ | 2753.6 |
| Xav | ........ | 2704 |
| Brandon W | ........ | 1702.32 |
| John | ........ | 1207.73 |
| marwex89 | ........ | 1175.24 |
| morefood2001 | ........ | 966.05 |
| dcs | ........ | 655.75 |
| Steve.L | ........ | 475.59 |
| orjan | ........ | 418.58 |
| Aereshaa | ........ | 383.54 |