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