Skip to content

Instantly share code, notes, and snippets.

@buptxge
Created March 11, 2016 09:09
Show Gist options
  • Save buptxge/7bb0787afdf1ac691769 to your computer and use it in GitHub Desktop.
Save buptxge/7bb0787afdf1ac691769 to your computer and use it in GitHub Desktop.
ProcessPoolExector test
import concurrent.futures
import math
import time
PRIMES = [
112272535095293,
112272535095293,
112582705942171,
112272535095293,
115280095190773,
115797848077099,
112582705942171,
112272535095293,
115280095190773,
115797848077099,
1099726899285419]
def is_prime(n):
if n % 2 == 0:
return False
sqrt_n = int(math.floor(math.sqrt(n)))
for i in range(3, sqrt_n + 1, 2):
if n % i == 0:
return False
return True
def main():
t1 = time.time()
with concurrent.futures.ProcessPoolExecutor(max_workers=1) as executor:
for number, prime in zip(PRIMES, executor.map(is_prime, PRIMES)):
print('%d is prime: %s' % (number, prime))
t2 = time.time()
print "1 worker"
print t2-t1
t1 = time.time()
with concurrent.futures.ProcessPoolExecutor(max_workers=2) as executor:
for number, prime in zip(PRIMES, executor.map(is_prime, PRIMES)):
print('%d is prime: %s' % (number, prime))
t2 = time.time()
print "2 workers"
print t2-t1
t1 = time.time()
with concurrent.futures.ProcessPoolExecutor(max_workers=3) as executor:
for number, prime in zip(PRIMES, executor.map(is_prime, PRIMES)):
print('%d is prime: %s' % (number, prime))
t2 = time.time()
print "3 workers"
print t2-t1
t1 = time.time()
with concurrent.futures.ProcessPoolExecutor(max_workers=4) as executor:
for number, prime in zip(PRIMES, executor.map(is_prime, PRIMES)):
print('%d is prime: %s' % (number, prime))
t2 = time.time()
print "4 workers"
print t2-t1
t1 = time.time()
with concurrent.futures.ProcessPoolExecutor(max_workers=100) as executor:
for number, prime in zip(PRIMES, executor.map(is_prime, PRIMES)):
print('%d is prime: %s' % (number, prime))
t2 = time.time()
print "100 workers"
print t2-t1
t1 = time.time()
with concurrent.futures.ProcessPoolExecutor() as executor:
for number, prime in zip(PRIMES, executor.map(is_prime, PRIMES)):
print('%d is prime: %s' % (number, prime))
t2 = time.time()
print "not set workers"
print t2-t1
t1 = time.time()
for prime in PRIMES:
print('%d is prime: %s' % (prime, is_prime(prime)))
t2 = time.time()
print t2-t1
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment