I'm making a prime number calculator, It works perfectally, but I want it to be faster through the use of multithreading. I was wondering if any of you could point me to somewhere with some resources (the python docs are not very helpful) or give me an example in my code.
import time
#Set variables
check2 = 0
check3 = 0
check5 = 0
check7 = 0
#get number
primenum = int(input("What number do you want to find out whether it is prime or not?\nIt must be a natural number\n**Calculations may take some time depending of the power of the computer and the size of the number**\n"))
#set divisor
primediv = primenum - 2
#assume it's prime until proven innocent
prime = 1
#Create variable for GUI
working = "Calculating"
work = 10000000
#set the number of divides to 0
divnum = 0
#start the clock
starttime = time.perf_counter()
#until it is not prime or divided by 1...
while prime == 1 and primediv >7:
#does simple checks to deal with large numbers quickly
#Does this by dividing with small numbers first
if primenum != 0 and check2 == 0:
primemod = primenum % 2
check2 = 1
print(working + ".")
working = working +"."
elif primenum != 0 and check3 == 0:
primemod = primenum % 3
check3 = 1
print(working + ".")
working = working +"."
elif primenum != 0 and check5 == 0:
primemod = primenum % 5
check5 = 1
print(working + ".")
working = working + "."
elif primenum != 0 and check7 == 0:
primemod = primenum % 7
check7 = 1
print(working + ".")
working = working + "."
#divde and get remainder
else:
primemod = primenum % primediv
#Working visuals
if divnum == work:
print(working + ".")
working = working +"."
work = work + 10000000
#if the can't be devided evenly
if primemod == 0:
#then it isn't a prime
prime = 0
else:
#if it can, keep going
primediv = primediv - 2
divnum = divnum + 1
#print results
if prime == 1:
print("That number is prime")
print ("It took ", time.perf_counter()-starttime, " seconds to perform that calculation\n")
else:
print("That number is not prime")
print ("It took ", time.perf_counter()-starttime, " seconds to perform that calculation\n")
The most popular implementations of Python (CPython and PyPy) come with a Global Interpreter Lock ("GIL"). The purpose of this is basically to simplify memory management. The effect of it is that only one thread at a time can be executing Python bytecode. So for a program that does all its calculations in Python, threading won't make it faster.
Usually the best was to make a program faster is to find a better algorithm. Look e.g. at this:
(1) Even numbers >2 can be divided by two, so they cannot be prime and need not be considered.
(2): For an odd number
c
to be prime, one must ensure thatc
modulo all previous odd numbers (p
) must be non-zero.(3): 2 is also a prime number.
A small improvement is that
p
only needs to go up tosqrt(c)
.Note that I'm using list comprehensions instead of for-loops, because they are generally faster.
Finally, a kind of sieve as Adam Smith mentioned;
To see which one is faster, we need to run tests;
First for
num=100
.Then 1000:
Then 10000;
Of these algoriths,
prime_list2
seems to hold up best.