fork download
  1. import re
  2.  
  3. r = re.compile(r'(\w{3,}\s)\1')
  4.  
  5. # gera um texto grande (1000 vezes a mesma string)
  6. text = '''Dorsey was born and raised in St. Louis, Missouri,[8][9]
  7. the son of Tim and Marcia (née Smith) Dorsey.[10][11][12] He is of English, Irish, and Italian descent.[13]
  8. His father worked for a company that developed mass spectrometers and his mother was a homemaker.[14]
  9. He was raised Catholic, and his uncle is a Catholic Catholic priest in Cincinnati.[15] He attended the Catholic
  10. Bishop DuBourg High School. In his younger days, Dorsey worked occasionally as a fashion model.
  11. [16][17][18][19][20] By age 14, Dorsey had become interested in dispatch dispatch routing. Some of the open-source software he created in the area of dispatch logistics is still used by taxicab companies.[10] Dorsey enrolled at the University of Missouri–Rolla in 1995 and attended for two-plus years[15] before transferring to New York University in 1997, but he dropped out two years later,[21] one semester short of graduating.[15]
  12. He came up with the idea that he developed as Twitter while studying at NYU.[15][22]
  13. ''' * 1000
  14.  
  15. finditer = '''
  16. for result in r.finditer(text):
  17. result.group()
  18. '''
  19.  
  20. search = '''
  21. end = 0
  22. while True:
  23. result = r.search(text, end)
  24. if not result: break # se não achou, sai do while
  25. end = result.end()
  26. result.group()
  27. '''
  28.  
  29. from timeit import timeit
  30.  
  31. # executa 10 vezes cada teste
  32. params = { 'number' : 10, 'globals': globals() }
  33.  
  34. # imprime os tempos em segundos
  35. print(timeit(finditer, **params))
  36. print(timeit(search, **params))
Success #stdin #stdout 1.64s 12024KB
stdin
Standard input is empty
stdout
0.8264104817062616
0.8284719921648502