You can use numba to JIT your current functions, which results in significant speed improvements (I've changed it to use numpy for ceil and sqrt as it is slightly faster than using math):
import numpy as np
import numba as nb
@nb.njit
def is_prime_number(number):
if number % 2 == 0:
return False
root_of_number = np.ceil(np.sqrt(number))
for i in range(3, root_of_number + 1, 2):
if number % i == 0:
return False
return True
@nb.njit
def main(number):
largest_prime_factor = 0
for i in range(1, number + 1):
if is_prime_number(i):
if number % i == 0:
if i > largest_prime_factor:
largest_prime_factor = i
if i % 1000000 == 0:
print(i)
print(f"Largest prime factor of {number}: {largest_prime_factor}")
Timings:
%timeit main_op(7919)
%timeit main(7919)
Output:
3.83 ms ± 36.1 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
74.3 µs ± 245 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
Answer from user19393974 on Stack OverflowIs there any way to increase the performance of my python code without using threads/processes? - Stack Overflow
I’m starting a series on Python performance optimizations, Looking for real-world use cases!
Optimizing speed
Try Pandas, it handles tabular data much faster than iterating through of lists.
More on reddit.comWhen speed matters (10 Ways to Make Python Go Faster)
Videos
You can use numba to JIT your current functions, which results in significant speed improvements (I've changed it to use numpy for ceil and sqrt as it is slightly faster than using math):
import numpy as np
import numba as nb
@nb.njit
def is_prime_number(number):
if number % 2 == 0:
return False
root_of_number = np.ceil(np.sqrt(number))
for i in range(3, root_of_number + 1, 2):
if number % i == 0:
return False
return True
@nb.njit
def main(number):
largest_prime_factor = 0
for i in range(1, number + 1):
if is_prime_number(i):
if number % i == 0:
if i > largest_prime_factor:
largest_prime_factor = i
if i % 1000000 == 0:
print(i)
print(f"Largest prime factor of {number}: {largest_prime_factor}")
Timings:
%timeit main_op(7919)
%timeit main(7919)
Output:
3.83 ms ± 36.1 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
74.3 µs ± 245 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
Also, for starters, you dont need to check all numbers up to "number+1". It is sufficient to test all numbers up to int(sqrt(number))+1. If you find a prime number, you divide your original number by that and repeat (recursively). At the end of this recursion, you will be left with some number which is itself prime and there will be no divisor found until sqrt(N). If that is the case, the number itself is a prime. I think with this method you will find primes quicker. E.g. for 3x7x7, you will just need 2+4+4 steps, instead of going through all the numbers. The last prime in the recursion should also be the largest prime factor, if I am not mistaken.
About code efficiency: Generally one should try to avoid for loops in python. I recommend you to look into e.g. the primefac module. I think you can get the full prime factorization of a number n by primefac.primefac(n) and then you can just pick its maximum (I am not too familiar with this module tho.)
Hey everyone,
I’m planning to start a series (not sure yet if it’ll be a blog, video, podcast, or something else) focused on Python performance. The idea is to explore concrete ways to:
-
Make Python code run faster
-
Optimize memory usage
-
Reduce infrastructure costs (e.g., cloud bills)
I’d love to base this on real-world use cases instead of just micro-benchmarks or contrived examples.
If you’ve ever run into performance issues in Python whether it’s slow scripts, web backends costing too much to run, or anything else I’d really appreciate if you could share your story.
These will serve as case studies for me to propose optimizations, compare approaches, and hopefully make the series valuable for the community.
Thanks in advance for any examples you can provide!