Thread: Problem with progress code

  1. #1
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463

    Problem with progress code

    Code:
    while(1) {
          
          c = fgetc(fo);
    
          if(c == EOF){
    
            break;
          }
          else{
            c = c + 'c' + 'd' - 't';
            fputc(c,fw);
            cnt++;
            if(cnt % d == 0) {
              prog++;
            }
            printf(" %d%%\r", prog);
            fflush(stdout);
          }
        }
        printf(" %d%%\n", prog);
        printf("DONE!\n");
    When the lines of code that are in red are removed, the program works very quickly, but when it's kept, it works extremely slowly.

    This is part of a program that encrypts characters from a file and then outputs it into another file. I created this progress meter to show how long it takes when encrypting. When the file size is small, < 5kb, there is no problem. But when it's 3MB, then it takes upto 5 minutes when the progress meter is there, when I remove it, it takes only 5 seconds.

    I don't see what the problem with the code is.
    =========================================
    Everytime you segfault, you murder some part of the world

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    use print only when the new percent value is different from the printed earlier one
    printf with fflush takes a long time - so make it only when really necesary
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    How do I do that ^ ?

    I removed fflush, but that makes no difference.
    =========================================
    Everytime you segfault, you murder some part of the world

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Does your code "know" when progress has changed, by any chance?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116
    I think it has to do with
    Code:
    if(cnt &#37; d == 0) {
    ...
    you're essentially calculating the cnt variable to zero with ==. Try just using =
    hope that helps.

  6. #6
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Quote Originally Posted by MikeyIckey View Post
    I think it has to do with
    Code:
    if(cnt % d == 0) {
    ...
    you're essentially calculating the cnt variable to zero with ==. Try just using =
    hope that helps.

    No, = is an assignment. I'm not trying to assign it, I'm trying to find a remainder with modulus.
    =========================================
    Everytime you segfault, you murder some part of the world

  7. #7
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Quote Originally Posted by matsp View Post
    Does your code "know" when progress has changed, by any chance?

    --
    Mats
    Yea, when cnt % d == 0
    =========================================
    Everytime you segfault, you murder some part of the world

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Code:
    while(1) {
          
    	c = fgetc(fo);
    
    	if (c == EOF){
    		break;
    	}
    	// As a matter of practice, you don't need an else here.
    	else {
    		c = c + 'c' + 'd' - 't';
    		fputc(c,fw);
    		cnt++;
    		if (cnt % d == 0) {
    			prog++;   // indicator changed... therefore...
    			printf(" %d%%\r", prog);    // update indicator only here
    			fflush(stdout);                 
    		}
    	}
    }
    printf(" %d%%\n", prog);
    printf("DONE!\n");
    Mainframe assembler programmer by trade. C coder when I can.

  9. #9
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You could also create another variable to hold this result since it never changes, and there's no need to recalculate it for EVERY STINKING CHARACTER.

    Code:
    int delta = 'c' + 'd' - 't';
    Mainframe assembler programmer by trade. C coder when I can.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by JFonseka View Post
    Yea, when cnt % d == 0
    Right, so my point being that you then only need to print the updated progress when it changes, as Todd posted below - I wanted you to figure that part out yourself.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    I can't believe I didn't see that mistake...

    Thanks
    =========================================
    Everytime you segfault, you murder some part of the world

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Todd Burch View Post
    You could also create another variable to hold this result since it never changes, and there's no need to recalculate it for EVERY STINKING CHARACTER.

    Code:
    int delta = 'c' + 'd' - 't';
    Although the compiler can calculate the sum/difference of a few constants in that simple way. And even if it didn't, it would still disappear in the noise in a profile, compared to fgetc()/fputc() - I'm sure the processor can add/subtract in something like 3 clock-cycles (on a reasonably modern processor - 486 or better), whilst fgetc() is bound to take a few hundred cycles.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    true.
    Mainframe assembler programmer by trade. C coder when I can.

  14. #14
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116
    Quote Originally Posted by JFonseka View Post
    No, = is an assignment. I'm not trying to assign it, I'm trying to find a remainder with modulus.
    sorry I was really exhausted when I posted that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code problem
    By sybariticak47 in forum C++ Programming
    Replies: 9
    Last Post: 02-28-2006, 11:50 AM
  2. Problem with game code.
    By ajdspud in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2006, 06:39 PM
  3. problem with selection code
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 06-14-2004, 01:05 PM
  4. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  5. Help with code for simple Y2K problem
    By Mule in forum C++ Programming
    Replies: 3
    Last Post: 03-06-2003, 12:53 AM