def single_thread(): print 'single_thread start: %s' % seconds_to_human(time.time()) for i in xrange(START, STOP, STEP): print sum_prime(i) print 'single_thread end: %s' % seconds_to_human(time.time())
def single_sum_zero(): start_time = seconds_to_human(time.time()) count = 0 for step in xrange(len(test_list)): for i in xrange(len(test_list) - step): if sum(test_list[i:i + step + 1]) == 0: count += 1 print 'single_sum_zero count: %s' % count print 'single_sum_zero start: %s' % start_time print 'single_sum_zero end: %s' % seconds_to_human(time.time())
def single_sum_zero(): start_time = seconds_to_human(time.time()) count = 0 for step in xrange(len(test_list)): for i in xrange(len(test_list) - step): if sum(test_list[i:i+step+1]) == 0: count += 1 print 'single_sum_zero count: %s' % count print 'single_sum_zero start: %s' % start_time print 'single_sum_zero end: %s' % seconds_to_human(time.time())
def multi_thread(): print 'multi_thread start: %s' % seconds_to_human(time.time()) work_queue = Queue() for i in xrange(START, STOP, STEP): work_queue.put(i) threads = [Thread(target=do_work, args=(work_queue, )) for i in range(8)] [t.start() for t in threads] [t.join() for t in threads] print 'multi_thread end: %s' % seconds_to_human(time.time())
def multi_process(): print 'multi_process start: %s' % seconds_to_human(time.time()) work_queue = multiprocessing.Queue() for i in xrange(START, STOP, STEP): work_queue.put(i) processes = [multiprocessing.Process(target=do_work, args=(work_queue, )) for i in range(8)] [t.start() for t in processes] [t.join() for t in processes] print 'multi_process end: %s' % seconds_to_human(time.time())
def multiprocess_sum_zero(): start_time = seconds_to_human(time.time()) step_queue = multiprocessing.Queue() for i in xrange(len(test_list) - 1): step_queue.put(i) processes = [multiprocessing.Process(target=sum_zero, args=(step_queue, )) for i in range(8)] [p.start() for p in processes] [p.join() for p in processes] print 'multi_sum_zero start: %s' % start_time print 'multi_sum_zero end: %s' % seconds_to_human(time.time())
def multi_process(): print 'multi_process start: %s' % seconds_to_human(time.time()) work_queue = multiprocessing.Queue() for i in xrange(START, STOP, STEP): work_queue.put(i) processes = [ multiprocessing.Process(target=do_work, args=(work_queue, )) for i in range(8) ] [t.start() for t in processes] [t.join() for t in processes] print 'multi_process end: %s' % seconds_to_human(time.time())
def multiprocess_sum_zero(): start_time = seconds_to_human(time.time()) step_queue = multiprocessing.Queue() for i in xrange(len(test_list) - 1): step_queue.put(i) processes = [ multiprocessing.Process(target=sum_zero, args=(step_queue, )) for i in range(8) ] [p.start() for p in processes] [p.join() for p in processes] print 'multi_sum_zero start: %s' % start_time print 'multi_sum_zero end: %s' % seconds_to_human(time.time())
def multiprocess_pool_sum_zero(): result = [] count = 0 start_time = seconds_to_human(time.time()) p = multiprocessing.Pool(8) for i in xrange(len(test_list) - 1): result.append(p.apply_async(pool_sum_zero, (i, ))) # async实现异步, apply和map区别在于: apply可以多个函数, map同一个函数和iterator p.close() p.join() for res in result: count += res.get() print 'pool_sum_zero count: %s' % count print 'pool_sum_zero start: %s' % start_time print 'pool_sum_zero end: %s' % seconds_to_human(time.time())
def multiprocess_pool_sum_zero(): result = [] count = 0 start_time = seconds_to_human(time.time()) p = multiprocessing.Pool(8) for i in xrange(len(test_list) - 1): result.append(p.apply_async( pool_sum_zero, (i, ))) # async实现异步, apply和map区别在于: apply可以多个函数, map同一个函数和iterator p.close() p.join() for res in result: count += res.get() print 'pool_sum_zero count: %s' % count print 'pool_sum_zero start: %s' % start_time print 'pool_sum_zero end: %s' % seconds_to_human(time.time())