Created
November 13, 2013 03:17
-
-
Save jlisee/7443118 to your computer and use it in GitHub Desktop.
A test program to investigate command lookup speed in plumbum.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Author: Joseph Lisee <[email protected] | |
import os | |
import time | |
from contextlib import contextmanager | |
# Little timing helper | |
@contextmanager | |
def timer(msg): | |
now = time.time() | |
yield | |
print '%s %.5f' % (msg,time.time() - now) | |
# Import statement | |
with timer('from plumbum import local:'): | |
from plumbum import local | |
# List of our test command | |
cmds = ['grep', 'wc', 'cat', 'head', 'ls', 'touch', 'mkdir', 'rm', 'echo', 'dd'] | |
# twisted which (run first so plumbum gets any cache benefits) | |
def which(name, flags=os.X_OK): | |
result = [] | |
exts = filter(None, os.environ.get('PATHEXT', '').split(os.pathsep)) | |
path = os.environ.get('PATH', None) | |
if path is None: | |
return [] | |
for p in os.environ.get('PATH', '').split(os.pathsep): | |
p = os.path.join(p, name) | |
if os.access(p, flags): | |
result.append(p) | |
for e in exts: | |
pext = p + e | |
if os.access(pext, flags): | |
result.append(pext) | |
return result | |
with timer('twisted.python.procutils.which:'): | |
tresults = set([which(cmd)[0] for cmd in cmds]) | |
# plumbum which | |
with timer('plumbum local.which:'): | |
presults = set([local.which(cmd) for cmd in cmds]) | |
# Make sure we get the same results | |
assert tresults == presults | |
# plumbum command import | |
with timer('from plumbum.cmd import:'): | |
from plumbum.cmd import grep, wc, cat, head, ls, touch, mkdir, rm, echo, dd |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment