Reputation: 109
As per my understanding (but actually didn't test) calculating len of string (for example) with len() func takes less time than simply calculating number of chars in the same string cause len() somehow optimized. Is it the case and how it works in simple words?
The follow up practical question probably would be : " If possible is it a good practice in general to use something like len(string) for early false detection in code in terms of O(code) complexity"
Thanks
UPDATED. Originally I didn't present any code so now just adding it and hope that would be a bit representative if I haven't mess a lot with it.
import timeit
def len_test(arr):
return len(arr)
def manual_count(num):
ln = 0
i = 0
while i < num:
ln += 1
i += 1
return ln
for i in range(1000, 10000, 1000):
count = i
array = [_ for _ in range(i)]
t1 = timeit.timeit(stmt='len_test(array)', setup='from __main__ import len_test, array', number=count)
t2 = timeit.timeit(stmt='manual_count(count)', setup='from __main__ import manual_count, count', number=count)
print('i: {}, len:{:.8}, count: {:.8}'.format(i, t1, t2))
i: 1000, len:0.0001499, count: 0.12168087
i: 2000, len:0.000327363, count: 0.53221788
i: 3000, len:0.000449699, count: 1.167412
i: 4000, len:0.000595823, count: 2.1061223
i: 5000, len:0.000762714, count: 3.2617024
i: 6000, len:0.000937534, count: 4.8079927
i: 7000, len:0.001076862, count: 6.5171025
i: 8000, len:0.001222231, count: 9.3587468
i: 9000, len:0.001398561, count: 11.686714
Upvotes: 1
Views: 2389
Reputation: 41
I checked it in a real condition and counting was 2 time or more faster than len().
My code that read two CSV format texts and then calculate number of lines is this:
import time
import csv
start_time = time.time()
psr = open('e_psr.txt')
cpr = open('e_cp.txt')
csv_psr = csv.reader(psr, delimiter=',')
csv_cp = csv.reader(cpr, delimiter=',')
csv_cp_copy = []
csv_psr_copy = []
r=0
e=0
for row in csv_psr:
csv_psr_copy.append(row)
for row in csv_cp:
csv_cp_copy.append(row)
e = len(csv_cp_copy)
r = len(csv_psr_copy)
psr.close()
cpr.close()
print(e,r)
print("\n--- %s seconds ---" % (time.time() - start_time))
and when I replaced len()
with a simple counter in for loop (e += 1
) the result significantly changed.
return with len():
10000 10000
--- 0.13390278816223145 seconds ---
return with counter:
10000 10000
--- 0.05642294883728027 seconds ---
Upvotes: 3
Reputation: 523
from timeit import default_timer as timer
test = [x for x in range(1000)]
count = 0
start = timer()
len(test)
end = timer()
print(end - start)
start = timer()
for i in test:
count += 1
end = timer()
print(end - start)
Returns:
2.643069343567298e-06
213.7110354941546e-06
If test = "This is a test string."
Returns:
2.2654880087719696e-06
1.0572277374269745e-05
Upvotes: 2