Program speed and function calls

This is a discussion on Program speed and function calls within the C Programming forums, part of the General Programming Boards category; I'm writing a little program. Right now most of my code is all inline with the main() function. It pretty ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    197

    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
    Location
    Toronto, Ontario, Canada
    Posts
    625
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,668
    > 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.
    Code:
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    269
    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
    Posts
    725
    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.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /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, 12:18 PM
  4. Results for the Encryption Contest -- June 23, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 07-07-2002, 08:04 AM
  5. Function Speed?
    By -leech- in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 01-18-2002, 09:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21