2012년 8월 3일 금요일

python 에서 namedtuple 이나 __slots__ 를 썼을때 얼마나 메모리 절약하나

new style class 에 대한 문서를 읽을때 __slots__ 로 메모리를 절약할수 있다고 하길래 가능하면 이걸 쓰고 있었는데 얼마나 효과가 있나 돌려봤다. 사정상 요즘 자주 쓰는 namedtuple 도 같이 비교해봤음.

테스트 환경


/tmp/x $ uname -a
Linux nude 3.2.0-27-generic #43-Ubuntu SMP Fri Jul 6 14:25:57 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
/tmp/x $ python -V
Python 2.7.3
/tmp/x $ 


소스


# -*- coding: utf-8 -*-
import sys
import os
import subprocess
import collections

def mem():
    p = subprocess.Popen(["ps", "-p", str(os.getpid()), "-o", "rss"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    print "rss:", (p.stdout.read().split("\n")[1])

class OldStyleClass:
    def __init__(self,a,b,c,d,e,f):
        self.a,self.b,self.c,self.d,self.e,self.f = a,b,c,d,e,f

NamedTuple = collections.namedtuple("NamedTuple", "a b c d e f")

class NewStyleClassWithSlots(object):
    __slots__ = "a b c d e f".split()
    def __init__(self,a,b,c,d,e,f):
        self.a,self.b,self.c,self.d,self.e,self.f = a,b,c,d,e,f

class NewStyleClass(object):
    def __init__(self,a,b,c,d,e,f):
        self.a,self.b,self.c,self.d,self.e,self.f = a,b,c,d,e,f

def test(cnt,cls):
    print cls
    t = eval(cls)
    l = [t(i,i,i,i,i,i) for i in xrange(cnt)]
    mem()

if __name__ == "__main__":
    test(int(sys.argv[1]), sys.argv[2])


결과

/tmp/x $ time python a.py 1000000 OldStyleClass
OldStyleClass
rss: 1157332
1.984 secs
/tmp/x $ time python a.py 1000000 NamedTuple
NamedTuple
rss: 150764
1.139 secs
/tmp/x $ time python a.py 1000000 NewStyleClassWithSlots
NewStyleClassWithSlots
rss: 134424
1.217 secs
/tmp/x $ time python a.py 1000000 NewStyleClass
NewStyleClass
rss: 1149272
2.083 secs
/tmp/x $ 


추가, 걍 tuple


/tmp/x $ time python a.py 1000000 "lambda *x: x"
lambda *x: x
rss: 144848
0.343 secs





2012년 7월 25일 수요일

firefox 버리고 chrome 으로

firefox 가 언제부턴가 업그레이드때마다 손이 가더니 이번에 14.0.1 에서 xmark 가 엉망으로 돌길래 그냥 chrome 으로 갈아탐... 파폭정말 오래썼는데 좀 아쉽다. 그리고 파폭 15 에서는 애드온에서 메모리 새는거 잡아줄거란 말이 있길래 더 아쉬움.

어쨌건 크롬으로 갈아타고 나니까..

  1. 기본 폰트 바꾸는게 짱난다. custom.css 편집까지 해야 했음.
  2. 애드온이 파폭에비에 많이 구림.
  3. 리눅스에서 정상적으로 돌아가는 마우스 제스쳐를 아직 못찾음.. 이게 제일 치명적. 스무스제스쳐라고 리눅스 지원하는 놈이 있었던 모양인데 malware 판정맞고 내려간 상황. 수정된 유저패치버전이 있긴 한데 깔아보진 않았음.
  4. 속도는 매우 빠름. 특히 뜨는 속도. 파폭은 프로세스 뜨는게 버그 수준으로 느리던데.. 물론 애드온 문제였겠지만.
  5. 구글계정으로 북마크 등이 싱크되는건 정말 편함. foxmark 시절 잘쓰던 xmark 가 요즘 자꾸 말썽이던데 크롬으로 바꾸고 제일 만족한 부분

그런데 제스쳐의 허접함이 너무 걸려서 좀 고민되네. 자주 쓰던 이미지 바로 다운받기, 링크들 한번에 열기, 한번에 저장하기 등등 죄다 아직 설정 못한 상태.



2012년 7월 4일 수요일

Gow (Gnu On Windows) is the lightweight alternative to Cygwin.

https://github.com/bmatzelle/gow/wiki/


좋아보인다.
다음번에 윈도머신 세팅할일 있으면 gnuwin 말고 이거 한번 떠올려보자.

2012년 2월 16일 목요일

Youporn.com is now a 100% Redis Site

이 요약은 사용할 수 없습니다. 이 글을 보려면 여기를 클릭하세요.

2012년 2월 15일 수요일

mercurial phases

http://mercurial.selenic.com/wiki/Phases

이전에 쓴 nudge alias 보단 이게 더 쓰기 좋은듯.
이거 응용한 local branch 류 확장도 곧 나오지 않을까?

2012년 2월 14일 화요일

Git performance results on a large repository

http://thread.gmane.org/gmane.comp.version-control.git/189776

페이스북 리파지토리 쩐다.
난 아마 저런규모 소스트리를 볼날이 없겠지....

2012년 1월 26일 목요일

mercurial 현재 브랜치만 push 하기

http://hgtip.com/tips/advanced/2009-09-28-nudge-a-gentler-push/

우왕. git 부럽지않네. mercurial 관련 최고의 팁인듯.