Printf speed

This is a discussion on Printf speed within the C Programming forums, part of the General Programming Boards category; I am writting a chess program, so speed is a need. Now I have the choice of printf every information ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    145

    Printf speed

    I am writting a chess program, so speed is a need.

    Now I have the choice of printf every information that the program is calculating when is thinking in its turn.

    I can show 100 lines/sec, or 2 lines/sec.
    Of couse, I would prefer 100 lines/sec if performance is not affected.

    Will printf slow down program performance? how much?

    Thanks

  2. #2
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    Obviously, there is a limit to how fast you can write to console. Just add a printf statement and see how much it limits your speed.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The important thing with writing a chess program of your own, is to get it first, up and running. Playing legal chess should be your first goal.

    Understanding what your program is evaluating is critical, of course. Use printf() to show the main line that your program believes will be best for both sides, and it's evaluation of the resulting position.

    Don't bother showing all the possible lines in the search tree, unless you're in debug mode, and that's what you need.

    How much will it slow down your program? Substantially. Still, you need something to refer back to, when your program makes an obvious error. Also, the players using your program, will want to see what the expected line of moves, will be, and it's score.

    You'll want to go here: http://64.68.157.89/forum/
    (www.talkchess.com/)
    and look into the programming & tech discussion, forum.

    Several world champion and very talented amateur chess programmers, visit and contribute.

    If you are using C, looking through some of the open source C chess programs, can be very instructive: Crafty, TSCP, etc.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Duplicate of:
    http://cboard.cprogramming.com/showthread.php?t=108468

    There are replies in both threads, so perhaps a moderator can merge the two.

    --
    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
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    As a reply to the post: printf can seriously slow your application down. How much depends on what your console actually is. A serial or network device may be significantly slower than a text-mode console in Windows, and the fastest is probably a full-screen text mode console in either Windows or Linux. [Actually, the size of the screen may also affect the print speed - I have been known to reduce a big console window to a few lines to improve the throughput of the application when it produces huge amount of console output - that will improve it by a fair amount if the printfs are actually what is slowing it down.

    --
    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.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Just to prove my point, I wrote a test that prints 1000 lines of printf.

    Unfortunately, in my dual screen setup, it appears much slower to scroll in full-screen than it does in Windows itself, so perhaps my last statement about text-only is not always true:
    80 x 25 Window 0.5 seconds
    80 x 50 Window 1.0 seconds
    80 x 71 Window [1]: 2.2 seconds
    80 x 25 full screen: 1.0 second
    80 x 50 full screen: 2.0 seconds.

    [1] With a 4000 line history buffer.

    --
    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.

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > Will printf slow down program performance? how much?
    Yes, lots.

    Since it's chess, I'd say speed is not a need. If you want something faster, then a library like pdcurses or ncurses would be suffice.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by matsp View Post
    Just to prove my point, I wrote a test that prints 1000 lines of printf.

    Unfortunately, in my dual screen setup, it appears much slower to scroll in full-screen than it does in Windows itself, so perhaps my last statement about text-only is not always true:
    80 x 25 Window 0.5 seconds
    80 x 50 Window 1.0 seconds
    80 x 71 Window [1]: 2.2 seconds
    80 x 25 full screen: 1.0 second
    80 x 50 full screen: 2.0 seconds.

    [1] With a 4000 line history buffer.

    --
    Mats
    Were you printing exactly the same thing in all the tests, or were you writing the entire line (which are different sizes in each test)?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Source code for the test:
    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main()
    {
      int i;
      clock_t t;
      t = clock();
      for(i = 0; i < 1000; i++)
        printf("%d abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ\n", i);
      printf("%f\n", (clock() - t) / (double) CLOCKS_PER_SEC);
      return 0;
    }
    --
    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.

  10. #10
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    lol why is speed a need, you're underestimating the speeds of computers today. Lack of experience?
    Anyway if you're really worried about outputting too much console I/O, write your data to a big memory buffer and dumping the whole buffer at once when the program is done dump the buffer.

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Merged.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Writing to a buffer is a good idea.

    As to "is it necessary to make it run fast", and there certainly are cases when this is essential. If you have a set limit for how long you are allowed to think on a chess-move, then you can easily spend half that time printing, which of course makes the system spend half as much time coming up with a good move.

    --
    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
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > I can show 100 lines/sec, or 2 lines/sec.
    Since there's no way for you to read 100 lines/sec, except to go "oooh, look at the speed those lines are being printed", I'd go with 2 per sec.

    If you're more interested in logging output for debug, then write it to a file. FILE I/O is far quicker than screen I/O.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  14. #14
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    You all are discussing about speed etc. and i don't even know how to start writing chess program. From where should i begin! that itself is not clear to me. If anybody can point me to some good article i can be thankful to him. Also in my 6th sem project i can try for a small chess game. It's only a 2 month project. Please suggest me can chess be completed in 2 months? and also how to start writing the program? I am eternally confused..!! Please reply to all my questions

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by chottachatri View Post
    You all are discussing about speed etc. and i don't even know how to start writing chess program. From where should i begin! that itself is not clear to me. If anybody can point me to some good article i can be thankful to him. Also in my 6th sem project i can try for a small chess game. It's only a 2 month project. Please suggest me can chess be completed in 2 months? and also how to start writing the program? I am eternally confused..!! Please reply to all my questions

    Speed is critically important in chess - always has been, and always will be. More look-ahead in the search tree will give your program greater strength.

    Clarity should be your first goal, however. Your program will need to be modified many times, and you must be able to do that easily, without adding more bugs to it.

    You should not start to write a chess program from scratch if you don't know how to even get started.

    A good start is to go to www.talkchess.com/ and start reading. For chess relevant questions, ask them there, for code questions, ask them here or there.

    There are many tutorials for writing a simple chess program (which is quite an oxymoron), on the net. Google away!

    TSCP (version 1.0) has been the standard for a simple chess program for years, but there are even simpler one's available: Simple Chess is one. Both are written in C.

    Be sure you know how to use arrays and structs before you start - otherwise it's just an exercise in frustration. Many chess authors code up a game like Tic-Tac-Toe or Connect 4, or something like that, first.

    IMO no, you can't write a chess program in 2 months, from scratch. Try Connect 4 instead. Chess has a lot of arcane rules like en-passant moves, castling, 50 move rule, etc., that make it much more difficult.
    Last edited by Adak; 10-24-2008 at 04:10 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. making it portable.....?
    By ShadeS_07 in forum C Programming
    Replies: 11
    Last Post: 12-24-2008, 08:38 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  3. segmentation fault upon reload
    By yabud in forum C Programming
    Replies: 8
    Last Post: 12-18-2006, 05:54 AM
  4. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 08:23 AM
  5. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 10:14 AM

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