Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simplyfy
#7
Multiple different ways of doing something?  We'd better... run speed tests!!!!

...and something's fishy about the set speed...  I was expecting that to do better.  (that said, they're all close enough that it doesn't matter)

Output:
D:\Projects\playground>python -VV Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 07:18:10) [MSC v.1900 32 bit (Intel)] D:\Projects\playground>python temp.py original: 6.258850955969614 nilamo: 4.656287876513283 buran: 4.732732260557226 volcano63: 5.075134360387665 D:\Projects\playground>python temp.py original: 6.198476880215798 nilamo: 4.8221962886350305 buran: 4.808510175991959 volcano63: 5.1591497014125824 D:\Projects\playground>python temp.py original: 6.5075879446218705 nilamo: 4.677959760624388 buran: 4.862944503352491 volcano63: 5.2680807758374115
Testing code:
def original():
   word = "abcde"
   twoletter = []
   for x in range(0, (len(word)-1)):
       a = word[x]
       b = word[x+1]
       c = a + b
       d = b + a
       twoletter.append(c)
       twoletter.append(d)
       if x + 2 < len(word):
           a = word[x]
           b = word[x + 2]
           c = a + b
           d = b + a
           twoletter.append(c)
           twoletter.append(d)
           if x + 3 < len(word):
               a = word[x]
               b = word[x + 3]
               c = a + b
               d = b + a
               twoletter.append(c)
               twoletter.append(d)
               if x + 4 < len(word):
                   a = word[x]
                   b = word[x + 4]
                   c = a + b
                   d = b + a
                   twoletter.append(c)
                   twoletter.append(d)
   return twoletter

def nilamo():
   word = "abcde"
   twoletter = []
   for left in word:
       for right in word:
           if left != right:
               twoletter.append(left + right)
   return twoletter

import itertools
def buran():
   word = "abcde"
   # using list comprehension
   my_list = [''.join(perm) for perm in itertools.permutations(word,2)]
   return my_list

def volcano63():
   word_set = set('abcde')
   two_letter = [left + right for right in word_set for left in word_set - set([right])]
   return two_letter

if __name__ == "__main__":
   import timeit
   for func in ["original", "nilamo", "buran", "volcano63"]:
       print("{0}: {1}".format(func, timeit.timeit("{0}()".format(func), globals=globals())))
Reply


Messages In This Thread
Simplyfy - by podge - Apr-30-2017, 04:38 PM
RE: Simplyfy - by buran - Apr-30-2017, 04:50 PM
RE: Simplyfy - by nilamo - Apr-30-2017, 05:11 PM
RE: Simplyfy - by podge - Apr-30-2017, 06:00 PM
RE: Simplyfy - by nilamo - Apr-30-2017, 07:08 PM
RE: Simplyfy - by volcano63 - Apr-30-2017, 07:49 PM
RE: Simplyfy - by nilamo - Apr-30-2017, 08:21 PM
RE: Simplyfy - by volcano63 - Apr-30-2017, 08:30 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020