Problem of Readability of Python
fanglicheng at gmail.com
Wed Oct 10 16:48:17 CEST 2007
On Oct 8, 4:24 am, al... at mac.com (Alex Martelli) wrote:
> Licheng Fang <fanglich... at gmail.com> wrote:
> > Python Tutorial says an empty class can be used to do this. But if
> > namespaces are implemented as dicts, wouldn't it incur much overhead
> > if one defines empty classes as such for some very frequently used
> > data structures of the program?
> Just measure:
> $ python -mtimeit -s'class A(object):pass' -s'a=A()' 'a.zop=23'
> 1000000 loops, best of 3: 0.241 usec per loop
> $ python -mtimeit -s'a=[None]' 'a=23'
> 10000000 loops, best of 3: 0.156 usec per loop
> So, the difference, on my 18-months-old laptop, is about 85 nanoseconds
> per write-access; if you have a million such accesses in a typical run
> of your program, it will slow the program down by about 85 milliseconds.
> Is that "much overhead"? If your program does nothing else except those
> accesses, maybe, but then why are your writing that program AT ALL?-)
> And yes, you CAN save about 1/3 of those 85 nanoseconds by having
> '__slots__=["zop"]' in your class A(object)... but that's the kind of
> thing one normally does only to tiny parts of one's program that have
> been identified by profiling as dramatic bottlenecks, to shave off the
> last few nanoseconds in the very last stages of micro-optimization of a
> program that's ALMOST, but not QUITE, fast enough... knowing about such
> "extreme last-ditch optimization tricks" is of very doubtful value (and
> I think I'm qualified to say that, since I _do_ know many of them...:-).
> There ARE important performance things to know about Python, but those
> worth a few nanoseconds don't matter much.
This is enlightening. Surely I shouldn't have worried too much about
performance before doing some measurement.
More information about the Python-list