Skip to content

Instantly share code, notes, and snippets.

@dogancelik
Last active December 17, 2015 18:08
Show Gist options
  • Save dogancelik/5650542 to your computer and use it in GitHub Desktop.
Save dogancelik/5650542 to your computer and use it in GitHub Desktop.
(Python) timeit for getting filename without extension
import timeit
t = timeit.Timer(stmt="splitext(__file__)[0]", setup="from os.path import splitext")
print t.timeit() # 2.1748800403
t = timeit.Timer(stmt="__file__[:-3]")
print t.timeit() # 0.110901126177
t = timeit.Timer(stmt='__file__[:__file__.rindex(".")]')
print t.timeit() # 0.387404180721
t = timeit.Timer(stmt="__file__.rsplit('.', 1)[0]")
print t.timeit() # 0.469573551729
@dogancelik
Copy link
Author

Things to note:

  • Second method assumes filename has an extension with 2 characters
  • using import os rather than from os.path import splitext increases time by ~8%
  • using .lower() increases time by ~12%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment