Python: Standard Library - Performance Metrics

Performance differences between different approaches to understanding some users are interested in the same problem. Python provides a measurement tool that provides a direct answer to these questions.

For example, using the tuple packing and unpacking to exchange element looks better than using traditional methods to more attractive, timeit modern approach proved faster.

from timeit import Timer
Timer(‘t=a; a=b; b=t’, ‘a=1; b=2’).timeit()
0.57535828626024577

Timer(‘a,b = b,a’, ‘a=1; b=2’).timeit()
0.54962537085770791

Fine-grained timeit respect,: mod: profile pstats module and provides a measure of time for larger blocks of code tools

Guess you like

Origin blog.csdn.net/weixin_44523387/article/details/92164691