An idiom for code generation with exec

eliben eliben at gmail.com
Sat Jun 21 01:28:44 EDT 2008


> So you are saying that for example "if do_reverse: data.reverse()" is
> *much* slower than "data.reverse()" ? I would expect that checking the
> truthness of a boolean would be negligible compared to the reverse
> itself. Did you try converting all checks to identity comparisons with
> None ? I mean replacing every "if compile_time_condition:" in a loop
> with
>
> compile_time_condition = compile_time_condition or None
> for i in some_loop:
>     if compile_time_condition is None:
>         ...
>
> It's hard to believe that the overhead of identity checks is
> comparable (let alone much higher) to the body of the loop for
> anything more complex than "pass".
>

There are also dict accesses (to extract the format parameters, such
as length and offsets) to the format, which are absent. Besides, the
fields are usually small, so reverse is relatively cheap.

Eli





More information about the Python-list mailing list