| 
									
										
										
										
											1998-07-17 21:10:42 +00:00
										 |  |  | import re | 
					
						
							|  |  |  | import time | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def main(): | 
					
						
							|  |  |  |     s = "\13hello\14 \13world\14 " * 1000 | 
					
						
							|  |  |  |     p = re.compile(r"([\13\14])") | 
					
						
							|  |  |  |     timefunc(10, p.sub, "", s) | 
					
						
							|  |  |  |     timefunc(10, p.split, s) | 
					
						
							|  |  |  |     timefunc(10, p.findall, s) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def timefunc(n, func, *args, **kw): | 
					
						
							| 
									
										
										
										
											2012-04-29 03:01:20 +02:00
										 |  |  |     t0 = time.perf_counter() | 
					
						
							| 
									
										
										
										
											1998-07-17 21:10:42 +00:00
										 |  |  |     try: | 
					
						
							|  |  |  |         for i in range(n): | 
					
						
							| 
									
										
										
										
											2003-04-06 09:01:11 +00:00
										 |  |  |             result = func(*args, **kw) | 
					
						
							| 
									
										
										
										
											1998-07-17 21:10:42 +00:00
										 |  |  |         return result | 
					
						
							|  |  |  |     finally: | 
					
						
							| 
									
										
										
										
											2012-04-29 03:01:20 +02:00
										 |  |  |         t1 = time.perf_counter() | 
					
						
							| 
									
										
										
										
											1998-07-17 21:10:42 +00:00
										 |  |  |         if n > 1: | 
					
						
							| 
									
										
										
										
											2007-02-09 05:37:30 +00:00
										 |  |  |             print(n, "times", end=' ') | 
					
						
							|  |  |  |         print(func.__name__, "%.3f" % (t1-t0), "CPU seconds") | 
					
						
							| 
									
										
										
										
											1998-07-17 21:10:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | main() |