Beispiel #1
0
oldformat_u_b = Benchmark(oldformat_u_f,
                          setup=setup_u,
                          description="Old formatting: s1 = '%s%s' % (s1, s2)")


def newformat_u_f(instr1, instr2):
    for x in xrange(10000):
        leftover = u"{0}{1}".format(instr1, instr2)


newformat_u_b = Benchmark(
    newformat_u_f,
    setup=setup_u,
    description="New formatting: s1 = '{0}{1}'.format(s1, s2)")

suite_u = Suite([oldformat_u_b, simple_u_b, join_u_b])


def setup_b():
    return {
        'instr1': string.ascii_lowercase.encode(),
        'instr2': string.ascii_uppercase.encode(),
    }


def simple_b_f(instr1, instr2):
    for x in xrange(10000):
        leftover = instr1 + instr2


simple_b_b = Benchmark(simple_b_f,
Beispiel #2
0
    s = set()
    for x in dupset:
        s.add(x)


def inserting_dict_f():
    s = list()
    for x in dupset:
        s.append(x)


def inserting_dict_and_making_set_f():
    s = list()
    for x in dupset:
        s.append(x)
    s = set(s)


dictset_b = Benchmark(dictset_f, description="dictionary as set")
setset_b = Benchmark(setset_f, description="set")
listset_b = Benchmark(listset_f, description="list")
inserting_set_b = Benchmark(inserting_set_f, description="inserting into set")
inserting_dict_b = Benchmark(inserting_dict_f,
                             description="appending into list")
inserting_dict_and_making_set_b = Benchmark(inserting_dict_and_making_set_f,
                                            description="set(list)")

suite = Suite([dictset_b, setset_b, listset_b])
suite2 = Suite(
    [inserting_set_b, inserting_dict_b, inserting_dict_and_making_set_b])
Beispiel #3
0
 def test_suite(self):
     suite = Suite(benchmarks=[Benchmark('10*10', iterations=10000, duration=0.1)])
     suite.benchmarks.append(Benchmark('100*100', iterations=10000, duration=0.1))
     results = suite.run()
     self.assertEqual(len(results), 2)
Beispiel #4
0
        result = result + (x * char)


add_two_b_b = Benchmark(add_two_f,
                        setup=setup_b,
                        description="Simple byte concatenation: s1 = s1 + s2")

add_two_u_b = Benchmark(
    add_two_f,
    setup=setup_u,
    description="Simple unicode concatenation: s1 = s1 + s2")


def join_two_f(char, null):
    l = []
    for x in xrange(1000):
        l.append(x * char)

    result = null.join(l)


join_two_b_b = Benchmark(join_two_f,
                         setup=setup_b,
                         description="Joining a byte list: s1 = ''.join(l)")

join_two_u_b = Benchmark(join_two_f,
                         setup=setup_u,
                         description="Joining a unicode list: s1 = ''.join(l)")

suite = Suite([add_two_b_b, join_two_b_b, add_two_u_b, join_two_u_b])
Beispiel #5
0
def sorted_list_f(l):
    return sorted(l)


cl = lambda a, b: -cmp(a * 2, b * 2)
kl = lambda a: a * 2


def sort_cmp(l):
    l.sort(cmp=cl)


def sort_key(l):
    l.sort(key=kl)


sort_b = Benchmark(sort_f, description="sort")
sorted_b = Benchmark(sorted_f, description="sorted")

suite = Suite([sort_b, sorted_b])

sort_list_b = Benchmark(sort_list_f, description="sort")
sorted_list_b = Benchmark(sorted_list_f, description="sorted")

suite2 = Suite([sort_list_b, sorted_list_b])

cmp_b = Benchmark(sort_cmp, description="cmp", setup=thesetup)
key_b = Benchmark(sort_key, description="key", setup=thesetup)

suite3 = Suite([cmp_b, key_b])
Beispiel #6
0
    s = unull
    for x in xrange(999, -1, -1):
        f = thelist[x]
        s = s + f


simple_u_b = Benchmark(
    simple_u_f, description="Simple unicode concatenation: s1 = s1 + s2")


def join_b_f():
    thelist = byties
    s = bnull
    s = s.join(thelist)


join_b_b = Benchmark(join_b_f,
                     description="Joining a byte list: s1 = b''.join(l)")


def join_u_f():
    thelist = stringies
    s = unull
    s = s.join(thelist)


join_u_b = Benchmark(join_u_f,
                     description="Joining a unicode list: s1 = u''.join(l)")

suite = Suite([simple_b_b, join_b_b, simple_u_b, join_u_b])
Beispiel #7
0
keys = list(range(1000))
dupes = random.sample(keys, 500)


def defaultdict_f():
    d = defaultdict(set)
    for k in keys:
        d[k].add(0)
    for k in dupes:
        d[k].add(0)


def normaldict_f():
    d = {}
    for k in keys:
        if k in d:
            d[k].add(0)
        else:
            d[k] = set([0])
    for k in dupes:
        if k in d:
            d[k].add(0)
        else:
            d[k] = set([0])


defaultdict_b = Benchmark(defaultdict_f, description="defaultdict")
normaldict_b = Benchmark(normaldict_f, description="dict")

suite = Suite([defaultdict_b, normaldict_b])
Beispiel #8
0
real_constant_outside_b = Benchmark(real_constant_outside_f,
                                    description="multiplication outside loop")
real_constant_inside_b = Benchmark(real_constant_inside_f,
                                   description="multiplication inside loop")
real_constant_division_outside_b = Benchmark(
    real_constant_division_outside_f, description="division outside loop")
real_constant_division_inside_b = Benchmark(real_constant_division_inside_f,
                                            description="division inside loop")
pseudo_constant_outside_b = Benchmark(
    pseudo_constant_outside_f, description="var multiplication outside loop")
pseudo_constant_inside_b = Benchmark(
    pseudo_constant_inside_f, description="var multiplication inside loop")
pseudo_constant_power_outside_b = Benchmark(
    pseudo_constant_power_outside_f, description="var power outside loop")
pseudo_constant_power_inside_b = Benchmark(pseudo_constant_power_inside_f,
                                           description="var power inside loop")
variation_b = Benchmark(variation_f, description='non-stupid')

suite = Suite([
    #    real_constant_outside_b,
    #    real_constant_inside_b,
    #    real_constant_division_outside_b,
    real_constant_division_inside_b,
    pseudo_constant_outside_b,
    pseudo_constant_inside_b,
    pseudo_constant_power_outside_b,
    pseudo_constant_power_inside_b,
    #    variation_b,
])
Beispiel #9
0
        b = ''.join((strings[999], strings[20], strings[14], strings[200]))


join_four_b = Benchmark(join_four_f, description="Joining 4")


def add_ten_f():
    for x in range(999, -1, -1):
        b = strings[999] + strings[20] + strings[14] + strings[200] + strings[
            234] + strings[9] + strings[40] + strings[5] + strings[
                120] + strings[123]


add_ten_b = Benchmark(add_ten_f, description="Adding 4")


def join_ten_f():
    l = []
    for x in range(999, -1, -1):
        b = ''.join((strings[999], strings[20], strings[14], strings[200],
                     strings[234], strings[9], strings[40], strings[5],
                     strings[120], strings[123]))


join_ten_b = Benchmark(join_ten_f, description="Joining 4")

suite = Suite([
    add_two_b, join_two_b, add_three_b, join_three_b, add_four_b, join_four_b,
    add_ten_b, join_ten_b
])