# Run this script using: # python benchmark.py script bench_unicode_eq.py --file=bench # # https://bitbucket.org/haypo/misc/src/tip/python/benchmark.py CHARSETS = ("A", "\xe9", "\u20ac", "\U0010ffff") LENGTHS = (1, 2, 3, 10, 10**3, 10**6) def bench_eq(bench, func, what): bench.start_group(what) for charset in CHARSETS: for length in LENGTHS: name = "%s, %r, %s" % (what, charset, length) setup = "a=%r * %s; b=%s" % (charset, length, func) bench.timeit("a == b", setup=setup, name=name) def run_benchmark(bench): bench_eq(bench, 'a', "equal (is)") bench_eq(bench, 'a.encode().decode()', "equal") bench_eq(bench, '"x" + a[:-1]', "text[0]") bench_eq(bench, 'a[0] + "x" + a[2:]', "text[1]") bench_eq(bench, 'a[:-1] + "x"', "text[-1]") bench_eq(bench, 'a[:-2] + "x" + a[-1]', "text[-2]")