Example #1
0
 def test_csv(self):
     bench1 = Benchmark('10*10', iterations=10000)
     results = bench1.run(foo=1, bar='A')
     self.assertEqual(results.csv_header(), 'Description,Count,Fastest,Total time,Iterations,bar,foo')
     csv = results.csv_data()
     self.assertTrue(csv.startswith('"10*10"'))
     self.assertTrue(csv.endswith(',10000,A,1'))
     self.assertEqual(len(csv.split(',')), 7)
Example #2
0
    def test_keywords(self):
        # You can pass in keywords to the test function, to parameterize the test
        runs = {}
        def blah(foo, bar):
            runs[foo] = bar
            
        bench1 = Benchmark(blah, iterations=1, bestof=1)
        bench1.run(foo=1, bar='A')
        bench1.run(foo=2, bar='B')
        bench1.run(foo=3, bar='C')

        self.assertEqual(runs, {1: 'A', 2: 'B', 3: 'C'} )
Example #3
0
 def test_function(self):
     # For more complex tests you can pass in a function:
     runs = [0]
     def blah():
         runs[0] += 1
         
     bench1 = Benchmark(blah, iterations=10000)
     result = bench1.run()
     
     # Check that the function was wrong approximately the expected
     # amounts of times. Allow for up to two runs "wrong":
     self.assertTrue(abs(runs[0]/30000)-result.count < 2)
Example #4
0
 def test_bestof(self):
     # Tests are being run multiple times, and the best result is picked.
     # This is tested by setting the duration to 0, because then each run
     # will always be one iteration. This test also makes sure the
     # Benchmark doesn't fail if the test takes longer than the duration.
     runs = [0]
     def blah():
         # We make a loop here, or this test is so fast under Jython that 
         # Jython claims it takes zero time.
         for loop in range(10000):
             a = 1
         runs[0] += 1
         
     bench1 = Benchmark(blah, iterations=1, duration=0, bestof=5)
     result = bench1.run()
     
     self.assertEqual(runs[0], 5)
Example #5
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])
Example #6
0

def setup_u():
    return {
        'instr1': string.ascii_lowercase.decode(),
        'instr2': string.ascii_uppercase.decode()
    }


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


simple_u_b = Benchmark(simple_u_f,
                       setup=setup_u,
                       description="Simple concatenation: s = s1 + s2")


def join_u_f(instr1, instr2):
    for x in xrange(10000):
        leftover = u''.join((instr1, instr2))


join_u_b = Benchmark(join_u_f,
                     setup=setup_u,
                     description="Joining two strings: s1=''.join((s1, s2))")


def oldformat_u_f(instr1, instr2):
    for x in xrange(10000):
Example #7
0
def setup_u():
    return {
        'null': u'',
        'char': u'A',
    }


def add_two_f(char, null):
    result = null
    for x in xrange(1000):
        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)
Example #8
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])
Example #9
0
 def test_statement(self):
     # You can benchmark a python statement by passing in a string.
     # This is fast, because you don't have function calling overhead.
     bench1 = Benchmark('10*10', iterations=10000)
     bench1.run()
Example #10
0
bchar = uchar.encode('ascii')
stringies = [uchar * x for x in xrange(1000)]
byties = [bchar * x for x in xrange(1000)]
unull = u''
bnull = unull.encode('ascii')


def simple_b_f():
    thelist = byties
    s = bnull
    for x in xrange(999, -1, -1):
        f = thelist[x]
        s = s + f


simple_b_b = Benchmark(simple_b_f,
                       description="Simple byte concatenation: s1 = s1 + s2")


def simple_u_f():
    thelist = stringies
    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():
Example #11
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])
Example #12
0
    f = 0
    for x in xrange(10000):
        f += x * const


def pseudo_constant_power_inside_f():
    f = 0
    for x in xrange(10000):
        f += x * (5**var)


def variation_f():
    f = len(xrange(10000)) * 17.5


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')
Example #13
0
from proust.suite import Suite

try:
    xrange = xrange
except NameError:
    xrange = range

strings = ['A' * x for x in xrange(1000)]


def add_two_f():
    for x in range(999, -1, -1):
        b = strings[9] + strings[20]


add_two_b = Benchmark(add_two_f, description="Adding 2")


def join_two_f():
    l = []
    for x in range(999, -1, -1):
        b = ''.join((strings[9], strings[20]))


join_two_b = Benchmark(join_two_f, description="Joining 2")


def add_three_f():
    for x in range(999, -1, -1):
        b = strings[999] + strings[20] + strings[14]