Last active
December 17, 2015 18:08
-
-
Save dogancelik/5650542 to your computer and use it in GitHub Desktop.
(Python) timeit for getting filename without extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Things to note:
import os
rather thanfrom os.path import splitext
increases time by ~8%.lower()
increases time by ~12%