View Poll Results: which one do you prefer or is better?

Voters
14. You may not vote on this poll
  • type 1

    3 21.43%
  • type 2

    11 78.57%

Thread: program size vs program speed

  1. #1
    Registered User toaster's Avatar
    Join Date
    Apr 2002
    Posts
    161

    program size vs program speed

    which one do you prefer or think is better?

    type 1:

    Code:
    int main()
    {
     cout << "this is line 1" << endl;
     cout << "this is line 2" << endl;
     cout << "this is line 3" << endl;
     cout << "this is line 4" << endl;
    }
    type 2:

    Code:
    void function(short unsigned int x)
    {
     cout<<"this is line " << x << endl;
    }
    int main()
    {
     function ( 1 );
     function ( 2 );
     function ( 3 );
     function ( 4 );
    }
    think only with code.
    write only with source.

  2. #2
    back? dbaryl's Avatar
    Join Date
    Oct 2001
    Posts
    597
    I prefer code 1 for this kind of thing... nice, short and simple. However, if it gets into some task that is WAAAAY repetitive, it helps to have a function do it for you. I don't think that's the case here though.

    As to which is better, I have no idea, ask someone who knows
    This is my signature. Remind me to change it.

  3. #3
    Registered User toaster's Avatar
    Join Date
    Apr 2002
    Posts
    161
    I don't know either but I think type 1 does it faster rather than type 2
    think only with code.
    write only with source.

  4. #4
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    unless you inline the function, where you'll sacrifice size for speed but can still keep your code neat

  5. #5
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Code:
    for (int i = 1; i <= 4 i++) {
       cout << "This is line " << i << endl;
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  6. #6
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    for (int i = 1; i <= 4 i++) {
    cout << "This is line " << i << endl;
    }
    SOLD!
    I prefer "other".
    The world is waiting. I must leave you now.

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    44
    Size and speed are not mutually exclusive. I've seen many things which 'should' have been faster by conventional rules when unrolled which, in practice, were not.

    On processors with branch prediction, multi-level caching, microcoding, slightly parallelising execution units and a myriad of potential optimisations that the compiler might do on your behalf... who can say which one will be faster except by compiling it. Even then, there is no guarentee that it will be faster when the next compiler version comes out, or on slightly different hardware or even with different compiler options.

    In reality, such micro-optimisation is rarely necessary, and when it is you should have already exhausted better optimisation techniques. Is that <1% performance increase really worth it compared with using a profiler to find critical points and using better algorithms and data structures to get you 50 or 60% increases in performance?

    Ian

  8. #8
    Registered User Aran's Avatar
    Join Date
    Aug 2001
    Posts
    1,301
    i would never use either of those.

    I would use a for loop.

  9. #9
    Señor Member
    Join Date
    Jan 2002
    Posts
    560
    I would never make any of those programs!

  10. #10
    back? dbaryl's Avatar
    Join Date
    Oct 2001
    Posts
    597
    Ummmmm... I believe the question was which of the 2 given you liked best, not that you liked any... oh well.
    This is my signature. Remind me to change it.

  11. #11
    Registered User toaster's Avatar
    Join Date
    Apr 2002
    Posts
    161
    the 2nd type should be similar to the for loop someone suggested.
    it's just that type 2 is expanded out.
    the reason why I came up with this decision was based on my old source I looked up.

    here's a sample of the source using both techniques:

    Code:
    //type 1
    ...
    cout<<"statement 1";
    function(variable_1);
    cout<<"statement 2";
    function(variable_2);
    cout<<"statement 3";
    function(variable_3);
    cout<<"statement 4";
    function(variable_4);
    ...
    versus

    Code:
    //type 2
    ...
    short unsigned int i;
    for( i=1 ; i<=4 ; i++ )
    {
     cout<<"statement "<<i;
     switch(i)
     {
     case 1: function(variable_1);
     case 2: function(variable_2);
     case 3: function(variable_3);
     case 4: function(variable_4);
     }
    }
    or a slight modification of above:

    Code:
    //alternate type 2
    ...
    void print(int i)
    {
     cout<<"statement"<<i;
    }
    ...
     print(1);
     function(variable_1);
     print(2);
     function(variable_2);
     print(3);
     function(variable_3);
     print(4);
     function(variable_4);
    ...
    Last edited by toaster; 05-18-2002 at 09:56 PM.
    think only with code.
    write only with source.

  12. #12
    Unregistered
    Guest
    you lot are mad, a for loop is the only way to do this. Its just silly to do it any other way (except possibly the 1st way, if you like typing )

  13. #13
    Former Member
    Join Date
    Oct 2001
    Posts
    955
    where I work in, speed is the mother of all the coding... You should always think of reducing the amount of pushes and pops, reuse variables, clean them as less often as you can, always think about code complexity, always have in mind the phrase "one line out of a loop can save your life", and ALWAYS keep your code clean enough so when you compile with -O2, it won't mess up your code.

    oh, by the way: printf is 1.17 times faster as cout... and scanf is 1.22 times faster as cin

    Cheers

    Oskilian

  14. #14
    back? dbaryl's Avatar
    Join Date
    Oct 2001
    Posts
    597
    "one line out of a loop can save your life"

    ???
    This is my signature. Remind me to change it.

  15. #15
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >oh, by the way: printf is 1.17 times faster as cout... and scanf is 1.22 times faster as cin

    Some compilers produce much slower code for cin/cout. However, no matter how much importance you place on speed, you will always be limited to the speed that a human can either type or read.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Error with a vector
    By Tropicalia in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2006, 07:45 PM
  3. Replies: 11
    Last Post: 03-25-2003, 05:13 PM
  4. Replies: 5
    Last Post: 09-03-2001, 09:45 PM