Skip to content

Instantly share code, notes, and snippets.

@hawkowl
Created March 31, 2015 06:52
Show Gist options
  • Save hawkowl/c5c37c2b0318edcccb6c to your computer and use it in GitHub Desktop.
Save hawkowl/c5c37c2b0318edcccb6c to your computer and use it in GitHub Desktop.
from twisted.python.compat import networkFormat, networkString, nativeString
import timeit
# Mixed types
def mixed_format():
networkFormat("{0}:{1}:{2}", (b"foo", u"bar", "baz"))
def bytes_format():
networkFormat("{0}:{1}:{2}", (b"foo", b"bar", b"baz"))
def unicode_format():
networkFormat("{0}:{1}:{2}", (u"foo", u"bar", u"baz"))
def mixed_perc():
"%s:%s:%s" % (nativeString(b"foo"), nativeString(u"bar"), nativeString("bar"))
def bytes_perc():
"%s:%s:%s" % (nativeString(b"foo"), nativeString(b"bar"), nativeString(b"bar"))
def unicode_perc():
"%s:%s:%s" % (nativeString(u"foo"), nativeString(u"bar"), nativeString(u"bar"))
def mixed_join():
b":".join([b"foo", u"bar".encode("ascii"), networkString("bar")])
def bytes_join():
b":".join([b"foo", b"bar", b"baz"])
def unicode_join():
b":".join([u"foo".encode("ascii"), u"bar".encode("ascii"), u"baz".encode("ascii")])
def prt(imp):
print(",".join(map(str, imp)))
prt(["mixedfrmt"] + timeit.Timer(mixed_format).repeat(6, 100000))
prt(["bytesfrmt"] + timeit.Timer(bytes_format).repeat(6, 100000))
prt(["unicodefrmt"] + timeit.Timer(unicode_format).repeat(6, 100000))
prt(["mixedperc"] + timeit.Timer(mixed_perc).repeat(6, 100000))
prt(["bytesperc"] + timeit.Timer(bytes_perc).repeat(6, 100000))
prt(["unicodeperc"] + timeit.Timer(unicode_perc).repeat(6, 100000))
prt(["mixedjoin"] + timeit.Timer(mixed_join).repeat(6, 100000))
prt(["bytesjoin"] + timeit.Timer(bytes_join).repeat(6, 100000))
prt(["unicodejoin"] + timeit.Timer(unicode_join).repeat(6, 100000))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment