dantiston
BAN USERI did the algebra for these summations and got the same penultimate result, and then solved the final summation and got:
((m-1)*((n*l) - ((l*m)/2) - (2 * l) - ((3/2)*n) - ((1/4)*m) - 3 - ((n**2)/2) + (((2*(m**2)) - m)/6)))
However, this does not produce the same results as the code, unfortunately. For instance, m=2; n=4; l=6 produces -8 instead of the desired 14
- dantiston January 08, 2014In most real world code, this would be true (for instance, getting unique IP addresses in their order of access), but I think it's good to note too that the overhead of a set is not always good. For instance
from random import randint
from datetime import datetime
long = 100000
short = 10
tests = [["********** Long list of lots of values **********", long, long],
["********** Long list of not many values **********", short, long],
["********** Short list of lots of values **********", long, short],
["********** Short list of lots of values **********", short, short]]
for test in tests:
print test[0]
items = ["".join(["@", str(randint(0,test[1]))]) for value in range(test[2])]
results = []
start = datetime.now()
result = []
for item in items:
if item not in result:
result.append(item)
results.append(result)
difference = datetime.now() - start
method1 = difference.microseconds/1000000.0
print "Method 1", method1
start = datetime.now()
result = []
values = set()
for item in items:
if item not in values:
result.append(item)
values.add(item)
results.append(result)
difference = datetime.now() - start
method2 = difference.microseconds/1000000.0
print "Method 2", method2
assert results[0] == results[1]
print "%s is faster!" % ("Method 1" if method1 < method2 else "Method 2")
Generates:
********** Long list of lots of values **********
Method 1 0.831988
Method 2 0.070339
Method 2 is faster!
********** Long list of not many values **********
Method 1 0.031872
Method 2 0.025667
Method 2 is faster!
********** Short list of lots of values **********
Method 1 8.4e-05
Method 2 3e-05
Method 2 is faster!
********** Short list of lots of values **********
Method 1 1.9e-05
Method 2 2.1e-05
Method 1 is faster!
Note that with a short list with not many values, (for instance, maybe an internal list of IP addresses on a forum or something), method 1 is faster. Just something to think about!.
I did the algebra for these summations got this result:
However, this does not produce the same results as the code, unfortunately. For instance, m=2; n=4; l=6 produces -8 instead of the desired 14
- dantiston January 08, 2014Maybe I made a mistake? I think the algebra is quite sound. I think the equation is wrong somehow...