Skip to content

Instantly share code, notes, and snippets.

@cdecker
Created January 2, 2017 10:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdecker/a4f4f042e58f0c8744db864f4a254f30 to your computer and use it in GitHub Desktop.
Save cdecker/a4f4f042e58f0c8744db864f4a254f30 to your computer and use it in GitHub Desktop.
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
Type "copyright", "credits" or "license" for more information.
IPython 5.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: from test import f, g, s
In [2]: %timeit s()
The slowest run took 27.60 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 1.41 µs per loop
In [3]: %timeit f()
The slowest run took 18.01 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 2.34 µs per loop
In [4]: %timeit g()
10000 loops, best of 3: 14.5 µs per loop
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
Type "copyright", "credits" or "license" for more information.
IPython 5.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: from test import f, g, s
In [2]: %timeit s()
The slowest run took 6.12 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 984 ns per loop
In [3]: %timeit f()
The slowest run took 11.51 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 1.73 µs per loop
In [4]: %timeit g()
100000 loops, best of 3: 9.36 µs per loop
from hashlib import sha256
from binascii import hexlify, unhexlify
a = 'a'
def s():
global a
a = sha256(a.encode()).hexdigest()
return a
def f():
return hexlify(unhexlify(s())[::-1])
def g():
""" Switches the endianness of a hex string (in pairs of hex chars) """
s = s()
pairList = [s[i:i+2].encode() for i in range(0, len(s), 2)]
return b''.join(pairList[::-1]).decode()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment