2018.10.18

python27 安装pip

进入python27/Scripts目录,打开命令行,输入easy_install.exe pip就可以安装pip

python对象内存分析

今天网上找了很多资料,关于分析python对象占用内存的情况。python 有个自带的方法

import sys
sys.getsizeof(obj)

getsizeof方法可以获取对象的内存使用情况,不过它只能计算python自带类型的内存占用,如
int long str list tuple dict...
对于非自带类型就没有办法了,而且对于dict内嵌list tuple dict使用getsizeof计算
的值也是不准确的。这里上网找到了一套方案来解决内嵌的问题

from sys import getsizeof
from itertools import chain
from collections import deque

def GetTotalSize(data, handlers={}, verbose=False):
  dctHandler = lambda d: chain.from_iterable(d.items())
  allHandler = {
    tuple: iter,
    list: iter,
    deque: iter,
    dict: iter,
    set: iter,
    frozenset: iter,
  }

  allHandler.update(handlers)     
  seen = set()                     
  default_size = getsizeof(0)      
  def sizeof(obj):
      if id(obj) in seen:
          return 0
      seen.add(id(obj))
      iSize = getsizeof(obj, default_size)

      if verbose:
          print(iSize, type(obj), repr(obj), file=stderr)

      for typ, handler in allHandler.items():
          if isinstance(data, typ):
              iSize += sum(map(sizeof, handler(obj)))
              break
      return iSize

    return sizeof(data)

def ReprSize(iSize):
  if iSize <= 1 << 12:
    return "%d B" % iSize
  if iSize > 1 << 12 and iSize <= 1 << 15:
    return "%.2f KB" % (iSize / 1024.0)
  if iSize > 1 << 15 and iSize <= 1 << 31:
    return ".2f MB" % (iSize / (1024 * 1024.0))
  if iSize > 1 << 31 and iSize <= 1 << 60:
    return ".2f GB" % (iSize / (1024 * 1024 * 1024.0))
  return ".2f TB" % (iSize / (1024 * 1024* 1024 * 1024.0))

不过这只能算dict list tuple的,对于具体的其它对象就没办法了,后来询问了其他同事,
他们有一个统计当前内存使用的脚本,可以查看当前内存所有对象的内存占用情况,还可以可视化,
看了下代码,大概是根据gc获取所有对象,然后使用getsizeof来计算所占用内存大小。
具体在等明天细细琢磨…

美文学习

It is impossible to always enjoy good days, but with bad days as our companions,we can be blessed with the brilliant light of the happy time.

不可能天天都是好日子,有了不顺心的日子,好日子才会闪闪发亮。

Having read lots of enlightening books, I usually take an objective view on myself , hence a different “me” will appear .

书读多了,就会客观地看待自己,就会出现另一个“我”。

Life is about detouring .
But I have no idea when I began to go ahead on a roundabout route .
Since I take o detour ,something will be left out there without any attention .
However , nothing can be happier like this .
Perhaps detouring can be truly called life .

人生即绕路
我不知道从哪里开始算绕路
因为绕路,所以途中耽搁
没有比途中耽搁更快乐的事
也许 绕路才是货真价实的人生

Since these books are worthwhile to read ,they should be sold at a lower price .But this is not a clearance sale ,all I want is to see people can enjoy the pleasure of reading .

因为书是好书,更要便宜卖,不是甩卖,只是想让更多人体会读书之乐

My love ,you were once my only supporter in the world ,no matter what happened ,you always spared no efforts to share my burdens.

我的妻,在这世上你曾是我唯一的支持者,不论好事坏事,你都全力为我分担。

Life is an accumulation of every irreplaceable days .

活着就是一个个无可替代的日子的累积

I have to think seriously about life ,to recall what I have done before ,to think about what I am doing right now ,and to expect what I can do in the future .

我必须要好好想想的是活着这件事,自己从前做了什么,现在正在做什么,今后想要做什么

It has become common for me to lose or forget something everyday ,Though older and weaker ,at least I still try to make my way at every dusk.

每天我都会遗失或忘记些什么,但至少这个黄昏我仍在蹒跚而行

People who can reflect on themselves mostly are honest and humble .It is the modesty that highlights their inner quality and charm .

能够自我反省的人,大多坦诚又谦虚。那种谦虚会使人显得有内涵,散发出魅力

Before my life comes to an end .I am still eager to read ,to learn and to imparted .

在生命终结之前,我还想读书,想学习,想受教于人

I was born poor ,I have no shortcuts but to experience every hardship and endure all the pains for doing what I want to do ,and finally ,thanks to my perseverance I made it .

我生来贫困,为了想做的事总是忍了又忍,就这样一路咬牙坚持下来,最后居然把这些事都做成了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值