コード例 #1
0

'''''' '''''' '''''' '''''' '''''' '''''' '''''' '''''' '''''' '''
                    Lookup Speed Tests
''' '''''' '''''' '''''' '''''' '''''' '''''' '''''' '''''' ''''''


@ComparisonBenchmark('lookup')
def class_attr_lookup():
    c = MyClass('calvin', 25, 'male')
    for i in xrange(10000):
        name, age, gender = c.name, c.age, c.gender


@ComparisonBenchmark('lookup')
def namedtuple_attr_lookup():
    c = MyNamedTuple('calvin', 25, 'male')
    for i in xrange(10000):
        name, age, gender = c.name, c.age, c.gender


@ComparisonBenchmark('lookup')
def dict_attr_lookup():
    c = {'name': 'calvin', 'age': 25, 'gender': 'male'}
    for i in xrange(10000):
        name, age, gender = c['name'], c['age'], c['gender']


ComparisonBenchmark.summarize('class')
ComparisonBenchmark.summarize('lookup')
コード例 #2
0
__author__ = 'calvin'
"""
This example demonstrates the performance benefit of using list comprehension instead of for loop and append.
It also demonstrates how to use imported modules in your benchmarks as well as compare functions of the same group.
"""

try:
    from builtins import range
except ImportError:
    range = xrange
from math import sin  #!

from pyperform import ComparisonBenchmark


@ComparisonBenchmark('Group1', validation=True, largs=(100, ))
def list_append(n, *args, **kwargs):
    l = []
    for i in range(1, n):
        l.append(sin(i))
    return l


@ComparisonBenchmark('Group1', validation=True, largs=(100, ))
def list_comprehension(n, *args, **kwargs):
    l = [sin(i) for i in range(1, n)]
    return l


ComparisonBenchmark.summarize('Group1', fs='report.txt')
コード例 #3
0
import random #!

def _setup():
    l = []
    lookup = {ii: l.append for ii in range(5)}


largs = (random.randint(0, 4),)

@ComparisonBenchmark('group1', setup=_setup, validation=True, timeit_number=10, timeit_repeat=1000)
def do_if():
    ii = random.randint(0, 4)
    if ii == 0:
        l.append(ii)
    elif ii == 1:
        l.append(ii)
    elif ii == 2:
        l.append(ii)
    elif ii == 3:
        l.append(ii)
    elif ii == 4:
        l.append(ii)

@ComparisonBenchmark('group1', setup=_setup, validation=True, timeit_number=10, timeit_repeat=1000)
def do_dict_lookup():
    ii = random.randint(0, 4)
    lookup[ii](ii)


ComparisonBenchmark.summarize('group1')
コード例 #4
0
__author__ = 'calvin'
"""
This example demonstrates the performance benefit of using list comprehension instead of for loop and append.
It also demonstrates how to use imported modules in your benchmarks as well as compare functions of the same group.
"""

try:
    from builtins import range
except ImportError:
    range = xrange
from pyperform import ComparisonBenchmark
from math import sin  #!


@ComparisonBenchmark('Group1', validation=True, largs=(100,))
def list_append(n, *args, **kwargs):
    l = []
    for i in range(1, n):
        l.append(sin(i))
    return l


@ComparisonBenchmark('Group1', validation=True, largs=(100,))
def list_comprehension(n, *args, **kwargs):
    l = [sin(i) for i in range(1, n)]
    return l


ComparisonBenchmark.summarize('Group1', fs='report.txt')
コード例 #5
0
__author__ = 'Calvin'
from pyperform import ComparisonBenchmark, BenchmarkedFunction


@ComparisonBenchmark('Group1', largs=(100,))
def mytest(l):
    out = 0.
    for i in range(l):
        out += i
    return out

@ComparisonBenchmark('Group1', largs=(100,))
def mytest2(l):
    out = 0.
    for i in range(l):
        out += i

    return out

@BenchmarkedFunction(largs=(5, 2, 10))
def TripleMultiply(a, b, c):
    result = a * b * c
    return result

with open('report.txt', 'w') as f:
    ComparisonBenchmark.summarize('Group1', f)

コード例 #6
0
    @ComparisonBenchmark('Calculate Savings', classname="Person", timeit_number=100, validation=True, largs=(55,),
                         kwargs={'monthly_spending': 500})
    def calculate_savings_method1(self, retirement_age, monthly_spending=0):
        savings = 0
        for y in range(self.age, retirement_age):
            for m in range(12):
                savings += self.monthly_income - monthly_spending
        return savings

    @ComparisonBenchmark('Calculate Savings', classname="Person", timeit_number=100, validation=True, largs=(55,),
                         kwargs={'monthly_spending': 500})
    def calculate_savings_method2(self, retirement_age, monthly_spending=0):
        yearly_income = 12 * (self.monthly_income - monthly_spending)
        n_years = retirement_age - self.age
        if n_years > 0:
            return yearly_income * n_years

    @BenchmarkedFunction(classname="Person", timeit_number=100, largs=(55,), kwargs={'monthly_spending': 500})
    def same_as_method_2(self, retirement_age, monthly_spending=0):
        yearly_income = 12 * (self.monthly_income - monthly_spending)
        n_years = retirement_age - self.age
        if n_years > 0:
            return yearly_income * n_years


# Can print the summary to file or if ComparisonBenchmark.summarize() is not given an fs parameter, it will print to
# console.
report_file = open('report.txt', 'w')
ComparisonBenchmark.summarize(group='Calculate Savings', fs=report_file)
コード例 #7
0
@ComparisonBenchmark('group1',
                     setup=_setup,
                     validation=True,
                     timeit_number=100,
                     timeit_repeat=10000)
def do_if():
    ii = random.randint(0, 4)
    if ii == 0:
        l.append(ii)
    elif ii == 1:
        l.append(ii)
    elif ii == 2:
        l.append(ii)
    elif ii == 3:
        l.append(ii)
    elif ii == 4:
        l.append(ii)


@ComparisonBenchmark('group1',
                     setup=_setup,
                     validation=True,
                     timeit_number=100,
                     timeit_repeat=10000)
def do_dict_lookup():
    ii = random.randint(0, 4)
    lookup[ii](ii)


ComparisonBenchmark.summarize('group1')
コード例 #8
0
                savings += self.monthly_income - monthly_spending
        return savings

    @ComparisonBenchmark('Calculate Savings',
                         classname="Person",
                         timeit_number=100,
                         validation=True,
                         largs=(55, ),
                         kwargs={'monthly_spending': 500})
    def calculate_savings_method2(self, retirement_age, monthly_spending=0):
        yearly_income = 12 * (self.monthly_income - monthly_spending)
        n_years = retirement_age - self.age
        if n_years > 0:
            return yearly_income * n_years

    @BenchmarkedFunction(classname="Person",
                         timeit_number=100,
                         largs=(55, ),
                         kwargs={'monthly_spending': 500})
    def same_as_method_2(self, retirement_age, monthly_spending=0):
        yearly_income = 12 * (self.monthly_income - monthly_spending)
        n_years = retirement_age - self.age
        if n_years > 0:
            return yearly_income * n_years


# Can print the summary to file or if ComparisonBenchmark.summarize() is not given an fs parameter, it will print to
# console.
report_file = open('report.txt', 'w')
ComparisonBenchmark.summarize(group='Calculate Savings', fs=report_file)
コード例 #9
0

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
                    Lookup Speed Tests
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''


@ComparisonBenchmark('lookup')
def class_attr_lookup():
    c = MyClass('calvin', 25, 'male')
    for i in xrange(10000):
        name, age, gender = c.name, c.age, c.gender


@ComparisonBenchmark('lookup')
def namedtuple_attr_lookup():
    c = MyNamedTuple('calvin', 25, 'male')
    for i in xrange(10000):
        name, age, gender = c.name, c.age, c.gender


@ComparisonBenchmark('lookup')
def dict_attr_lookup():
    c = {'name': 'calvin', 'age': 25, 'gender': 'male'}
    for i in xrange(10000):
        name, age, gender = c['name'], c['age'], c['gender']


ComparisonBenchmark.summarize('class')
ComparisonBenchmark.summarize('lookup')