Home > database >  How to count the overall progress with a nested loop?
How to count the overall progress with a nested loop?

Time:09-25

my problem is the following - I can't figure out how to calculate the total execution progress with a nested loop ?

For example:

def task():
    main_theards = 5 
    target_for_one_theard = 50
    for i in range(main_theard):
        for j in range(target_for_one_theard):
              pass
    progress = 100
    return "Success"

I tried to do this:

def task():
    main_theards = 5 
    target_for_one_theard = 50
    for i in range(main_theard):
        for j in range(target_for_one_theard):
              progress = 100.0 * j / target_count
              print(progress)
    progress = 100
    return "Success"

This successfully counts the progress for the nested loop, but I need to calculate the progress relative to the thread

Expected result : the overall progress of the function takes into account the nested loop

CodePudding user response:

You can do it like this:

loops = 10
subloops = 20
for i in range(loops):
    for j in range(subloops):
        progress = 100 * (i * subloops   j) / (loops * subloops)
        print(progress)

It's confusing that you are using thread language but not using any threads. The logic would be somewhat different if you use threads and the statements run out of order.

  • Related