Skip to content

Instantly share code, notes, and snippets.

@ianmiell
Last active February 16, 2018 12:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ianmiell/74b8fe23758f001e4f862b3e2d02e3d0 to your computer and use it in GitHub Desktop.
Save ianmiell/74b8fe23758f001e4f862b3e2d02e3d0 to your computer and use it in GitHub Desktop.
imiell@basquiat $ cat a.sh
#!/bin/bash
for i in {1..100000}
do
VAR='HEADERMy voice is my passwordFOOTER'
PASS=${VAR#HEADER}
PASS=${PASS%FOOTER}
done
imiell@basquiat $ cat a.py
for i in range(100000):
var = 'HEADERMy voice is my passwordFOOTER'
password = var.lstrip('HEADER')
password = password.rstrip('FOOTER')
imiell@basquiat $ time ./a.sh
real 0m2.442s
user 0m2.395s
sys 0m0.026s
imiell@basquiat $ time python a.py
real 0m0.104s
user 0m0.085s
sys 0m0.010s
imiell@basquiat $ cat a.sh
#!/bin/bash
for i in {1..1000}
do
VAR='HEADERMy voice is my passwordFOOTER'
PASS=${VAR#HEADER}
PASS=${PASS%FOOTER}
done
imiell@basquiat $ cat a.py
for i in range(1000):
var = 'HEADERMy voice is my passwordFOOTER'
password = var.lstrip('HEADER')
password = password.rstrip('FOOTER')
imiell@basquiat $ time ./a.sh
real 0m0.035s
user 0m0.034s
sys 0m0.001s
imiell@basquiat $ time python a.py
real 0m0.029s
user 0m0.016s
sys 0m0.013s
@akx
Copy link

akx commented Jan 21, 2018

Note that (l|r|)strip() doesn't strip the verbatim strings you pass in, but any number of the characters passed in! That is, .strip('HEADER') is equivalent to .strip('ADEHR'), for instance, and if the password we want to get at happened to start with any of those characters (or ended with any number of E, F, O, T, or R), those would have gotten stripped as well and we'd ended up with the wrong password.

A more equivalent version would be

password = (password[6:] if password.startswith('HEADER') else password)
password = (password[:-6] if password.endswith('FOOTER') else password)

(or you could use the re module).

@ianmiell
Copy link
Author

ianmiell commented Feb 4, 2018

Fair point - out of context this makes less sense. I was looking for a speed comparison to prove bash is slower. But it wasn't.

@ianmiell
Copy link
Author

Except that the startup time of python makes a difference here - have updated the gist to reflect the fact that bash is slower after all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment