Optimizing Python Code

I’m used to Python being slow but don’t really know how to optimize it, outside of Cython and C, which I don’t have any interest in doing. I still need it to be at least a bit readable.
There are a lot of nested loops in the program and with several dozen varaibles.
Is there any way to speed up the lines of code I’m running?

1 Like

can you send the code? Aside from that, I can only give you general advice:

  • use runtime analysis to check the runtime of your code (super basic Tl;DR is that number of nested loops is amt of n^max_loop_amt)
    – side note: I’m talking about worst case scenarios which is the case you should almost always account for
  • try searching up more efficient versions of your algo
  • your computer can run abt 10^5 operations in one second in python. Think about it like this: if you have a list of 10, you can iterate over it in five nested loops. So the size of your iteration is also important

EDIT: all of these are just some tips to figure out how fast your code will run


Can you please give the code otherwise I will not be able to help you.

oh sorry if it’s a bit confusing – these things will take a bit to learn and are college-level topics that you learn in first-year classes, so I wouldn’t be surprised if it doesn’t immediately click. If it does, you are an awesome coder, otherwise, these topics will probably require an irl tutor or prof to understand better so you can ask a bunch of questions

1 Like

took me a bit to fish the file out. Here’s the a little bit readable code:

from random import randint

l = []
f = []
for i in range(0,1000000):
    l.append(randint(i, 1000000))
    for j in range(0, 1000001 - l[i]):
        f.append(randint(l[i], 1000000))
        for b in range((l[i]+f[j]), 100000000000000):
            if l[i] > f[j]:
                for n in range(f[j], l[i]):
                    a1 = randint(1, 100000)
                    a2 = randint(1, 100000)
                    print(f"{a1} - {a2} = {a1 - a2}")
            elif f[j] > l[i]:
                for n in range(l[i], f[j]):
                    a1 = randint(1, 100000)
                    a2 = randint(1, 100000)
                    print(f"{a1} + {a2} = {a1 + a2}")
for item in l:
for item in f:

EDIT: my ide’s still spitting values out.

1 Like

what is the code’s like… purpose

1 Like

Please… Please don’t name variables single letters. Variable names should be meaningful, although i, j, and k are often used in for loops which I guess is okay.


spit out values that I can then process to spit out a very random sum through a mix-up of arithmetic (*, /, +, -). Also, the program’s still going.

  1. press ctrl c to stop the code
  2. ok but you can’t really do anything more optimized for … printing out 1m values. There’s no way to do it… faster. How about trying to … do less like why do you need so many

I probably should, looking at the indexing and stuff like that.


Process finished with exit code -1073741510 (0xC000013A: interrupted by Ctrl+C)
. . . what does that mean, other than the ctrl-c interupt.


it just manually stops the code instead of completing it

what I mean is the code -1073741510. I don’t get that everytime I keyboardinterupt (I use the big red square).

1 Like

that’s an artifact from C becuase python is coded in C, running C code will basically have the compiler run main which returns an int, a flag, that states 0 if run properly, etc

now, why do you need so many numbers? Generating that many numbers is slow because of that. you’re generating that many numbers

Would the same thing go for C too?

1 Like

yes, it would apply also to C (and c++)

but would C be naturally faster at it than python?

1 Like

yes because it’s closer to the compiler than python (it’s much faster, abt 2x or more). BTW can you mark as solved anything if it helps?

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.