How to determine if printing is being a bottleneck in my code?

Discussion in 'Python' started by SherjilOzair, Dec 5, 2012.

  1. SherjilOzair

    SherjilOzair Guest

    Hello list,

    When it comes to printing things while some computation is being done, there are 2 extremes.

    1. printing speed is slower than data-to-print generation speed. In this case, printing is a bottleneck. Examples: "for i in xrange(2**30): print i". Without the print, this code would be much faster.

    2. data-to-print generation speed is slower than printing speed. So, this case, printing does now slow you down much. Example: for m in matrices: print m.inverse() # inverse is a time-taking function

    These two cases are pretty easy. But, my question is, how to draw the line? How do I know that print is slowing me down, and I should probably remove some of them? Is there a scientific way to do this, rather than just intuition and guesswork?

    I can clarify, if needed.
    Thanks,
    Sherjil Ozair
     
    SherjilOzair, Dec 5, 2012
    #1
    1. Advertising

  2. SherjilOzair

    Roy Smith Guest

    In article <>,
    SherjilOzair <> wrote:

    > Hello list,
    >
    > When it comes to printing things while some computation is being done, there
    > are 2 extremes.
    >
    > 1. printing speed is slower than data-to-print generation speed. In this
    > case, printing is a bottleneck. Examples: "for i in xrange(2**30): print i".
    > Without the print, this code would be much faster.
    >
    > 2. data-to-print generation speed is slower than printing speed. So, this
    > case, printing does now slow you down much. Example: for m in matrices: print
    > m.inverse() # inverse is a time-taking function
    >
    > These two cases are pretty easy. But, my question is, how to draw the line?
    > How do I know that print is slowing me down, and I should probably remove
    > some of them? Is there a scientific way to do this, rather than just
    > intuition and guesswork?
    >
    > I can clarify, if needed.
    > Thanks,
    > Sherjil Ozair


    The profiler (http://docs.python.org/2/library/profile.html) is your
    friend.
     
    Roy Smith, Dec 5, 2012
    #2
    1. Advertising

  3. SherjilOzair

    rusi Guest

    On Dec 5, 7:36 am, Roy Smith <> wrote:
    > In article <>,
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >  SherjilOzair <> wrote:
    > > Hello list,

    >
    > > When it comes to printing things while some computation is being done, there
    > > are 2 extremes.

    >
    > > 1. printing speed is slower than data-to-print generation speed. In this
    > > case, printing is a bottleneck. Examples: "for i in xrange(2**30): print i".
    > > Without the print, this code would be much faster.

    >
    > > 2. data-to-print generation speed is slower than printing speed. So, this
    > > case, printing does now slow you down much. Example: for m in matrices:print
    > > m.inverse() # inverse is a time-taking function

    >
    > > These two cases are pretty easy. But, my question is, how to draw the line?
    > > How do I know that print is slowing me down, and I should probably remove
    > > some of them? Is there a scientific way to do this, rather than just
    > > intuition and guesswork?

    >
    > > I can clarify, if needed.
    > > Thanks,
    > > Sherjil Ozair

    >
    > The profiler (http://docs.python.org/2/library/profile.html) is your
    > friend.


    One added caveat: When 'printing' is the bottleneck it can be
    - in the actual disk/console/network I/O
    - in the str/repr builtin/customized that precedes it

    [Or both of course]

    So after you identify printing as the bottleneck, it may be worthwhile
    to print to a dummy device.
    On Unix one usually uses /dev/null for this but whether that will help
    clarify or muddy the picture I am not sure (Whats the overhead to
    writing to null?)
    A more internal-to-python method may be to replace the print with a
    str/repr assigned to say a global variable
     
    rusi, Dec 5, 2012
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Glenn

    Performance Bottleneck in ASP.NET

    Glenn, Jan 8, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    580
    Glenn.
    Jan 8, 2004
  2. =?Utf-8?B?bWF0dmRs?=

    Access to remote application and bottleneck

    =?Utf-8?B?bWF0dmRs?=, Jul 10, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    355
    =?Utf-8?B?bWF0dmRs?=
    Jul 10, 2005
  3. David Zimmerman
    Replies:
    1
    Views:
    2,125
    Harald Kirsch
    Jul 22, 2003
  4. Roedy Green
    Replies:
    6
    Views:
    3,302
    Steve Horsley
    Jul 23, 2003
  5. JLM
    Replies:
    2
    Views:
    1,477
Loading...

Share This Page