#!/usr/bin/env python

import sys
import re
import timeit
import portage
settings = portage.settings

# Build a regex from thirdpartymirrors for the SRC_URI.mirror check.
thirdpartymirrors_list = portage.flatten(settings.thirdpartymirrors().values())
thirdpartymirrors_re = re.compile(r'^(%s)' % \
	"|".join(re.escape(x) for x in thirdpartymirrors_list))

uri = "http://dev.gentoo.org/~zmedico/portage/archives/portage-2.1.5.tar.bz2"
number = 5000

def regex_imp():
	for server in thirdpartymirrors_list:
		uri.startswith(server)

def startswith_imp():
	thirdpartymirrors_re.match(uri)

def main():
	write = sys.stdout.write

	t = timeit.Timer(stmt="regex_imp()", setup="from __main__ import regex_imp")
	write("using re: %s\n\n" % t.timeit(number=number))

	t = timeit.Timer(stmt="startswith_imp()", setup="from __main__ import startswith_imp")
	write("using startswith: %s\n" % t.timeit(number=number))

if __name__ == '__main__':
	main()
