예제 #1
0
def main():
    # configure logger
    log_format = '[%(levelname)s] (#%(lineno)s) %(filename)s->%(funcName)s>>> "%(message)s"'
    logging.basicConfig(format=log_format)
    text = None
    with open('resources/big.txt', 'r') as f:
        text = f.read()
    result = {}
    pattern = 'Boscombe Valley is a country district not very far from Ross, in Herefordshire. The largest landed proprietor in that part is a Mr. John Turner, who made his money in Australia and returned some years ago to the old country. One of the farms which he held, that of Hatherley, was let to Mr. Charles McCarthy, who was also an ex-Australian. The men had known each other in the colonies, so that it was not unnatural that when they came to settle down they should do so as near each other as possible. Turner was apparently the richer man, so McCarthy became his tenant but still remained'
    runs = 50
    for name in sm.algs:
        alg = sm.algs[name]
        print('Measuring time for "{}" algorithm.'.format(name))
        result[name] = {
            'min': maxsize,
            'max': 0
        }
        for i in range(1, runs):
            start = time.clock()
            sm.search(pattern, text, alg, False)
            end = time.clock()
            duration = end-start
            if duration < result[name]['min']:
                result[name]['min'] = duration
            if duration > result[name]['max']:
                result[name]['max'] = duration
    result['find'] = {
        'min': maxsize,
        'max': 0
    }
    for i in range(1, runs):
        start = time.clock()
        text.find(pattern)
        end = time.clock()
        duration = end-start
        if duration < result['find']['min']:
            result['find']['min'] = duration
        if duration > result['find']['max']:
            result['find']['max'] = duration
    pprint(result)
    with open('resources/pi_10million.txt', 'r') as f:
        text = f.read()
    result = {}
    for name in sm.algs:
        alg = sm.algs[name]
        print('Measuring time for "{}" algorithm.'.format(name))
        result[name] = {
            'min': maxsize,
                'max': 0
            }
        for i in range(1, 3):
            start = time.clock()
            sm.search('180986', text, alg, False)
            end = time.clock()
            duration = end-start
            if duration < result[name]['min']:
                result[name]['min'] = duration
            if duration > result[name]['max']:
                result[name]['max'] = duration
    pprint(result)
 def test_all(self):
     for name in sm.algs:
         alg = sm.algs[name]
         text = 'a' * 1024 + 'b'
         self.assertEqual([len(text) - 1], sm.search('b', text, alg, True))
         text = ('a' * 128 + 'b') * 5
         result = [x * 128 + x - 1 for x in range(1, 6)]
         self.assertEqual(result, sm.search('b', text, alg, True))
         pattern = 'ac'
         text = ('abab' * 1024 + 'ac') * 5
         result = [x * 4 * 1024 + 2 * (x - 1) for x in range(1, 6)]
         self.assertEqual(result, sm.search(pattern, text, alg, True))
 def test_all(self):
     for name in sm.algs:
         alg = sm.algs[name]
         text = 'a'*1024+'b'
         self.assertEqual([ len(text)-1 ], sm.search('b', text, alg, True))
         text = ('a'*128+'b')*5
         result = [ x*128+x-1 for x in range(1,6) ]
         self.assertEqual(result, sm.search('b', text, alg, True))
         pattern = 'ac'
         text = ('abab'*1024+'ac')*5
         result = [ x*4*1024+2*(x-1) for x in range(1,6) ]
         self.assertEqual(result, sm.search(pattern, text, alg, True))
예제 #4
0
def main():
    # configure logger
    log_format = '[%(levelname)s] (#%(lineno)s) %(filename)s->%(funcName)s>>> "%(message)s"'
    logging.basicConfig(format=log_format)
    text = None
    with open('resources/big.txt', 'r') as f:
        text = f.read()
    result = {}
    pattern = 'Boscombe Valley is a country district not very far from Ross, in Herefordshire. The largest landed proprietor in that part is a Mr. John Turner, who made his money in Australia and returned some years ago to the old country. One of the farms which he held, that of Hatherley, was let to Mr. Charles McCarthy, who was also an ex-Australian. The men had known each other in the colonies, so that it was not unnatural that when they came to settle down they should do so as near each other as possible. Turner was apparently the richer man, so McCarthy became his tenant but still remained'
    runs = 50
    for name in sm.algs:
        alg = sm.algs[name]
        print('Measuring time for "{}" algorithm.'.format(name))
        result[name] = {'min': maxsize, 'max': 0}
        for i in range(1, runs):
            start = time.clock()
            sm.search(pattern, text, alg, False)
            end = time.clock()
            duration = end - start
            if duration < result[name]['min']:
                result[name]['min'] = duration
            if duration > result[name]['max']:
                result[name]['max'] = duration
    result['find'] = {'min': maxsize, 'max': 0}
    for i in range(1, runs):
        start = time.clock()
        text.find(pattern)
        end = time.clock()
        duration = end - start
        if duration < result['find']['min']:
            result['find']['min'] = duration
        if duration > result['find']['max']:
            result['find']['max'] = duration
    pprint(result)
    with open('resources/pi_10million.txt', 'r') as f:
        text = f.read()
    result = {}
    for name in sm.algs:
        alg = sm.algs[name]
        print('Measuring time for "{}" algorithm.'.format(name))
        result[name] = {'min': maxsize, 'max': 0}
        for i in range(1, 3):
            start = time.clock()
            sm.search('180986', text, alg, False)
            end = time.clock()
            duration = end - start
            if duration < result[name]['min']:
                result[name]['min'] = duration
            if duration > result[name]['max']:
                result[name]['max'] = duration
    pprint(result)
 def test_corner_cases(self):
     for name in sm.algs:
         alg = sm.algs[name]
         self.assertEqual(
             self.one_char['result'],
             sm.search(self.one_char['pattern'], self.one_char['text'], alg,
                       True))
         self.assertEqual(
             self.longer['result'],
             sm.search(self.longer['pattern'], self.longer['text'], alg,
                       True))
         self.assertEqual(
             self.not_in_text['result'],
             sm.search(self.not_in_text['pattern'],
                       self.not_in_text['text'], alg, True))
         self.assertEqual(
             self.same_length['result'],
             sm.search(self.same_length['pattern'],
                       self.same_length['text'], alg, True))
         self.assertEqual(
             self.same_length['result'],
             sm.search(self.same_length['pattern'],
                       self.same_length['text'], alg, True))
         self.assertEqual(
             self.same_length['result'],
             sm.search(self.same_length['pattern'],
                       self.same_length['text'], alg, True))
         self.assertEqual(
             self.same_length['result'],
             sm.search(self.same_length['pattern'],
                       self.same_length['text'], alg, True))
         self.assertEqual(
             self.same_length['result'],
             sm.search(self.same_length['pattern'],
                       self.same_length['text'], alg, True))
         self.assertEqual(
             self.is_the_same['result'],
             sm.search(self.is_the_same['pattern'],
                       self.is_the_same['text'], alg, True))
         self.assertEqual(
             self.almost['result'],
             sm.search(self.almost['pattern'], self.almost['text'], alg,
                       True))
 def test_corner_cases(self):
     for name in sm.algs:
         alg = sm.algs[name]
         self.assertEqual(self.one_char['result'],
             sm.search(self.one_char['pattern'], self.one_char['text'], alg, True))
         self.assertEqual(self.longer['result'],
             sm.search(self.longer['pattern'], self.longer['text'], alg, True))
         self.assertEqual(self.not_in_text['result'],
             sm.search(self.not_in_text['pattern'], self.not_in_text['text'], alg, True))
         self.assertEqual(self.same_length['result'],
             sm.search(self.same_length['pattern'], self.same_length['text'], alg, True))
         self.assertEqual(self.same_length['result'],
             sm.search(self.same_length['pattern'], self.same_length['text'], alg, True))
         self.assertEqual(self.same_length['result'],
             sm.search(self.same_length['pattern'], self.same_length['text'], alg, True))
         self.assertEqual(self.same_length['result'],
             sm.search(self.same_length['pattern'], self.same_length['text'], alg, True))
         self.assertEqual(self.same_length['result'],
             sm.search(self.same_length['pattern'], self.same_length['text'], alg, True))
         self.assertEqual(self.is_the_same['result'],
             sm.search(self.is_the_same['pattern'], self.is_the_same['text'], alg, True))
         self.assertEqual(self.almost['result'],
             sm.search(self.almost['pattern'], self.almost['text'], alg, True))