Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

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.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-10-2007, 09:24 AM
falco85 falco85 is offline
Programmer
 
Join Date: Apr 2006
Posts: 105
Rep Power: 10
falco85 is on a distinguished road
Default Making Code Faster

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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 01-11-2007, 04:01 PM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 3,418
Last Blog:
wxWidgets is NOT code ...
Rep Power: 37
WingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to behold
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-26-2007, 07:45 AM
AsmDeveloper AsmDeveloper is offline
Newbie
 
Join Date: Feb 2007
Posts: 2
Rep Power: 0
AsmDeveloper is on a distinguished road
Default

I think that the best method of this is use profilers. Examples: AMD CodeAnalyst, Intel VTune.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-26-2007, 04:04 PM
falco85 falco85 is offline
Programmer
 
Join Date: Apr 2006
Posts: 105
Rep Power: 10
falco85 is on a distinguished road
Default

I've never heard of any of these profilers. What do they do?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-26-2007, 09:32 PM
FlyByWire128 FlyByWire128 is offline
Newbie
 
Join Date: Feb 2007
Age: 21
Posts: 16
Rep Power: 7
FlyByWire128 is on a distinguished road
Default

Quote:
Originally Posted by falco85 View Post
I've never heard of any of these profilers. What do they do?
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 02-27-2007, 04:21 PM
Deathcry's Avatar   
Deathcry Deathcry is offline
Learning Programmer
 
Join Date: Feb 2007
Posts: 69
Rep Power: 7
Deathcry is on a distinguished road
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-28-2007, 11:38 AM
taskiyu taskiyu is offline
Newbie
 
Join Date: Feb 2007
Posts: 3
Rep Power: 0
taskiyu is on a distinguished road
Default

I used your net send program it was very god like!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 03-10-2007, 09:04 AM
Code Goddess's Avatar   
Code Goddess Code Goddess is offline
Newbie
 
Join Date: Mar 2007
Age: 30
Posts: 9
Rep Power: 0
Code Goddess is on a distinguished road
Default

Quote:
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;
That has exactly the same number of operations. I would expect a good compiler to produce exactly the same machine code from both snippets. In fact, it's possible that a compiler would have more trouble optimizing the compact code. Fewer lines doesn't mean faster. The first example could be "optimized" like this:
Code:
while ( --n >= 0 ) {
  *p = 0;
  ++p;
}
This assumes that n is a signed integral type. The prefix increment and decrement are logically more efficient than the postfix because temporary storage isn't required, but that's still pushing it because I don't know of a compiler that can't optimize those into equivalent machine code.

Ideally, you would optimize this loop by deferring to a library function that's heavily optimized for the system:
Code:
memset ( p, 0, n * sizeof *p );
You're more likely to get good results from a change like that than silly micro code optimizations that your compiler already does for you. Those kind of optimizations were okay two decades ago, but now you don't want to sacrifice clarity for dubious tiny increases in speed. Better yet, use smarter algorithms. That's where the biggest hits to performance come from.

Also, keep in mind that you can optimize your program all you want, but unless it's code bound, you're not going to get much improvement. If you spend most of your time waiting for results from a user, network connection, database, or pipe, it doesn't matter how fast your calculations are. The perceived performance is determined by the slowest link.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 03-12-2007, 01:00 PM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 3,418
Last Blog:
wxWidgets is NOT code ...
Rep Power: 37
WingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to behold
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 04-04-2007, 03:13 PM
thormj thormj is offline
Newbie
 
Join Date: Apr 2007
Posts: 1
Rep Power: 0
thormj is on a distinguished road
Default

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];
(err... slow!!!)
--
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);
  }
It will work ok for small things, but larger ones will run very slowly because calloc is trying to figure out where to get the memory (ditto for new and delete).
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

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


All times are GMT -5. The time now is 02:02 AM.

Contest Stats

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

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 100%


Complete - Celebrate!

Ads