X-FeedAbuse: http://nntpfeed.proxad.net/abuse.pl feeded by 78.192.65.63 Path: csiph.com!usenet.pasdenom.info!nntpfeed.proxad.net!news.muarf.org!news.roellig-ltd.de!open-news-network.org!border2.nntp.ams1.giganews.com!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'url:pypi': 0.03; 'args': 0.04; 'finally:': 0.05; '"__main__":': 0.07; '__name__': 0.07; 'filename': 0.07; 'main()': 0.07; 'matches': 0.07; 'root,': 0.07; 'true)': 0.07; '*args):': 0.09; 'creighton': 0.09; 'files:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'def': 0.14; 'argparse': 0.16; 'fnmatch': 0.16; 'main():': 0.16; 'optimised': 0.16; 're.compile(': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'skip:) 30': 0.16; 'subprocess': 0.16; 'time.time()': 0.16; 'two.': 0.16; 'wrote:': 0.16; 'laura': 0.18; 'version.': 0.18; 'python?': 0.18; 'shell': 0.18; 'do.': 0.22; 'parser': 0.22; 'see:': 0.22; 'try:': 0.22; '2015': 0.23; 'matching': 0.23; 'import': 0.24; 'sort': 0.25; 'wondering': 0.25; 'header:User- Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'switch': 0.27; 'regular': 0.29; 'cat': 0.29; 'path,': 0.29; 'structure,': 0.29; 'url:se': 0.29; 'that.': 0.30; 'posts': 0.31; 'task': 0.31; 'code': 0.31; 'skip:p 30': 0.32; 'url:python': 0.33; 'surprised': 0.33; 'could': 0.35; 'to:addr:python-list': 0.35; 'but': 0.36; 'url:org': 0.36; 'there': 0.36; 'faster': 0.36; 'subject:: ': 0.37; 'difference': 0.38; 'version': 0.38; 'received:org': 0.38; 'skip:p 20': 0.38; 'files': 0.38; 'end': 0.39; 'to:addr:python.org': 0.39; 'skip:e 20': 0.39; 'build': 0.40; 'received:de': 0.40; 'your': 0.60; 'more.': 0.62; 'here:': 0.62; 'results': 0.66; '8bit%:40': 0.66; 'beat': 0.66; 'matthew': 0.66; 'saturday': 0.66; 'skip:\xe2 10': 0.70; 'gain': 0.81; 'cecil': 0.84; 'find.': 0.84; 'investigated': 0.84; 'westerhof': 0.84; 'thing,': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Find in ipython3 Date: Sun, 07 Jun 2015 12:27:05 +0200 Organization: None References: <87y4k2hyvf.fsf@Equus.decebal.nl> <87bnguhbec.fsf@Equus.decebal.nl> <874mmlqhul.fsf@Equus.decebal.nl> <87sia4ox8h.fsf@Equus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Gmane-NNTP-Posting-Host: p57bd96d4.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 113 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1433672873 news.xs4all.nl 2934 [2001:888:2000:d::a6]:55183 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:92225 Cecil Westerhof wrote: > On Saturday 6 Jun 2015 13:07 CEST, Laura Creighton wrote: > >> The !find version is C code optimised to do one thing, find files in >> your directory structure, which happens to be what you want to do. >> General regular expression matching is harder. >> >> Carl Friedrich Bolz investigated regular expression algorithms and >> their implementation to see if this is the sort of task that a JIT >> can improve. He blogged about it in 2 posts (part1 and part2). There >> are benchmarks for part2. Benchmarks in part2. >> >> see: >> http://morepypy.blogspot.se/2010/05/efficient-and-elegant-regular.html >> http://morepypy.blogspot.se/2010/06/jit-for-regular-expression-matching.html >> >> You may get faster results if you use Matthew Barnett's replacement >> for re here: https://pypi.python.org/pypi/regex >> >> You will get faster results if you build your IPython shell to use >> PyPy, but I would still be very surprised if it beat the C program >> find. > > I have to look into that. But I prefer to write a version that can be > used by ‘everyone’. > > It is of-course not a very big program. The difference is significant, > but I do not use find that much. And if it is significant I still can > use the shell version. > > There is no gain to get in standard Python? By switching from fnmatch > to re I got almost a speed gain of two. So I was wondering if I could > do more. Just wait for Python 3.5. The switch from os.listdir() to the (new) os.scandir() in the implementation of os.walk() is likely to improve the situation: $ cat findfiles.py import fnmatch import os import re import subprocess import time def find_re(root, pattern, ignore_case=False): match = re.compile( fnmatch.translate(pattern), re.IGNORECASE if ignore_case else 0).match results = [] for path, _folders, files in os.walk(root): for filename in files: if match(filename): results.append(os.path.join(path, filename)) return results def find_sp( root, pattern, ignore_case=False, encoding="utf-8", errors="surrogateescape"): name_opt = "-iname" if ignore_case else "-name" matches = subprocess.Popen( ["find", root, name_opt, pattern, "-print0"], stdout=subprocess.PIPE ).communicate()[0].decode(encoding, errors=errors).split("\0") assert len(matches[-1]) == 0 del matches[-1] return matches def measure(f, *args): start = time.time() try: return f(*args) finally: end = time.time() print("{}{}".format(f.__name__, args), end - start) def main(): import argparse parser = argparse.ArgumentParser() parser.add_argument("root") parser.add_argument("pattern") parser.add_argument("-i", "--ignore-case", action="store_true") args = parser.parse_args() a = measure(find_re, args.root, args.pattern, args.ignore_case) b = measure(find_sp, args.root, args.pattern, args.ignore_case) measure(find_re, args.root, args.pattern, args.ignore_case) assert sorted(a) == sorted(b) print(len(a), "matches") if __name__ == "__main__": main() $ python3.4 findfiles.py . '*a*.PY' -i find_re('.', '*a*.PY', True) 0.14614605903625488 find_sp('.', '*a*.PY', True) 0.043445587158203125 find_re('.', '*a*.PY', True) 0.16485309600830078 1454 matches $ python3.5 findfiles.py . '*a*.PY' -i find_re('.', '*a*.PY', True) 0.07263660430908203 find_sp('.', '*a*.PY', True) 0.04418468475341797 find_re('.', '*a*.PY', True) 0.07320952415466309 1454 matches