On Mon, Aug 31, 2009 at 2:20 PM, Antoine Pitrou <solip...@pitrou.net> wrote:
> Gregory P. Smith <greg <at> krypto.org> writes: > > > > food for thought as noticed by a coworker who has been profiling some hot > code > to optimize a library...If a function does not have a return statement we > return > None. Ironically this makes the foo2 function below faster than the bar2 > function at least as measured using bytecode size > > I would be surprised if this "bytecode size" difference made a significant > difference in runtimes, given that function call cost should dwarf the > cumulated > cost of POP_TOP and LOAD_CONST (two of the simplest opcodes you could > find). > > the attached sample code repeatably shows that it makes a difference though its really not much of one (2-3%). I was just wondering if a bytecode for a superinstruction of the common sequence: 6 POP_TOP 7 LOAD_CONST 0 (None) 10 RETURN_VALUE might be worth it.
#!/usr/bin/python2.4 import dis import time def returns_none(x): x() def returns_call(x): return x() def dummy(): pass print 'returns_none:' dis.dis(returns_none) start, start_clock = time.time(), time.clock() for _ in xrange(10000000): returns_none(dummy) stop, stop_clock = time.time(), time.clock() print 'elapsed time', stop-start, stop_clock-start_clock print 'returns_call:' dis.dis(returns_call) start, start_clock = time.time(), time.clock() for _ in xrange(10000000): returns_call(dummy) stop, stop_clock = time.time(), time.clock() print 'elapsed time', stop-start, stop_clock-start_clock
_______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com