File: issue_9.py

package info (click to toggle)
python-pyahocorasick 1.4.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 748 kB
  • sloc: ansic: 4,554; python: 2,823; sh: 312; makefile: 242
file content (57 lines) | stat: -rw-r--r-- 1,125 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# -*- coding: utf-8 -*-
"""
    Aho-Corasick string search algorithm.

    Author    : Wojciech Muła, wojciech_mula@poczta.onet.pl
    WWW       : http://0x80.pl
    License   : public domain
"""

import os
import sys

import ahocorasick

ac = ahocorasick.Automaton()
ac.add_word('SSSSS', 1)
ac.make_automaton()

try:
    range = xrange # for Py2
except NameError:
    pass

def get_memory_usage():
    # Linux only
    pid = os.getpid()

    lines = []
    try:
        with open('/proc/%d/status' % pid, 'rt') as f:
            lines = f.readlines()
    except:
        pass

    for line in lines:
        if line.startswith('VmSize'):
            return float(line.split()[1])

    return 0

def test():
    with open('README.rst', 'r') as f:
        data = f.read()[:1024 * 2]

    for loop in range(1000):
        for start in range(0, len(data) - 20):
            ac.iter(data, start)


if __name__ == '__main__':

    before = get_memory_usage()
    test()
    after = get_memory_usage()

    print("Memory's usage growth: %s (before = %s, after = %s)" % (after - before, before, after))
    assert(before == after)