Re:No-op statements syntactically valid only since Python X.Y
by bvrmn on Monday June 06, 2022 @03:22AM
(#31638012)
What's wrong with printing to stderr in python?
[ Parent
| on HN ]
Re:No-op statements syntactically valid only since Python X.Y
by eesmith on Monday June 06, 2022 @03:44AM
(#31638121)
Agreed!
In fact, I think this makes it so all print() calls by default write to stderr:
>>> import sys, functools, builtins
>>> real_print = print
>>> builtins.print = functools.partial(real_print, file=sys.stderr)
>>> print("Hello")
Hello
Re:No-op statements syntactically valid only since Python X.Y
by abdusco on Monday June 06, 2022 @06:05AM
(#31638703)
Or use logging module as you should, which prints to stderr by default.
import logging
logging.basicConfig()
logging.info('something happened')
Re:No-op statements syntactically valid only since Python X.Y
by prirun on Monday June 06, 2022 @10:01AM
(#31640596)
The problem for me with logging is that it's slow for production code:
Python 2.7.15 Linux (logging is 60x slower):
[root@hbtest ~]# py -m timeit -s 'import logging' "logging.info('something happened')"
1000000 loops, best of 3: 1.31 usec per loop
[root@hbtest ~]# py -m timeit -s 'import sys; debug = False' "if debug: print >>sys.stderr, 'something happened'"
10000000 loops, best of 3: 0.0216 usec per loop
Python 2.7.15 OSX (logging is 71x slower): $ py -m timeit -s 'import logging' "logging.info('something happened')"
1000000 loops, best of 3: 0.925 usec per loop
[jim@mbp hbrel]$ py -m timeit -s 'import sys; debug = False' "if debug: print >>sys.stderr, 'something happened'"
100000000 loops, best of 3: 0.013 usec per loop
Python 3.6.8 Linux (logging is 99x slower): [root@hbtest ~]# python3 -m timeit -s 'import logging' "logging.info('something happened')"
1000000 loops, best of 3: 1.62 usec per loop
[root@hbtest ~]# python3 -m timeit -s 'import sys; debug = False' "if debug: print >>sys.stderr, 'something happened'"
100000000 loops, best of 3: 0.0163 usec per loop
And, as usual, Python 3 is 25% slower than Python 2 for the logging case (but 2x faster for the noop if debug case). I hope the recent efforts to increase Python 3 performance are successful.
Re:No-op statements syntactically valid only since Python X.Y
by mixmastamyk on Monday June 06, 2022 @10:45AM
(#31641148)
Microseconds are not important unless in a very tight loop. Avoid there and/or make a shortcut beforehand.