• 使用memory_profiler异常


    在使用memory_profiler模块0.55.0版本执行命令诊断程序内存用量时,遇到下面错误:

    C:UsersChenDesktoppython_doc第四模块课件>python -m memory_profiler tr1y.py
    Traceback (most recent call last):
      File "C:UsersChenPython36lib
    unpy.py", line 193, in _run_module_as_main
        "__main__", mod_spec)
      File "C:UsersChenPython36lib
    unpy.py", line 85, in _run_code
        exec(code, run_globals)
      File "C:UsersChenPython36libsite-packagesmemory_profiler.py", line 1227, in <module>
        exec_with_profiler(script_filename, prof, args.backend, script_args)
      File "C:UsersChenPython36libsite-packagesmemory_profiler.py", line 1128, in exec_with_profiler
        exec(compile(f.read(), filename, 'exec'), ns, ns)
    UnicodeDecodeError: 'gbk' codec can't decode byte 0x8b in position 331: illegal multibyte sequence

    根据提示我找到memory_profiler模块,查看源码line1128附近,原来在line1127出了问题:

    因为我用的是国产windows系统,默认字符编码是gbk,若无指定打开文件时也会用gbk,但是我要检测的脚本文件使用utf-8字符编码,因此,改成

    with open(filename, encoding='utf-8')就好了,顺利输出诊断信息:

    C:UsersChenDesktoppython_doc第四模块课件>python -m memory_profiler tr1y.py
    (1.800000000000033, -1.800000000000033)
    length of x: 1000
    total elements: 1000000
    Filename: tr1y.py
    
    Line #    Mem usage    Increment   Line Contents
    ================================================
        49  111.680 MiB  111.680 MiB   @profile
        50                             def calculate_z_serial_purepython(max_iter, zs, cs):
        51  119.312 MiB    7.633 MiB       output = [0] * len(zs)
        52  119.336 MiB    0.000 MiB       for i in range(len(zs)):
        53  119.336 MiB    0.008 MiB           n = 0
        54  119.336 MiB    0.012 MiB           z = zs[i]
        55  119.336 MiB    0.000 MiB           c = cs[i]
        56  119.336 MiB    0.004 MiB           while abs(z) < 2 and n < max_iter:
        57  119.320 MiB    0.000 MiB               z = z * z + c
        58  119.320 MiB    0.000 MiB               n += 1
        59  119.336 MiB    0.000 MiB           output[i] = n
        60  119.258 MiB    0.000 MiB       return output

     顺便说一下:从诊断信息来看,在执行line51生成output列表时,可见7MB的RAM被加入这个进程,但这不意味着output列表大小就是7MB,只是进程在列表内部分配时增长了大约7MB。

  • 相关阅读:
    SP1812 LCS2
    SP1811 LCS
    P3804 【模板】后缀自动机
    P3808 【模板】AC自动机(简单版)
    P3879 [TJOI2010]阅读理解
    P2602 [ZJOI2010]数字计数
    P4719 【模板】动态dp
    P1122 最大子树和
    P3554 [POI2013]LUK-Triumphal arch
    P3565 [POI2014]HOT-Hotels
  • 原文地址:https://www.cnblogs.com/tarantino/p/10802567.html
Copyright © 2020-2023  润新知