Skip to content

Instantly share code, notes, and snippets.

@thekindlyone
Created January 10, 2014 23:04
Show Gist options
  • Save thekindlyone/8364401 to your computer and use it in GitHub Desktop.
Save thekindlyone/8364401 to your computer and use it in GitHub Desktop.
comparison
def testvivek():
i=0
sum=0
giveup3=0
giveup5=0
lim=1000
while True:
num3=3*i #generating multiples for processing
num5=5*i # ^
if num3>=lim: # if multiple of 3>limit then stop adding those, and set flag
giveup3=1
num3=0
if num5>=lim: # same as ^ but for 5
giveup5=1
num5=0
if giveup3 and giveup5: # if both give up no more chance of finding any more hence stop
break
if num3%5==0: # to handle multiples of 15
num3=0
sum=sum+num3+num5
#print i,'. sum = ',sum,' num3 = ',num3,' num5 = ',num5,' giveup3 = ',giveup3,' giveup5 = ',giveup5 #log
i+=1 #counter
return sum
def testoneliner():
return sum(num for num in range(1000)if (num%3==0 or num%5==0))
if __name__ == '__main__':
import timeit
print 'vivek: '
print(timeit.timeit("testvivek()",number=1000, setup="from __main__ import testvivek"))
print 'oneliner'
print(timeit.timeit("testoneliner()",number=1000, setup="from __main__ import testoneliner"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment