I'm trying to find the length of a string by comparing the string to different strings. Python compares strings as follows:
if (op == Py_EQ) {
/* Supporting Py_NE here as well does not save
much time, since Py_NE is rarely used. */
if (Py_SIZE(a) == Py_SIZE(b)
&& (a->ob_sval[0] == b->ob_sval[0]
&& memcmp(a->ob_sval, b->ob_sval, Py_SIZE(a)) == 0)) {
result = Py_True;
} else {
result = Py_False;
}
goto out;
}
The way I see it (maybe I'm wrong), it is suppose to take less time to compare strings with different lengths than strings with the same length. I've built this function:
def find_length(string, possible_length = xrange(1, 33)):
l = []
for i in possible_length:
temp = '*' * i
l.append(timeit.timeit(lambda: temp == string, number=10**5))
return l.index(max(l)) + 1
And when using it like this: print find_length('test')
I was expecting to get the result of 4, but instead I got (after I ran it 5 times): 20, 10, 26, 22, 8.
First I thought that perhaps because I'm dealing with such short times 10^5 isn't enough but it gave the same results (not 20,10,26... but inconsistent results as well).
Does anyone find a mistake in my code / logic?