Performance of int/long in Python 3

Neil Hodgson nhodgson at iinet.net.au
Mon Apr 1 19:43:59 EDT 2013


Mark Lawrence:

> You've given many examples of the same type of micro benchmark, not many
> examples of different types of benchmark.

    Trying to work out what jmfauth is on about I found what appears to 
be a performance regression with '<' string comparisons on Windows 
64-bit. Its around 30% slower on a 25 character string that differs in 
the last character and 70-100% on a 100 character string that differs at 
the end.

    Can someone else please try this to see if its reproducible? Linux 
doesn't show this problem.

 >c:\python32\python -u "charwidth.py"
3.2 (r32:88445, Feb 20 2011, 21:30:00) [MSC v.1500 64 bit (AMD64)]
a=['C:/Users/Neil/Documents/b','C:/Users/Neil/Documents/z']176
[0.7116295577956576, 0.7055591343157613, 0.7203483026429418]

a=['C:/Users/Neil/Documents/λ','C:/Users/Neil/Documents/η']176
[0.7664397841378787, 0.7199902325464409, 0.713719289812504]

a=['C:/Users/Neil/Documents/b','C:/Users/Neil/Documents/η']176
[0.7341851791817691, 0.6994205901833599, 0.7106807593741005]

a=['C:/Users/Neil/Documents/𠀀','C:/Users/Neil/Documents/𠀁']180
[0.7346812372666784, 0.6995411113377914, 0.7064768417728411]

 >c:\python33\python -u "charwidth.py"
3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:57:17) [MSC v.1600 64 bit 
(AMD64)]
a=['C:/Users/Neil/Documents/b','C:/Users/Neil/Documents/z']108
[0.9913326076446045, 0.9455845241056282, 0.9459076605341776]

a=['C:/Users/Neil/Documents/λ','C:/Users/Neil/Documents/η']192
[1.0472289217234318, 1.0362342484091207, 1.0197109728048384]

a=['C:/Users/Neil/Documents/b','C:/Users/Neil/Documents/η']192
[1.0439643704533834, 0.9878581050301687, 0.9949265834034335]

a=['C:/Users/Neil/Documents/𠀀','C:/Users/Neil/Documents/𠀁']312
[1.0987483965446412, 1.0130257167690004, 1.024832248526499]

    Here is the code:

# encoding:utf-8
import os, sys, timeit
print(sys.version)
examples = [
"a=['$b','$z']",
"a=['$λ','$η']",
"a=['$b','$η']",
"a=['$\U00020000','$\U00020001']"]
baseDir = "C:/Users/Neil/Documents/"
#~ baseDir = "C:/Users/Neil/Documents/Visual Studio 
2012/Projects/Sigma/QtReimplementation/HLFKBase/Win32/x64/Debug"
for t in examples:
     t = t.replace("$", baseDir)
     # Using os.write as simple way get UTF-8 to stdout
     os.write(sys.stdout.fileno(), t.encode("utf-8"))
     print(sys.getsizeof(t))
     print(timeit.repeat("a[0] < a[1]",t,number=5000000))
     print()

    For a more significant performance difference try replacing the 
baseDir setting with (may be wrapped):
baseDir = "C:/Users/Neil/Documents/Visual Studio 
2012/Projects/Sigma/QtReimplementation/HLFKBase/Win32/x64/Debug"

    Neil



More information about the Python-list mailing list