Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Coding Improvement
#4
(Mar-24-2024, 10:45 AM)MoreMoney Wrote: Thanks it's work but i'm open for improvement is it possible to increase the completion time even small amount? How to do it?
There are many ways algorithms can do some improvement,but for big leap can use eg Numba, Cython, PyPy.
As this is homework i guess is more the algorithms rute.
To show a example with Numba.
from numba import jit
import time

@jit(nopython=True)
def find_pin_numba(target_pin):
    for guess in range(1000000000):
        if guess == target_pin:
            return True
    return False

def main():
    pin_input = input("Enter PIN: ")
    # Validate input
    if not pin_input.isdigit() or len(pin_input) != 9:
        print("Entered PIN is invalid.")
        return
    target_pin = int(pin_input)
    start = time.time()
    if find_pin_numba(target_pin):
        print(f"Found PIN in {time.time() - start} seconds.")
    else:
        print("PIN not found.")

if __name__ == "__main__":
    main()
# Python
G:\div_code\po_env
λ python fast_1.py
Enter PIN: 999999999
Found pin in 46.00567603111267 seconds.

# Numba
G:\div_code\po_env
λ python fast_numba.py
Enter PIN: 999999999
Found PIN in 0.43939685821533203 seconds.
So as you see that speed goes down from 46-sec standar Python to 0.4-sec with Numba.
The only change is is comment out line 5.
#@jit(nopython=True)
MoreMoney likes this post
Reply


Messages In This Thread
Coding Improvement - by MoreMoney - Mar-21-2024, 11:12 AM
RE: ATM PIN Coding Improvement - by deanhystad - Mar-21-2024, 09:33 PM
RE: ATM PIN Coding Improvement - by MoreMoney - Mar-24-2024, 10:45 AM
RE: Coding Improvement - by snippsat - Mar-24-2024, 11:49 PM
RE: Coding Improvement - by MoreMoney - Mar-26-2024, 03:57 PM
RE: Coding Improvement - by snippsat - Mar-26-2024, 05:58 PM
RE: Coding Improvement - by MoreMoney - Mar-27-2024, 01:03 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020