This makes sense, obviously you should compile it first, which when executed should execute exactly the same as the original context python.<br><br><div class="gmail_quote">On Fri, Aug 14, 2009 at 3:29 PM, ilya <span dir="ltr"><<a href="mailto:ilya.nikokoshev@gmail.com">ilya.nikokoshev@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">That's a useful statistics, but the bottleneck is **only** because of<br>
parsing 'value +=5'.<br>
<br>
Here's how I time it:<br>
<br>
# insert my old program here...<br>
<br>
<br>
from timeit import Timer<br>
from codeop import compile_command<br>
<br>
def timing(something):<br>
setup = 'from __main__ import test, interrogate, command, inc5'<br>
best = sorted(Timer(something, setup).repeat(3, 1000))[0]<br>
print('{0!r} -> {1:.3} ms'.format(something, best))<br>
print('# test.value =', test.value)<br>
<br>
command = '''<br>
for i in range(10):<br>
value += 1<br>
'''<br>
<br>
inc5 = compile_command(command)<br>
<br>
timing("interrogate(test, command)")<br>
timing(command.replace('value', 'test.value'))<br>
timing("interrogate(test, inc5)")<br>
<br>
Result:<br>
<br>
15<br>
'interrogate(test, command)' -> 0.0908 ms<br>
# test.value = 30015<br>
'\nfor i in range(10):\n test.value += 1\n' -> 0.00408 ms<br>
# test.value = 60015<br>
'interrogate(test, inc5)' -> 0.00469 ms<br>
# test.value = 90015<br>
<br>
so interrogate() with additional precompiling introduces very little<br>
overhead. Though I agree it's inconvenient to write functions as<br>
strings; I think someone smarter than me can find a way to do it like<br>
a regular function call.<br>
<div><div></div><div class="h5"><br>
<br>
<br>
On Fri, Aug 14, 2009 at 3:17 PM, Steven D'Aprano<<a href="mailto:steve@pearwood.info">steve@pearwood.info</a>> wrote:<br>
> On Fri, 14 Aug 2009 07:35:55 pm ilya wrote:<br>
>> Sorry, it's actually even easier; interrogate() is a one-liner:<br>
>><br>
>> class Test:<br>
>> '''Class to be interrogated.'''<br>
>> def __init__(self, value):<br>
>> self.value = value<br>
>><br>
>> test = Test(10)<br>
>><br>
>> def interrogate(what, how):<br>
>> exec(how, what.__dict__)<br>
><br>
> Apart from the security implications of exec(), it also takes a fairly<br>
> hefty performance hit. In Python 2.6:<br>
><br>
>>>> from timeit import Timer<br>
>>>> setup = 'from __main__ import test, interrogate'<br>
>>>> Timer("interrogate(test, 'value += 5')", setup).repeat()<br>
> [18.503479957580566, 18.218451023101807, 18.218581914901733]<br>
>>>> Timer("test.value += 5", setup).repeat()<br>
> [0.33056807518005371, 0.33118104934692383, 0.33114814758300781]<br>
><br>
> That's a factor of 55 times slower -- not precisely an optimization.<br>
><br>
><br>
><br>
> --<br>
> Steven D'Aprano<br>
> _______________________________________________<br>
> Python-ideas mailing list<br>
> <a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
> <a href="http://mail.python.org/mailman/listinfo/python-ideas" target="_blank">http://mail.python.org/mailman/listinfo/python-ideas</a><br>
><br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/python-ideas" target="_blank">http://mail.python.org/mailman/listinfo/python-ideas</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>-Prozacgod<br><br>"Prozac may heal the mind, but friends can mend the soul"<br>