Results 1 to 4 of 4

Thread: Slow output to nul:

  1. #1
    Expert
    Matt Mahoney's Avatar
    Join Date
    May 2008
    Location
    Melbourne, Florida, USA
    Posts
    3,255
    Thanks
    306
    Thanked 778 Times in 485 Posts

    Question Slow output to nul:

    I found some strange behavior when testing output speed. The following program outputs the letter A 500,000 times to a file named as an argument, or to stdout by default.

    Code:
    // t.cpp
    #include <stdio.h>
    int main(int argc, char** argv) {
      FILE* out=stdout;
      if (argc==2) out=fopen(argv[1], "wb");
      for (int i=0; i<500000; ++i) putc('A', out);
      return 0;
    }
    The following commands all run in less than 0.05 seconds:

    Code:
    t x  (write to file x)
    t nul: (discard output)
    t > x (redirect stdout to file x)
    copy x x1
    copy x nul:
    type x > x1
    type x > nul:
    But the following takes 14 seconds.
    Code:
    t > nul:
    I get the same behavior whether I compile with g++ v4.6.1 (g++ -O3 -fomit-frame-pointer, also g++ with no optimization) or Microsoft 16.00.30319.01 (cl /O2 or cl). I'm testing under Vista SP2 32 bit on a 2.0 GHz T3200. I tried varying the output size, and time is proportional. Anyone else see this behavior?

    Edit: same problem in Windows 7 Enterprise 32 bit, Intel Core i7 M620, 2.67 GHz, except it only takes 7 seconds because it's a faster machine.

    Edit: the problem does NOT occur in Windows XP home edition (2002 SP3) 32 bit on a 2.2 GHz Athlon-64 3500+.
    Last edited by Matt Mahoney; 30th December 2011 at 20:46.

  2. #2
    Programmer schnaader's Avatar
    Join Date
    May 2008
    Location
    Hessen, Germany
    Posts
    539
    Thanks
    192
    Thanked 174 Times in 81 Posts
    Same problem here (Windows 7 Professional 64-Bit), 2.4 GHz, 15 seconds. But also note that with this change:

    Code:
      for (int i=0; i<50000; ++i) {
        fprintf(out, "AAAAAAAAAA");
      }
    it takes "only" 3 seconds. So I suppose this is some strange combination of flaws of shell pipes and using putc (instead of buffering), although it still is really strange (especially that it seems to work on XP). On my system, I can even see the speed difference between the original and the modified version when calling the program without any parameters, outputting to screen.
    Last edited by schnaader; 30th December 2011 at 21:33.
    http://schnaader.info
    Damn kids. They're all alike.

  3. #3
    Administrator Shelwien's Avatar
    Join Date
    May 2008
    Location
    Kharkov, Ukraine
    Posts
    3,134
    Thanks
    179
    Thanked 921 Times in 469 Posts
    Its also slower on XP (0.2s instead of 0.02s).
    But adding this:
    Code:
    setvbuf( out , NULL , _IOFBF , 1<<16 );
    Seems to fix it.

  4. #4
    Expert
    Matt Mahoney's Avatar
    Join Date
    May 2008
    Location
    Melbourne, Florida, USA
    Posts
    3,255
    Thanks
    306
    Thanked 778 Times in 485 Posts
    Yes, that works for me too. Thanks.

Similar Threads

  1. Rangecoding with restricted output alphabet
    By Shelwien in forum Data Compression
    Replies: 0
    Last Post: 17th August 2010, 18:38
  2. Slow Visual 2010
    By Cyan in forum The Off-Topic Lounge
    Replies: 23
    Last Post: 24th May 2010, 02:03
  3. Fast arithcoder for compression of LZ77 output
    By Bulat Ziganshin in forum Forum Archive
    Replies: 13
    Last Post: 15th April 2007, 17:40

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •