# -*- coding: utf8 -*-
"""
1. 使用 memory_profiler 分析脚本内存使用详情
2. 使用 cProfile 分析脚本时间使用详情
Wed Jan 24 19:55:54 2018 matrix_sum.txt
378806 function calls (378683 primitive calls) in 48.725 seconds
Ordered by: internal time
List reduced from 173 to 10 due to restriction <10>
ncalls tottime percall cumtime percall filename:lineno(function)
1 15.762 15.762 15.762 15.762 {numpy.core.multiarray.array}
1 12.886 12.886 46.854 46.854 memory_profiler.py:656(f)
1 10.572 10.572 33.969 33.969 profile.test.py:16(matrix_sum)
100000 7.172 0.000 7.172 0.000 profile.test.py:10(build_matrix_row)
3600 1.060 0.000 1.258 0.000 _strptime.py:299(_strptime)
200000 0.344 0.000 0.344 0.000 {method 'randint' of 'mtrand.RandomState' objects}
1 0.298 0.298 0.298 0.298 {method 'reduce' of 'numpy.ufunc' objects}
1 0.214 0.214 48.725 48.725 <string>:1(<module>)
1 0.164 0.164 0.164 0.164 {zip}
3602 0.094 0.000 0.097 0.000 locale.py:365(normalize)
Line # Mem usage Increment Line Contents
================================================
16 61.0 MiB 61.0 MiB @profile
17 def matrix_sum(cols_array, duration_tuple_array):
18 matrix = [
19 2833.7 MiB 2772.8 MiB row for row in [build_matrix_row(dt) for dt in duration_tuple_array]
20 ]
21 2834.1 MiB 0.4 MiB return dict(zip(cols_array, list(array(matrix).sum(0))))
Line # Mem usage Increment Line Contents
================================================
23 44.1 MiB 44.1 MiB @profile
24 def main():
25 44.1 MiB 0.0 MiB cProfile.run(
26 44.1 MiB 0.0 MiB "matrix_sum([(datetime.strptime('2018-01-01 12:00:00', '%Y-%m-%d %H:%M:%S')+timedelta(seconds=i)).strftime('%Y-%m-%d %H:%M:%S') for i in range(0, 3600)], [(random.randint(0, 1800), random.randint(1800, 3600)) for i in range(0, 100000)])",
27 82.6 MiB 38.5 MiB 'matrix_sum.txt'
28 )
29 82.6 MiB 0.0 MiB p = pstats.Stats('matrix_sum.txt')
30 82.6 MiB 0.0 MiB p.strip_dirs().sort_stats('time').print_stats(10)
"""
import cProfile
import pstats
import StringIO
import random
from numpy import *
from datetime import datetime,timedelta
from memory_profiler import profile
def build_matrix_row(duration_tupple):
zero_left = int(duration_tupple[0])
one_middle = int(duration_tupple[1]) - int(duration_tupple[0]) + 1
zero_right = int(3600 - duration_tupple[1] - 1)
return [0] * zero_left + [1] * one_middle + [0] * zero_right
@profile
def matrix_sum(cols_array, duration_tuple_array):
matrix = [
row for row in [build_matrix_row(dt) for dt in duration_tuple_array]
]
return dict(zip(cols_array, list(array(matrix).sum(0))))
@profile
def main():
cProfile.run(
"matrix_sum([(datetime.strptime('2018-01-01 12:00:00', '%Y-%m-%d %H:%M:%S')+timedelta(seconds=i)).strftime('%Y-%m-%d %H:%M:%S') for i in range(0, 3600)], [(random.randint(0, 1800), random.randint(1800, 3600)) for i in range(0, 100000)])",
'matrix_sum.txt'
)
p = pstats.Stats('matrix_sum.txt')
p.strip_dirs().sort_stats('time').print_stats(10)
if __name__ == '__main__':
main()
Python 脚本时间、内存分析
最新推荐文章于 2024-05-11 14:23:15 发布