A trivial question about print
Brad Bollenbach
bbollenbach at shaw.ca
Thu Apr 11 21:52:57 EDT 2002
In article <Wbit8.8361$vF6.239936 at news2.tin.it>, Alex Martelli wrote:
> Ante Bagaric wrote:
>> Sure in most cases this feature helps, you dont need zillion " " strings
>> between your variables :), but wouldnt it be nice if there was
>> python-simple way to just get around it?
>
> There IS -- it's sys.output.write (or more generally the write method
> of any file object). Does no formatting -- it emits a string, and
> that's all.
Not to mention, sys.stdout.write() is considerably faster than print:
_printspeed.py_
#!/usr/bin/python
for i in range(1000000):
print "hello, world"
_writespeed.py_
#!/usr/bin/python
import sys
faster = sys.stdout.write
for i in range(1000000):
faster("hello, world\n")
$ time ./printspeed.py
[snip 1,000,000 lines of output]
real 1m11.301s
user 0m23.140s
sys 0m6.620s
$ time ./writespeed.py
[snip 1,000,000 lines of output]
real 0m54.578s
user 0m12.250s
sys 0m6.730s
You probably won't care about this unless you working on large amounts
of data, but it's worth keeping in mind.
--
Brad Bollenbach
More information about the Python-list
mailing list