mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 13:41:24 +00:00 
			
		
		
		
	Speed test_thread up from 51.328s to 0.081s by reducing its sleep times. We
still sleep at all to make it likely that all threads are active at the same time.
This commit is contained in:
		
							parent
							
								
									b1d3d96374
								
							
						
					
					
						commit
						a14585308a
					
				
					 1 changed files with 10 additions and 6 deletions
				
			
		|  | @ -10,10 +10,13 @@ | |||
| NUMTRIPS = 3 | ||||
| 
 | ||||
| 
 | ||||
| _print_mutex = thread.allocate_lock() | ||||
| 
 | ||||
| def verbose_print(arg): | ||||
|     """Helper function for printing out debugging output.""" | ||||
|     if test_support.verbose: | ||||
|         print arg | ||||
|         with _print_mutex: | ||||
|             print arg | ||||
| 
 | ||||
| 
 | ||||
| class BasicThreadTest(unittest.TestCase): | ||||
|  | @ -38,8 +41,8 @@ def newtask(self): | |||
| 
 | ||||
|     def task(self, ident): | ||||
|         with self.random_mutex: | ||||
|             delay = random.random() * NUMTASKS | ||||
|         verbose_print("task %s will run for %s" % (ident, round(delay, 1))) | ||||
|             delay = random.random() / 10000.0 | ||||
|         verbose_print("task %s will run for %sus" % (ident, round(delay*1e6))) | ||||
|         time.sleep(delay) | ||||
|         verbose_print("task %s done" % ident) | ||||
|         with self.running_mutex: | ||||
|  | @ -138,11 +141,12 @@ def task2(self, ident): | |||
|                 # give it a good chance to enter the next | ||||
|                 # barrier before the others are all out | ||||
|                 # of the current one | ||||
|                 delay = 0.001 | ||||
|                 delay = 0 | ||||
|             else: | ||||
|                 with self.random_mutex: | ||||
|                     delay = random.random() * NUMTASKS | ||||
|             verbose_print("task %s will run for %s" % (ident, round(delay, 1))) | ||||
|                     delay = random.random() / 10000.0 | ||||
|             verbose_print("task %s will run for %sus" % | ||||
|                           (ident, round(delay * 1e6))) | ||||
|             time.sleep(delay) | ||||
|             verbose_print("task %s entering %s" % (ident, i)) | ||||
|             self.bar.enter() | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Jeffrey Yasskin
						Jeffrey Yasskin