<div dir="ltr">I'm trying to profile it adding this code:<div><br></div><div><div>import cProfile</div><div>import re</div><div>import pstats</div><div><br></div><div>cProfile.run('re.compile("foo|bar")', 'restats')</div>
<div><br></div><div>p = pstats.Stats('restats')<br></div><div>p.strip_dirs().sort_stats('name')</div><div>p.sort_stats('time').print_stats(10)</div><div><br></div></div><div>but where I have to add this in my code?</div>
<div><br></div><div>because I obtain </div><div><br></div><div>Thu Apr 10 15:23:17 2014    restats</div><div><br></div><div>         194 function calls (189 primitive calls) in 0.001 seconds</div><div><br></div><div>   Ordered by: internal time</div>
<div>   List reduced from 34 to 10 due to restriction <10></div><div><br></div><div>   ncalls  tottime  percall  cumtime  percall filename:lineno(function)</div><div>      3/1    0.000    0.000    0.000    0.000 sre_compile.py:33(_compile)</div>
<div>        1    0.000    0.000    0.000    0.000 sre_compile.py:208(_optimize_chars</div><div>et)</div><div>      3/1    0.000    0.000    0.000    0.000 sre_parse.py:141(getwidth)</div><div>        1    0.000    0.000    0.000    0.000 sre_compile.py:362(_compile_info)</div>
<div>        2    0.000    0.000    0.000    0.000 sre_parse.py:380(_parse)</div><div>        1    0.000    0.000    0.000    0.000 sre_parse.py:302(_parse_sub)</div><div>        1    0.000    0.000    0.001    0.001 re.py:226(_compile)</div>
<div>       10    0.000    0.000    0.000    0.000 sre_parse.py:183(__next)</div><div>        1    0.000    0.000    0.001    0.001 sre_compile.py:496(compile)</div><div>       15    0.000    0.000    0.000    0.000 {isinstance}</div>
<div><br></div><div>but my program take more than 0.001 seconds! </div><div>I think it's not working as I want.</div><div><br></div><div>Gabriele</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">2014-04-10 15:09 GMT-04:00 Danny Yoo <span dir="ltr"><<a href="mailto:dyoo@hashcollision.org" target="_blank">dyoo@hashcollision.org</a>></span>:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Gabriele,<br>
<br>
Have you profiled your program?  Please look at:<br>
<br>
    <a href="https://docs.python.org/2/library/profile.html" target="_blank">https://docs.python.org/2/library/profile.html</a><br>
<br>
If you can, avoid guessing what is causing performance to drop.<br>
Rather, use the tools in the profiling libraries to perform<br>
measurements.<br>
<br>
<br>
It may be that your program is taking a long time because of something<br>
obvious, but perhaps there is some other factor that's contributing.<br>
Please do this.  More details would be helpful.  Are you using any<br>
libraries such as Numpy?  Just writing something in C doesn't<br>
magically make it go faster.  CPython is written in C, for example,<br>
and yet people do not say that Python itself is very fast.  :P<br>
<br>
It may be the case that writing the computations in C will allow you<br>
to specify enough type information so that the computer can<br>
effectively run your computations quickly.  But if you're using<br>
libraries like Numpy to do vector parallel operations, I would not be<br>
surprised if that would outperform native non-parallel C code.<br>
<br>
See:<br>
<br>
    <a href="http://technicaldiscovery.blogspot.com/2011/06/speeding-up-python-numpy-cython-and.html" target="_blank">http://technicaldiscovery.blogspot.com/2011/06/speeding-up-python-numpy-cython-and.html</a><br>
<br>
which demonstrates that effective use of NumPy may speed up<br>
computations by a significant order of magnitude, if you use the<br>
library to take advantage of its vectorizing compuations.<br>
<br>
<br>
More domain-specific details may help folks on the tutor list to give<br>
good advice here.<br>
</blockquote></div><br></div>