Thread: How can I improve/shorten my C code?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by awsdert View Post
    If you want to go to extremes for improving then add booleans called isVowel and isDigit, use them like this:
    Code:
    c = tolower(c);
    isDigit = (c >= '0' & c <= '9');
    isVowel = (c == 'a' | c == 'e' | c == 'i' | c == 'o' | c == 'u' );
    vowelCount +=  isVowel;
    if ( isDigit )
      puts("...");
    else if ( !isVowel )
    {
      conCount++;
      // Your loop
    }
    For further extremes do it in ASM and utilise the MUL instruction to set and address to goto when a statement is false to and then use the GOTO instruction I think to goto to that address or 0 (next address), 0 for continue into the "if" block, address for skip the block, it basically is a hack of sorts for removing the slowest part of code, branch instructions (used by if/else if/switch/case statements)
    a) Why are you using bitwise OR?
    b) Parenthesis where they're not required could, arguably, be regarded as clutter
    c) I have no idea what you're talking about regarding the MUL, GOTO is. Further, removing branch instructions?! What for? To remove the slowest part of the code? This is premature optimisation on steroids and would make the code an unreadable and unmaintainable mess. There is no need to avoid branch/eliminate instructions. wow. Obfuscating code by doing stuff like avoiding branches will more than likely get in the way of the compiler's optimiser and maybe (or even likely when you take things to such extremes) result in SLOWER code, not faster

  2. #2
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,734
    Quote Originally Posted by Hodor View Post
    a) Why are you using bitwise OR?
    b) Parenthesis where they're not required could, arguably, be regarded as clutter
    c) I have no idea what you're talking about regarding the MUL, GOTO is. Further, removing branch instructions?! What for? To remove the slowest part of the code? This is premature optimisation on steroids and would make the code an unreadable and unmaintainable mess. There is no need to avoid branch/eliminate instructions. wow. Obfuscating code by doing stuff like avoiding branches will more than likely get in the way of the compiler's optimiser and maybe (or even likely when you take things to such extremes) result in SLOWER code, not faster
    a) so the comiller doesn't try to sneak in branches for the sake of that expression
    b) That's a POV thing, I prefer the brackets
    c) MUL & GOTO are ASM instructions, not C language, I take it you haven't dabbled much if at all in ASM

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by awsdert View Post
    a) so the comiller doesn't try to sneak in branches for the sake of that expression
    b) That's a POV thing, I prefer the brackets
    c) MUL & GOTO are ASM instructions, not C language, I take it you haven't dabbled much if at all in ASM
    a) Using bitwise OR the way you used it there is broken (it won't work... seriously)
    b) Yep, fair enough. I and many others find them annoying
    c) GOTO is not an ASM instruction. I take it that you haven't dabbled in ASM. Even if GOTO *was* an ASM instruction it's a branch

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me shorten this code plz.
    By oblisgr in forum C Programming
    Replies: 3
    Last Post: 07-09-2020, 12:12 AM
  2. HOw can I shorten my code?
    By kdushyant297 in forum C Programming
    Replies: 1
    Last Post: 09-15-2017, 10:40 AM
  3. Making a function to shorten my code?
    By evilcubed in forum C Programming
    Replies: 8
    Last Post: 12-08-2012, 11:46 AM
  4. help to shorten my code.
    By hugoguan in forum C Programming
    Replies: 7
    Last Post: 12-01-2010, 02:19 AM
  5. Need to simplify/shorten this code. Help.
    By Lonck in forum C++ Programming
    Replies: 5
    Last Post: 11-08-2007, 04:23 AM

Tags for this Thread