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%

program size vs program speed

This is a discussion on program size vs program speed within the A Brief History of Cprogramming.com forums, part of the Community Boards category; so you're implementing it's better not to use the extra variable in the for loop and instead go with type ...

  1. #16
    Registered User toaster's Avatar
    Join Date
    Apr 2002
    Posts
    161
    so you're implementing it's better not to use the extra variable in the for loop and instead go with type 1?

    I've always wondered if we can use chars instead of ints in for loops since chars use less space than ints. Since the use of type casting is now supported by many compilers, I don't think that should matter anymore. Maybe some old Borland Turbo C++ compiler might support the short and tiny pointers and what not.
    Funny thing is there isn't bool in thay old compiler. instead we have to use an int or char or enumerator (I don't prefer this) , etcetra to substitute this.

    oskilian, where did you get that comparative info from or is it just your compiler?
    think only with code.
    write only with source.

  2. #17
    Peace
    Join Date
    Aug 2001
    Posts
    1,510
    >>reuse variables, clean them as less often as you can, always think about code complexity

    reuse... It just seems to canabalistic, doesnt it? And it makes the flow absolutly horrible. I occassionally reuse vars for vital sections of code but in general I sacrifice the benefit for the benefit of being able to understand wtf is going on one month later.


    >>I've always wondered if we can use chars instead of ints in for loops

    Yes. Theres no difference between a char and a short besides the size.
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  3. #18
    Unregistered
    Guest
    Originally posted by lightatdawn
    >>reuse variables, clean them as less often as you can, always think about code complexity

    reuse... It just seems to canabalistic, doesnt it? And it makes the flow absolutly horrible. I occassionally reuse vars for vital sections of code but in general I sacrifice the benefit for the benefit of being able to understand wtf is going on one month later.


    >>I've always wondered if we can use chars instead of ints in for loops

    Yes. Theres no difference between a char and a short besides the size.
    yes, but how do I go by doing that?

    Code:
    char c;
    for( ? )
    {
     //code
    }
    say I want to use indexing in a for loop using a char instead of a short: since a char obviously requires less memory versus an unsigned short int, wouldn't it better to use a char instead if we are only staying with the range 0 <= c <= 255? otherwise, I would stay with ints.

    as for the above sample outline, how would you add such use with chars? if it works, wouldn't the memory size for the char be extended for the use of type casting the value back to int meaning the memory size of the char and int would be the same? if that is true, all this would be a waste of time.

    Code:
    // is this how you use chars for indexing in a for loop
    char c;
    for( c=char(0); c<char(10); c++)
    {
     cout<<int(c);
    }

  4. #19
    Peace
    Join Date
    Aug 2001
    Posts
    1,510
    >>as for the above sample outline, how would you add such use with chars?

    Exactly the same way as with shorts or any other type.

    >>meaning the memory size of the char and int would be the same?

    AFAIK on most new compilers, yes.

    >>if that is true, all this would be a waste of time.

    Pretty much, yes. As a matter of fact it could actually be detrimental since using a 32-bit type will actually be faster than a low-bit padded one.

    >>for( c=char(0); c<char(10); c++)

    Why are you doing this? A char is no different in use than a short. You dont need to implicitly cast your value to type char. This is just fine:

    for(char c = 0; c < 10; c ++)
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  5. #20
    Nerd Xmevs's Avatar
    Join Date
    Apr 2002
    Posts
    146
    I hardly ever care about speed... but I care about size.

    Whether I use #1 or #2 depends if there are many entries.

Page 2 of 2 FirstFirst 12
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, 10: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, 04:13 PM
  4. Replies: 5
    Last Post: 09-03-2001, 09:45 PM

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