Optimising literals away

Aleksey alekseymv at gmail.com
Tue Aug 31 12:33:49 EDT 2010


On Aug 30, 10:38 pm, Tobias Weber <t... at gmx.net> wrote:
> Hi,
> whenever I type an "object literal" I'm unsure what optimisation will do
> to it.
>
> def m(arg):
>   if arg & set([1,2,3]):
>     return 4
>
> Is the set created every time the method is called? What about a
> frozenset? Or tuple vs list? After how many calls per second does it pay
> to save it at the module level? Would anybody else find this ugly?
>
> Also I never profiled the regular expression cache...
>
> --
>   Tobias Weber

I test time creation of any types ang get next result:

dictionary = 393 000 * 10
frozenset = 267 000 * 10
list = 519 000 * 10
set = 268 000 * 10
tuple = 5 935 500 * 10
global assign = 5 882 700 * 10

All results multiple by 10 becouse i do 10 creations in one loop and
count loops per second.

As you see create global variable is more faster (20 times) then
create list and from it create set! Assigments is ~ 5 882 000*10,>>>
set creation is 268 000*10

My test system is Ubuntu 10.04, Dell Inspiron 1525, Core2Duo, T8300,
2Gb , Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) [GCC 4.4.3].
I make tests with XPyLIB.timetest module. (XPyLIB hosted at
sourceforge - http://sourceforge.net/apps/trac/xpylib/wiki/CookBook/TimeTest)

Assign global (pre declared by "global") function is next (N - is a
times of repeating):

gset = set((1,2,3))
def t_set_assign_global(ntimes = N, funcloop=u'funcloop',
excludecall=u'excludecall'):
    """Set assigment from global : global=(1,2,3); loop a = global 10
times in while.

    @UID@ e710b888-bacd-4248-9ff7-1f7a348e1c8f
    @author@ Mazhugin Aleksey
    @score_common@ 1
    """
    a = 0
    global gset
    while ntimes > 0:
        a = gset
        a = gset
        a = gset
        a = gset
        a = gset
        a = gset
        a = gset
        a = gset
        a = gset
        a = gset
        ntimes -= 1



Set function is next:

def t_set_create(ntimes = N, funcloop=u'funcloop',
excludecall=u'excludecall'):
    """Set creation : t=(1,2,3); loop a = set(t) 10 times in while.

    @UID@ a021a756-f9a5-44ec-b9e6-e5532b56c09f
    @author@ Mazhugin Aleksey
    @score_common@ 1
    """
    a = 0
    t = (1,2,3)
    while ntimes > 0:
        a = set(t)
        a = set(t)
        a = set(t)
        a = set(t)
        a = set(t)
        a = set(t)
        a = set(t)
        a = set(t)
        a = set(t)
        a = set(t)
        ntimes -= 1



Also i test regular expression compiled pattern vs non-compiled:

compiled = 343 000*2
not compiled = 164 000*2

Functions is next:

patt5 = u'*.tmp,*.pyc,*.pyo,*.bak,*.log'
path1 = u'/home/user/project/src/file.ext'
path2 = u'/home/user/project/logs/debug.log'

def t_rematch(ntimes=10, funcloop=u'funcloop',
excludecall='excludecall'):
    """
    Compiled.

    @UID@ 665f4014-9c11-4668-baae-e49230027bd4
    @author@ Mazhugin Aleksey
    @score_common@ 1
    """
    ci = patt5.replace(u'\\',u'\\\\').replace(u'|',u'\
\|').replace(u'.',u'\\.').replace(u'*',u'.*'). \
        replace(u'?',u'.?').replace(u'$',u'\\$').replace(u'^',u'\
\^').replace(u'{',u'\\{'). \
        replace(u'(',u'\\(').replace(u'[',u'\\[').replace(u'+',u'\\
+').split(u',')
    repat = u'|'.join([u'('+i+u'$)' for i in ci])
    rec = re.compile(repat)
    r = 0
    while ntimes:
        r = rec.match(path1) is not None
        r = rec.match(path2) is not None
        ntimes -= 1

def t_rematch_string(ntimes=10, funcloop=u'funcloop',
excludecall='excludecall'):
    """
    Not compiled.

    @UID@ 80fa1ca3-5d51-4f6e-8ac2-4ccafe4c1160
    @author@ Mazhugin Aleksey
    @score_common@ 1
    """
    ci = patt5.replace(u'\\',u'\\\\').replace(u'|',u'\
\|').replace(u'.',u'\\.').replace(u'*',u'.*'). \
        replace(u'?',u'.?').replace(u'$',u'\\$').replace(u'^',u'\
\^').replace(u'{',u'\\{'). \
        replace(u'(',u'\\(').replace(u'[',u'\\[').replace(u'+',u'\\
+').split(u',')
    repat = u'|'.join([u'('+i+u'$)' for i in ci])
    #rec = re.compile(repat)
    r = 0
    while ntimes:
        r = re.match(repat, path1) is not None
        r = re.match(repat, path2) is not None
        ntimes -= 1



More information about the Python-list mailing list