Opened 12 years ago

Closed 12 years ago

#932 closed feature-request (Fixed)

[PATCH] Optimize ti_name even more

Reported by: Plisk Owned by: andar
Priority: minor Milestone:
Component: Core Version: 1.1.7
Keywords: Cc:

Description

Looking through pydoc str accidentally gave a sight to optional arg to str.split(). So here are some more tests

import os

def f1(name):
    first_slash_index = name.find("/")
    if first_slash_index != -1:
        name = name[:first_slash_index]
    return name

def f2(name):
    name = os.path.split(name)[0]
    return name

def f3(name):
    name = name.split("/")[0]
    return name

def f4(name):
    name = name.split("/", 1)[0]
    return name

if __name__ == "__main__":
    import timeit
    print timeit.Timer("f1('11/22/33/44/55/66/test.txt')", "from __main__ import f1").timeit()
    print timeit.Timer("f2('11/22/33/44/55/66/test.txt')", "from __main__ import f2").timeit()
    print timeit.Timer("f3('11/22/33/44/55/66/test.txt')", "from __main__ import f3").timeit()
    print timeit.Timer("f4('11/22/33/44/55/66/test.txt')", "from __main__ import f4").timeit()

0.595607995987
2.00995993614
0.795026063919
0.599367141724

And a patch based on this

Index: deluge/core/torrent.py
===================================================================
--- deluge/core/torrent.py      (revision 5251)
+++ deluge/core/torrent.py      (working copy)
@@ -621,7 +584,7 @@

         def ti_name():
             if self.handle.has_metadata():
-                name = self.torrent_info.file_at(0).path.split("/")[0]
+                name = self.torrent_info.file_at(0).path.split("/", 1)[0]
                 try:
                     return name.decode("utf8", "ignore")
                 except UnicodeDecodeError:

Change History (1)

comment:1 Changed 12 years ago by andar

  • Resolution set to fixed
  • Status changed from new to closed

Committed [5253]

Note: See TracTickets for help on using tickets.