4 ms·
What's wrong with printing to stderr in python?
by bvrmn 4y ago
What's wrong with printing to stderr in python?
- eesmith 4y agoAgreed! 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
- abdusco 4y agoOr use logging module as you should, which prints to stderr by default. import logging logging.basicConfig() logging.info('something happened')
- prirun 4y agoThe 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.
- mixmastamyk 4y agoMicroseconds are not important unless in a very tight loop. Avoid there and/or make a shortcut beforehand.
- eesmith 4y agoAnd we don't know why tus666 wants to print to stderr, nor what the issue was with Python, so this digression seems built on sand.