NoGambling1 / ConsoleGames

Some checkers game like tf idk
MIT License
2 stars 1 forks source link

optimizing coded for pymatrix #13

Closed orangejuiceplz closed 3 months ago

orangejuiceplz commented 3 months ago

hi i optimized your code for you

  1. using _slots__ in the CMatrix class:

    • this is import so listen up
    • __slots__ is a memory optimization technique in python
    • it tells python to use a fixed-size array for the specified attributes instead of a dynamic dictionary
    • this reduces memory usage for each instance of the class, which is particularly beneficial when creating many objects (like in this matrix visualization).
    • it can also slightly improve access speed to the attributes.
    • theres more on the documentation, i just summarized it
  2. optimized resize_screen function with list comprehensions:

    • list comprehensions are generally faster than traditional for loops for creating lists
    • list comprehensions provide a more efficient way to create these structures
    • for example, matrix = [[CMatrix() for _ in range(COLS)] for _ in range(LINES + 1)] creates the entire matrix in a single line, which is typically faster than nested loops
  3. precomputing (is this even a word?) values in the main loop:

    • values that don't change within the loop (like update_delay = args.u * 0.01) are calculated once before the loop starts
    • this avoids recalculating these values in each iteration, saving processing time and strain on the CPU
    • Boolean flags (like is_async = args.a) are also precomputed, which can make conditional checks in the loop slightly faster.
  4. simplifying conditional statements:

    • i simplified some conditional statements for better readability and performance.
    • for example, using bold_style = curses.A_BOLD if args.b or args.B else 0 precomputes the bold style instead of checking it repeatedly
  5. variable naming and usage:

    • Using cell = matrix[i][j] in the drawing loop avoids repeated indexing of the matrix.
    • This can improve performance, especially in tight loops, as it reduces the number of times Python needs to perform index lookups.

its just a slight overview

NoGambling1 commented 3 months ago

Destiny does NOT get to write code 😭🙏

shiinadestinee commented 3 months ago

dude i fucking hate you why can't i write my own code smh

shiinadestinee commented 3 months ago

i added some potential error handling. it seems to work. take a look at it i guess?

orangejuiceplz commented 3 months ago

was late on that sorry. lookin' good for an amateur. feel free to merge

shiinadestinee commented 3 months ago

okay