Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to determine if printing is being a bottleneck in my code?

Reply
Thread Tools

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

 
 
SherjilOzair
Guest
Posts: n/a
 
      12-05-2012
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
 
Reply With Quote
 
 
 
 
Roy Smith
Guest
Posts: n/a
 
      12-05-2012
In article <(E-Mail Removed)>,
SherjilOzair <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
rusi
Guest
Posts: n/a
 
      12-05-2012
On Dec 5, 7:36*am, Roy Smith <(E-Mail Removed)> wrote:
> In article <(E-Mail Removed)>,
>
>
>
>
>
>
>
>
>
> *SherjilOzair <(E-Mail Removed)> 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 matricesrint
> > 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Access to remote application and bottleneck =?Utf-8?B?bWF0dmRs?= ASP .Net 0 07-10-2005 06:08 AM
Performance Bottleneck in ASP.NET Glenn ASP .Net 2 01-08-2004 03:04 AM
Re: Why is java.io.FileInputStream.readBytes my performance bottleneck Roedy Green Java 6 07-23-2003 08:37 PM
Re: Why is java.io.FileInputStream.readBytes my performance bottleneck David Zimmerman Java 1 07-22-2003 10:08 AM



Advertisments