Parsed the IANA subtag (see Cascaded string split, pythonic way) and made a list of 8600 tags:
tags= ['aa',
'ab',
'ae',
'af',
'ak',
'am',
'an',
'ar',
# ...
I want to check for example mytag="ro" if is in the list:
what is the fastest way to do that:
First solution:
if mytag in tags:
print "found"
Second solution:
if mytag in Set(tags):
print "found"
Third solution: Transform the list in a big string like: '|aa|ab|ae|af|ak|am|an|ar|...' and then see if string is in another string:
tags = '|aa|ab|ae|af|ak|am|an|ar|...'
if mytag in tags:
print "found"
Is there another way? Which is the fastest, is this already measured, if not how can I benchmark myself (shoul I take a random element from the list or should I take the last and then test it, can someone provide python code for a ‘chronometer’)?
As I don’t have access to the original string, any test would be biased. However, you asked for a chronometer ? Check the
timeitmodule, designed to time some code snippets.Note that if you use
IPython,%timeitis a magic function that makes it a breeze to time the execution of a function, as illustrated below.Some comments
Setbyset…setand long string before running any testtagslist is the way to go indeed.As an example of use of
%timeitin IPython: