Thread: Program speed and function calls

  1. #1
    Registered User
    Join Date
    Aug 2005

    Program speed and function calls

    I'm writing a little program. Right now most of my code is all inline with the main() function. It pretty much reads from top to bottom without making "citations." The code difficult to follow and looks very amateurish. I could neaten everything up a bit with a more complex program structure. If I do this, will my program still execute with the same fantastic speed?

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Toronto, Ontario, Canada
    Yes, adding comments will not decrease the overall efficiency of your program. Same with white space.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > If I do this, will my program still execute with the same fantastic speed
    So long as your functions do a meaningful amount of work, you would hardly notice. The improvements to the code usually outweigh such details.

    This would be for example a pretty pointless thing.
    void assign ( int *loc, int val )  {
      *loc = val;
    Which you might call later
    assign( &myvar, 0 );
    where all you really want is
    myvar = 0;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Still in the egg
    Well, the compilers are smart enough nowadays (unless you use a compiler from stone age!). Anyway, write your program in a sensible manner(i mean readable form). The compiler translates your functions into inline main functions where it is necessary. Actually, this is a bit compiler dependent .For exmaple gcc has several optimization options, each produces (most of tht times) a different machine language output.

  5. #5
    Registered User
    Join Date
    Mar 2006
    fnoyan is right. How well do you know your machine? I mean... pipeline sizes, prefetching parameters, cache speed, swap... all these affect program speed. Most half-decent compilers will at least know something about the platform they are written for, and can optimize code for those platforms. Just write straight code, and let the compiler(s) do the speedup for you.
    #include <stdio.h>
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. I am very new . . . :(
    By Eternalglory47 in forum C++ Programming
    Replies: 6
    Last Post: 09-05-2008, 11:29 AM
  3. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM
  4. Results for the Encryption Contest -- June 23, 2002
    By ygfperson in forum A Brief History of
    Replies: 18
    Last Post: 07-07-2002, 08:04 AM
  5. Function Speed?
    By -leech- in forum A Brief History of
    Replies: 6
    Last Post: 01-18-2002, 10:57 AM