Thread: Need your opinion

  1. #1
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Thumbs up Need your opinion

    Which code is more efficient, the first one or the second one?

    1) this code draws, "one by one", an ASCII graphical element on the screen
    Code:
    short i = 0;
    
    for (i = 0; i < 60; i++)
    {
        printf ("%c", 196);
    }
    2) this code draws, "everything at once", an ASCII graphical element on the screen
    Code:
    char Lines[65];
    short i = 0;
    
    for (i = 0; i < 60; i++)
    {
        Lines[i] = 196;
    }
    Lines[i] = '\0';
    printf ("%s", Lines);
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    I would think the second one is better because there is only one printf function call. The first one has to call the function 60 times. Every time you call a function there is a small bit of overhead involved in pushing all the function parameters onto the stack and so forth. Calling a function once is therefore better than 60 times.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Unregistered
    Guest
    The second one may be more efficient, but the first one is more readable, so I'd go with it. Any more opinions?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here's a better method:

    char lines[65] = {0};
    memset( lines, 196, 60 );
    printf( "%s", lines );

    Efficient and readable.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    That is really a better method. Thanks quzah.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Her opinion, your opinion
    By RoD in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2002, 10:50 AM
  2. A matter of opinion
    By pepperblue255 in forum C++ Programming
    Replies: 2
    Last Post: 10-05-2002, 09:51 PM
  3. Freedom of opinion
    By Shiro in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 02-10-2002, 07:06 AM
  4. opinion about books
    By clement in forum C Programming
    Replies: 7
    Last Post: 09-24-2001, 04:18 PM