Skip to content

Instantly share code, notes, and snippets.

@ExpHP
Created August 22, 2017 23:25
Show Gist options
  • Select an option

  • Save ExpHP/9b65c918e8e5a5fc2ae5dfe63f7675cf to your computer and use it in GitHub Desktop.

Select an option

Save ExpHP/9b65c918e8e5a5fc2ae5dfe63f7675cf to your computer and use it in GitHub Desktop.
import numpy as np
import timeit

def measure(s):
	ts = timeit.repeat(s, globals=globals())
	print(f'{s:>20}: {min(ts):>6.3f}s ({ts})')


x = 2
xs = [0,1,2]
identity = lambda x: x
videntity = np.vectorize(identity)
vfloat    = np.vectorize(identity, otypes=['float64'])
vobject   = np.vectorize(identity, otypes=['object'])

print(np.__version__)
print(f'{"snippet":^20}  best  (all times)')
measure('videntity(x)')
measure('vfloat(x)')
measure('vobject(x)')
measure('videntity(xs)')
measure('vfloat(xs)')
measure('vobject(xs)')

I benched each commit twice and alternated between them to help rule out large-scale fluctuations in background CPU load. Times between the last a3507d3 run and the last 707f33f run differ by almost a constant 0.4s, which is the kind of difference I expected to see)

1.14.0.dev0+a3507d3 (vectorize-decorator)
      snippet         best  (all times)
        videntity(x): 21.768s ([22.481908158000806, 22.126309535000473, 21.767917130999194])
           vfloat(x): 10.091s ([10.213460775001295, 10.102545202998954, 10.090628451998782])
          vobject(x):  9.759s ([9.792092114001207, 9.759098587001063, 9.782961138000246])
       videntity(xs): 25.536s ([25.586909683999693, 25.535762661000263, 25.539334753999356])
          vfloat(xs): 12.238s ([12.257686156001, 12.247956515999249, 12.238118137000129])
         vobject(xs): 10.387s ([10.38699293800164, 10.402060299000368, 10.409089253000275])

1.14.0.dev0+707f33f (master)
      snippet         best  (all times)
        videntity(x): 21.030s ([21.067762631000733, 21.051139414999852, 21.029984390999743])
           vfloat(x):  9.671s ([9.67083306999848, 9.710276227000577, 9.693550022000636])
          vobject(x):  9.427s ([9.426805156001137, 9.43146521699964, 9.439738913000838])
       videntity(xs): 25.736s ([25.736185777001083, 25.75581542299915, 25.780563274000087])
          vfloat(xs): 12.114s ([12.113508943000852, 12.125795419999122, 12.124225663999823])
         vobject(xs): 10.150s ([10.17923738799982, 10.149743374000536, 10.175849371000368])

1.14.0.dev0+a3507d3  (vectorize-decorator)
      snippet         best  (all times)
        videntity(x): 21.581s ([21.581047177000073, 21.620422907999455, 21.600887791999412])
           vfloat(x): 10.104s ([10.130099814999994, 10.116891741001382, 10.104108626999732])
          vobject(x):  9.834s ([9.873765270000149, 9.833725597000011, 9.848447660000602])
       videntity(xs): 26.053s ([26.079194400001143, 26.092705879000277, 26.0533020769999])
          vfloat(xs): 12.454s ([12.458595768001032, 12.453564949999418, 12.461592382998788])
         vobject(xs): 10.570s ([10.581810928999403, 10.570412836999822, 10.583278764999704])

1.14.0.dev0+707f33f (master)
      snippet         best  (all times)
        videntity(x): 20.952s ([21.0368907510001, 20.951934971999435, 21.000456947000202])
           vfloat(x):  9.706s ([9.710992902999351, 9.715530942999976, 9.70636702900083])
          vobject(x):  9.415s ([9.44331799900101, 9.414527223998448, 9.427343850000398])
       videntity(xs): 25.608s ([25.612814928999796, 26.496048314998916, 25.60797780499888])
          vfloat(xs): 12.072s ([12.072046217999741, 12.115006013000311, 12.097886954999922])
         vobject(xs): 10.126s ([10.134322165000412, 10.126409878999766, 10.132111399001587])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment